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 <DateTimeInput validate={required()} /> doesn't work correctly #7744

Merged
merged 2 commits into from
May 23, 2022
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
34 changes: 34 additions & 0 deletions packages/ra-ui-materialui/src/input/DateTimeInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,39 @@ describe('<DateTimeInput />', () => {
).not.toBeNull();
});
});

it('should be displayed if field has been touched multiple times and is invalid', async () => {
const onSubmit = jest.fn();
render(
<AdminContext dataProvider={testDataProvider()}>
<SimpleForm onSubmit={onSubmit}>
<DateTimeInput
{...defaultProps}
validate={required()}
/>
</SimpleForm>
</AdminContext>
);
const input = screen.getByLabelText(
'resources.posts.fields.publishedAt *'
);
fireEvent.change(input, {
target: { value: new Date().toISOString() },
});
fireEvent.blur(input);
await waitFor(() => {
expect(screen.queryByText('ra.validation.required')).toBeNull();
});
fireEvent.change(input, {
target: { value: '' },
});
fireEvent.blur(input);
fireEvent.click(screen.getByText('ra.action.save'));
await waitFor(() => {
expect(
screen.queryByText('ra.validation.required')
).not.toBeNull();
});
});
});
});
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/src/input/DateTimeInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { InputHelperText } from './InputHelperText';
* @param {string} value Date string, formatted as yyyy-MM-ddThh:mm
* @return {Date}
*/
const parseDateTime = (value: string) => new Date(value);
const parseDateTime = (value: string) => (value ? new Date(value) : value);

/**
* Input component for entering a date and a time with timezone, using the browser locale
Expand Down