Skip to content

Commit

Permalink
[DataGrid] Fix invalid date error when filtering date/dateTime co…
Browse files Browse the repository at this point in the history
…lumns (#12709)
  • Loading branch information
cherniavskii committed Apr 11, 2024
1 parent cca7bd4 commit a84c0d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ function convertFilterItemValueToInputValue(
return '';
}
const dateCopy = new Date(itemValue);
if (Number.isNaN(dateCopy.getTime())) {
return '';
}
// The date picker expects the date to be in the local timezone.
// But .toISOString() converts it to UTC with zero offset.
// So we need to subtract the timezone offset.
Expand Down Expand Up @@ -69,7 +72,8 @@ function GridFilterInputDate(props: GridFilterInputDateProps) {

setIsApplying(true);
filterTimeout.start(rootProps.filterDebounceMs, () => {
applyValue({ ...item, value: new Date(value) });
const date = new Date(value);
applyValue({ ...item, value: Number.isNaN(date.getTime()) ? undefined : date });
setIsApplying(false);
});
},
Expand Down
24 changes: 24 additions & 0 deletions test/e2e/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
devices,
BrowserContextOptions,
BrowserType,
WebError,
} from '@playwright/test';
import { pickersTextFieldClasses } from '@mui/x-date-pickers/PickersTextField';
import { pickersSectionListClasses } from '@mui/x-date-pickers/PickersSectionList';
Expand Down Expand Up @@ -514,6 +515,29 @@ async function initializeEnvironment(
await page.locator('[role="gridcell"][data-field="brand"] input').inputValue(),
).not.to.equal('v');
});

// https://github.com/mui/mui-x/issues/12705
it('should not crash if the date is invalid', async () => {
await renderFixture('DataGrid/KeyboardEditDate');

await page.hover('div[role="columnheader"][data-field="birthday"]');
await page.click(
'div[role="columnheader"][data-field="birthday"] button[aria-label="Menu"]',
);
await page.click('"Filter"');
await page.keyboard.type('08/04/2024', { delay: 10 });

let thrownError: Error | null = null;
context.once('weberror', (webError: WebError) => {
thrownError = webError.error();
console.error(thrownError);
});

await page.keyboard.press('Backspace');

await sleep(200);
expect(thrownError).to.equal(null);
});
});

describe('<DatePicker />', () => {
Expand Down

0 comments on commit a84c0d9

Please sign in to comment.