Skip to content

Commit

Permalink
frontend: Fixing date time picker on change trigger logic (#3011)
Browse files Browse the repository at this point in the history
The current logic is missing the scenario where the allowEmpty is true
and the value is valid, this update adds that scenario.
  • Loading branch information
lucechal14 authored May 15, 2024
1 parent ca29fb4 commit 3bc4eb7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
9 changes: 3 additions & 6 deletions frontend/packages/core/src/Input/date-time.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ const DateTimePicker = ({ onChange, allowEmpty = false, ...props }: DateTimePick
<MuiDateTimePicker
renderInput={inputProps => <PaddedTextField {...inputProps} />}
onChange={(value: Dayjs | null) => {
if (!allowEmpty && value && value.isValid()) {
const dateValue = value ? value.toDate() : null;
const isDateValid = value && value.isValid();
if (allowEmpty || (!allowEmpty && isDateValid)) {
const dateValue = isDateValid ? value.toDate() : null;
onChange(dateValue);
}

if (allowEmpty && !value) {
onChange(null);
}
}}
{...props}
/>
Expand Down
14 changes: 14 additions & 0 deletions frontend/packages/core/src/Input/tests/date-time.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ test("onChange is called when valid value", () => {
expect(onChange).toHaveBeenCalled();
});

test("onChange is called when valid value and allowEmpty is true", () => {
render(
<ThemeProvider>
<DateTimePicker value={new Date()} onChange={onChange} allowEmpty />
</ThemeProvider>
);

expect(screen.getByPlaceholderText("mm/dd/yyyy hh:mm (a|p)m")).toBeVisible();
fireEvent.change(screen.getByPlaceholderText("mm/dd/yyyy hh:mm (a|p)m"), {
target: { value: "11/16/2023 02:55 AM" },
});
expect(onChange).toHaveBeenCalled();
});

test("onChange is called when value is empty", () => {
render(
<ThemeProvider>
Expand Down

0 comments on commit 3bc4eb7

Please sign in to comment.