Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ffe-datepicker-react): need to pad day and month in onChange #2456

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions packages/ffe-datepicker-react/src/datepicker/DatepickerComp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React, {
} from 'react';
import { DatepickerContext } from './DatepickerContext';
import { SpinButton } from './SpinButton';
import { PadZero } from './PadZero';
import { padZero } from './padZero';
import { Button } from '../button';
import { Calendar } from '../calendar';
import { isDateInputWithTwoDigitYear, validateDate } from '../util/dateUtil';
Expand All @@ -18,6 +18,7 @@ import { getSimpleDateFromString } from '../datelogic/simpledate';
import { ErrorFieldMessage } from '@sb1/ffe-form-react';
import i18n from '../i18n/i18n';
import { isMonth } from '../types';
import { toNumber } from './toNumber';

export interface DatepickerCompProps {
'aria-invalid'?: React.ComponentProps<'input'>['aria-invalid'];
Expand Down Expand Up @@ -261,7 +262,10 @@ export const DatepickerComp: React.FC<DatepickerCompProps> = ({
min={1}
max={31}
onSpinButtonChange={(newValue, allowFocusNext = true) => {
onChange(`${newValue}.${month}.${year}`);
onChange(
`${padZero(toNumber(newValue))}.${month}.${year}`,
);

return allowFocusNext
? setDay(newValue, () =>
monthRef.current?.focus({
Expand All @@ -279,7 +283,7 @@ export const DatepickerComp: React.FC<DatepickerCompProps> = ({
aria-describedby={ariaDescribedby()}
aria-labelledby={labelId}
>
{day ? <PadZero value={day} /> : 'dd'}
{day ? padZero(day) : 'dd'}
</SpinButton>
.
<SpinButton
Expand All @@ -288,7 +292,9 @@ export const DatepickerComp: React.FC<DatepickerCompProps> = ({
min={1}
max={12}
onSpinButtonChange={(newValue, allowFocusNext = true) => {
onChange(`${day}.${newValue}.${year}`);
onChange(
`${day}.${padZero(toNumber(newValue))}.${year}`,
);
return allowFocusNext
? setMonth(newValue, () =>
yearRef.current?.focus({
Expand All @@ -313,7 +319,7 @@ export const DatepickerComp: React.FC<DatepickerCompProps> = ({
aria-describedby={ariaDescribedby()}
aria-labelledby={labelId}
>
{month ? <PadZero value={month} /> : 'mm'}
{month ? padZero(month) : 'mm'}
</SpinButton>
.
<SpinButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { createContext, useState } from 'react';
import { Locale } from '../datelogic/types';
import { validateDate } from '../util/dateUtil';
import { getSimpleDateFromString } from '../datelogic/simpledate';
import { toNumber } from './toNumber';

interface DatepickerContextInterface {
day?: number | null;
Expand Down Expand Up @@ -55,16 +56,6 @@ export const DatepickerProvider: React.FC<Props> = ({
newDate?.toString() ?? '',
);

const getTotal = (numbers: (number | undefined)[]) => {
const validNumbers = numbers.filter(it => typeof it === 'number');
return validNumbers
.map(
(it, index) =>
(it ?? 1) * Math.pow(10, validNumbers.length - index - 1),
)
.reduce((acc, curr) => acc + curr, 0);
};

return (
<DatepickerContext.Provider
value={{
Expand All @@ -74,7 +65,7 @@ export const DatepickerProvider: React.FC<Props> = ({
setDay: (newValue, focusNext = undefined) => {
const numbers = newValue.slice(-2);
const [first, second] = numbers;
const total = getTotal(numbers);
const total = toNumber(numbers);
if (total > MAX_DAYS) {
focusNext?.();
} else if (first > 3) {
Expand All @@ -90,7 +81,7 @@ export const DatepickerProvider: React.FC<Props> = ({
setMonth: (newValue, focusNext = undefined) => {
const numbers = newValue.slice(-2);
const [first, second] = numbers;
const total = getTotal(numbers);
const total = toNumber(numbers);

if (total > MONTHS_PER_YEAR) {
focusNext?.();
Expand All @@ -105,7 +96,7 @@ export const DatepickerProvider: React.FC<Props> = ({
}
},
setYear: newValue => {
setYear(getTotal(newValue.slice(-4)));
setYear(toNumber(newValue.slice(-4)));
},
calendarActiveDate,
setCalendarActiveDate: date => {
Expand Down
12 changes: 0 additions & 12 deletions packages/ffe-datepicker-react/src/datepicker/PadZero.tsx

This file was deleted.

6 changes: 6 additions & 0 deletions packages/ffe-datepicker-react/src/datepicker/padZero.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const padZero = (value: number) => {
if (value < 10) {
return `0${value}`;
}
return value;
};
9 changes: 9 additions & 0 deletions packages/ffe-datepicker-react/src/datepicker/toNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const toNumber = (numbers: (number | undefined)[]) => {
const validNumbers = numbers.filter(it => typeof it === 'number');
return validNumbers
.map(
(it, index) =>
(it ?? 1) * Math.pow(10, validNumbers.length - index - 1),
)
.reduce((acc, curr) => acc + curr, 0);
};
Loading