Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexShukel committed Sep 12, 2023
1 parent 90e955c commit fc9c8a7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
6 changes: 4 additions & 2 deletions packages/x/src/useConverterField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ export class ConversionError extends Error {
}
}

export type ConverterFieldConfig<T> = {
export type ValueConverter<T> = {
parse: (value: string) => T;
format: (value: T) => string;
} & FieldConfig<T>;
};

export type ConverterFieldConfig<T> = ValueConverter<T> & FieldConfig<T>;

export type ConverterFieldBag<T> = {
text: string;
Expand Down
19 changes: 8 additions & 11 deletions packages/x/src/useDateField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import customParseFormat from 'dayjs/plugin/customParseFormat';

import { DateFieldI18nContext } from './DateFieldI18n';
import { formatDate } from './formatDate';
import { ConversionError, ConverterFieldBag, useConverterField } from './useConverterField';
import { ConversionError, ConverterFieldBag, useConverterField, ValueConverter } from './useConverterField';

dayjs.extend(customParseFormat);

Expand All @@ -24,10 +24,7 @@ export type DateFieldConfig = FieldConfig<Date | null | undefined> & {
minDate?: Date;
maxDate?: Date;
pickTime?: boolean;

formatDate?: (date: Date | null | undefined, pickTime: boolean) => string;
parseDate?: (text: string, pickTime: boolean) => Date;
};
} & Partial<ValueConverter<Date | null | undefined>>;

export type DateFieldBag = ConverterFieldBag<Date | null | undefined>;

Expand All @@ -39,19 +36,19 @@ export const useDateField = ({
minDate,
maxDate,
pickTime = false,
formatDate: customFormatDate,
parseDate: customParseDate,
format: customFormatDate,
parse: customParseDate,
}: DateFieldConfig): DateFieldBag => {
const i18n = useContext(DateFieldI18nContext);

const parse = useCallback(
(text: string) => {
text = text.trim();

if (customParseDate) {
return customParseDate(text, pickTime);
return customParseDate(text);
}

text = text.trim();

if (text.length === 0) {
return null;
}
Expand All @@ -70,7 +67,7 @@ export const useDateField = ({
const format = useCallback(
(value: Date | null | undefined) => {
if (customFormatDate) {
return customFormatDate(value, pickTime);
return customFormatDate(value);
}

return formatDate(value, pickTime);
Expand Down
12 changes: 6 additions & 6 deletions packages/x/tests/useDateField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,25 +252,25 @@ describe('Date field', () => {
});

it('Should be able to format date differently', () => {
const formatDate = jest.fn(() => 'custom');
const format = jest.fn(() => 'custom');
const initialValue = new Date();
const [{ result }] = renderUseDateField({ formatDate, initialValue });
const [{ result }] = renderUseDateField({ format, initialValue });

expect(result.current.text).toBe('custom');
expect(formatDate).toBeCalledWith(initialValue, false);
expect(format).toBeCalledWith(initialValue);
});

it('Should call custom parseDate function', async () => {
const parseDate = jest.fn();
const parse = jest.fn();

const [{ result }] = renderUseDateField({ parseDate });
const [{ result }] = renderUseDateField({ parse });

await act(() => {
result.current.onTextChange('2023-09-12');
});

await waitFor(() => {
expect(parseDate).toBeCalledWith('2023-09-12', false);
expect(parse).toBeCalledWith('2023-09-12');
});
});

Expand Down

0 comments on commit fc9c8a7

Please sign in to comment.