Skip to content

Commit

Permalink
fix: onChangeFormatter in datepicker in form component
Browse files Browse the repository at this point in the history
affects: @medly-components/forms
  • Loading branch information
gmukul01 committed Jul 20, 2024
1 parent 08c57f3 commit a4e4442
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/forms/src/components/Fields/Fields.components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ export const Fields: FC<FieldsProps> = memo(props => {
{...({
...commonProps,
value: value || null,
onChange: handlers.handleDateChange(name, (componentProps as DatePickerProps).displayFormat)
onChange: handlers.handleDateChange(
name,
(componentProps as DatePickerProps).displayFormat,
(componentProps as DatePickerProps).onChangeFormatter
)
} as GetComponentProps<typeof DatePicker>)}
/>
);
Expand Down
34 changes: 34 additions & 0 deletions packages/forms/src/components/Form/Form.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { cleanup, fireEvent, render, screen, waitFor } from '@test-utils';
import { formatRFC3339 } from 'date-fns';
import { FormCustomComponent } from '../Fields/types';
import { Form } from './Form';
import { testSchema } from './testSchema';
Expand Down Expand Up @@ -332,6 +333,39 @@ describe('Form', () => {
});
});

it('with onChangeFormatter', async () => {
const mockOnSubmit = jest.fn(),
formData = {
birthDate: '2024-07-19T00:00:00+05:30'
};
render(
<Form
name="Test form"
fieldSchema={{
birthDate: {
type: 'date',
label: 'Birth Date',
placeholder: 'Birth Date',
displayFormat: 'dd/MM/yyyy',
valueFormatter: value => (value ? new Date(value) : null),
onChangeFormatter: value => (value ? formatRFC3339(value) : null)
}
}}
onSubmit={mockOnSubmit}
initialState={{
birthDate: '2024-07-18T00:00:00+05:30'
}}
/>
);
// DatePicker
fireEvent.change(screen.getByRole('textbox', { name: 'Birth Date' }), {
target: { value: '19/07/2024' }
});

fireEvent.submit(screen.getByRole('form'));
expect(mockOnSubmit).toHaveBeenCalledWith(formData);
});

it('should allow user to clear number input', () => {
render(
<Form
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/hooks/useForm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface Handlers {
// SingleSelect change handler
handleSingleSelectChange: (name: string) => (value: any) => void;
// DatePicker Component
handleDateChange: (name: string, displayFormat: DisplayFormat) => (value: any) => void;
handleDateChange: (name: string, displayFormat: DisplayFormat, onChangeFormatter?: (value: Date | null) => any) => (value: any) => void;
// DateRangePicker Component
handleDateRangeChange: (name: string, displayFormat: DisplayFormat) => (value: any) => void;
// CheckboxGroup change handler
Expand Down
4 changes: 2 additions & 2 deletions packages/forms/src/hooks/useForm/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export const useForm = (initialState: Record<string, unknown>): UseFormResult =>
);

const handleDateChange: Handlers['handleDateChange'] = useCallback(
memoize((name, displayFormat) => value => {
setValues(val => ({ ...val, [name]: value ? format(value, displayFormat || 'MM/dd/yyyy') : '' }));
memoize((name, displayFormat, onChangeFormatter) => value => {
setValues(val => ({ ...val, [name]: value ? (onChangeFormatter ? value : format(value, displayFormat || 'MM/dd/yyyy')) : '' }));
}),
[]
);
Expand Down

0 comments on commit a4e4442

Please sign in to comment.