diff --git a/docs/data/data-grid/custom-columns/EditingWithDatePickers.js b/docs/data/data-grid/custom-columns/EditingWithDatePickers.js index 975cfe98b71e..c05f6a6dd1f4 100644 --- a/docs/data/data-grid/custom-columns/EditingWithDatePickers.js +++ b/docs/data/data-grid/custom-columns/EditingWithDatePickers.js @@ -4,6 +4,7 @@ import { useGridApiContext, GRID_DATE_COL_DEF, GRID_DATETIME_COL_DEF, + getGridDateOperators, } from '@mui/x-data-grid'; import { randomCreatedDate, @@ -18,131 +19,6 @@ import InputBase from '@mui/material/InputBase'; import locale from 'date-fns/locale/en-US'; import { styled } from '@mui/material/styles'; -function buildApplyDateFilterFn(filterItem, compareFn, showTime = false) { - if (!filterItem.value) { - return null; - } - - // Make a copy of the date to not reset the hours in the original object - const filterValueCopy = new Date(filterItem.value); - filterValueCopy.setHours(0, 0, 0, 0); - - const filterValueMs = filterValueCopy.getTime(); - - return (value) => { - if (!value) { - return false; - } - - // Make a copy of the date to not reset the hours in the original object - const dateCopy = new Date(value); - dateCopy.setHours( - showTime ? value.getHours() : 0, - showTime ? value.getMinutes() : 0, - 0, - 0, - ); - const cellValueMs = dateCopy.getTime(); - - return compareFn(cellValueMs, filterValueMs); - }; -} - -function getDateFilterOperators(showTime = false) { - return [ - { - value: 'is', - getApplyFilterFn: (filterItem) => { - return buildApplyDateFilterFn( - filterItem, - (value1, value2) => value1 === value2, - showTime, - ); - }, - InputComponent: GridFilterDateInput, - InputComponentProps: { showTime }, - }, - { - value: 'not', - getApplyFilterFn: (filterItem) => { - return buildApplyDateFilterFn( - filterItem, - (value1, value2) => value1 !== value2, - showTime, - ); - }, - InputComponent: GridFilterDateInput, - InputComponentProps: { showTime }, - }, - { - value: 'after', - getApplyFilterFn: (filterItem) => { - return buildApplyDateFilterFn( - filterItem, - (value1, value2) => value1 > value2, - showTime, - ); - }, - InputComponent: GridFilterDateInput, - InputComponentProps: { showTime }, - }, - { - value: 'onOrAfter', - getApplyFilterFn: (filterItem) => { - return buildApplyDateFilterFn( - filterItem, - (value1, value2) => value1 >= value2, - showTime, - ); - }, - InputComponent: GridFilterDateInput, - InputComponentProps: { showTime }, - }, - { - value: 'before', - getApplyFilterFn: (filterItem) => { - return buildApplyDateFilterFn( - filterItem, - (value1, value2) => value1 < value2, - showTime, - ); - }, - InputComponent: GridFilterDateInput, - InputComponentProps: { showTime }, - }, - { - value: 'onOrBefore', - getApplyFilterFn: (filterItem) => { - return buildApplyDateFilterFn( - filterItem, - (value1, value2) => value1 <= value2, - showTime, - ); - }, - InputComponent: GridFilterDateInput, - InputComponentProps: { showTime }, - }, - { - value: 'isEmpty', - getApplyFilterFn: () => { - return (value) => { - return value == null; - }; - }, - requiresFilterValue: false, - }, - { - value: 'isNotEmpty', - getApplyFilterFn: () => { - return (value) => { - return value != null; - }; - }, - requiresFilterValue: false, - }, - ]; -} - const dateAdapter = new AdapterDateFns({ locale }); /** @@ -155,7 +31,11 @@ const dateColumnType = { renderEditCell: (params) => { return ; }, - filterOperators: getDateFilterOperators(), + filterOperators: getGridDateOperators(false).map((item) => ({ + ...item, + InputComponent: GridFilterDateInput, + InputComponentProps: { showTime: false }, + })), valueFormatter: (params) => { if (typeof params.value === 'string') { return params.value; @@ -207,7 +87,7 @@ function GridFilterDateInput(props) { return ( { return ; }, - filterOperators: getDateFilterOperators(true), + filterOperators: getGridDateOperators(true).map((item) => ({ + ...item, + InputComponent: GridFilterDateInput, + InputComponentProps: { showTime: true }, + })), valueFormatter: (params) => { if (typeof params.value === 'string') { return params.value; diff --git a/docs/data/data-grid/custom-columns/EditingWithDatePickers.tsx b/docs/data/data-grid/custom-columns/EditingWithDatePickers.tsx index 6036f41eafbb..c380a85f6e07 100644 --- a/docs/data/data-grid/custom-columns/EditingWithDatePickers.tsx +++ b/docs/data/data-grid/custom-columns/EditingWithDatePickers.tsx @@ -7,11 +7,9 @@ import { GridRenderEditCellParams, GRID_DATE_COL_DEF, GRID_DATETIME_COL_DEF, - GridFilterItem, GridColTypeDef, GridFilterInputValueProps, - GetApplyFilterFn, - GridFilterOperator, + getGridDateOperators, } from '@mui/x-data-grid'; import { randomCreatedDate, @@ -27,137 +25,6 @@ import locale from 'date-fns/locale/en-US'; import { styled } from '@mui/material/styles'; import { TextFieldProps } from '@mui/material/TextField'; -function buildApplyDateFilterFn( - filterItem: GridFilterItem, - compareFn: (value1: number, value2: number) => boolean, - showTime: boolean = false, -): ReturnType> { - if (!filterItem.value) { - return null; - } - - // Make a copy of the date to not reset the hours in the original object - const filterValueCopy = new Date(filterItem.value); - filterValueCopy.setHours(0, 0, 0, 0); - - const filterValueMs = filterValueCopy.getTime(); - - return (value) => { - if (!value) { - return false; - } - - // Make a copy of the date to not reset the hours in the original object - const dateCopy = new Date(value); - dateCopy.setHours( - showTime ? value.getHours() : 0, - showTime ? value.getMinutes() : 0, - 0, - 0, - ); - const cellValueMs = dateCopy.getTime(); - - return compareFn(cellValueMs, filterValueMs); - }; -} - -function getDateFilterOperators( - showTime: boolean = false, -): GridFilterOperator[] { - return [ - { - value: 'is', - getApplyFilterFn: (filterItem) => { - return buildApplyDateFilterFn( - filterItem, - (value1, value2) => value1 === value2, - showTime, - ); - }, - InputComponent: GridFilterDateInput, - InputComponentProps: { showTime }, - }, - { - value: 'not', - getApplyFilterFn: (filterItem) => { - return buildApplyDateFilterFn( - filterItem, - (value1, value2) => value1 !== value2, - showTime, - ); - }, - InputComponent: GridFilterDateInput, - InputComponentProps: { showTime }, - }, - { - value: 'after', - getApplyFilterFn: (filterItem) => { - return buildApplyDateFilterFn( - filterItem, - (value1, value2) => value1 > value2, - showTime, - ); - }, - InputComponent: GridFilterDateInput, - InputComponentProps: { showTime }, - }, - { - value: 'onOrAfter', - getApplyFilterFn: (filterItem) => { - return buildApplyDateFilterFn( - filterItem, - (value1, value2) => value1 >= value2, - showTime, - ); - }, - InputComponent: GridFilterDateInput, - InputComponentProps: { showTime }, - }, - { - value: 'before', - getApplyFilterFn: (filterItem) => { - return buildApplyDateFilterFn( - filterItem, - (value1, value2) => value1 < value2, - showTime, - ); - }, - InputComponent: GridFilterDateInput, - InputComponentProps: { showTime }, - }, - { - value: 'onOrBefore', - getApplyFilterFn: (filterItem) => { - return buildApplyDateFilterFn( - filterItem, - (value1, value2) => value1 <= value2, - showTime, - ); - }, - InputComponent: GridFilterDateInput, - InputComponentProps: { showTime }, - }, - { - value: 'isEmpty', - getApplyFilterFn: () => { - return (value): boolean => { - return value == null; - }; - }, - requiresFilterValue: false, - }, - { - value: 'isNotEmpty', - getApplyFilterFn: () => { - return (value): boolean => { - return value != null; - }; - }, - requiresFilterValue: false, - }, - ]; -} - const dateAdapter = new AdapterDateFns({ locale }); /** @@ -170,7 +37,11 @@ const dateColumnType: GridColTypeDef = { renderEditCell: (params) => { return ; }, - filterOperators: getDateFilterOperators(), + filterOperators: getGridDateOperators(false).map((item) => ({ + ...item, + InputComponent: GridFilterDateInput, + InputComponentProps: { showTime: false }, + })), valueFormatter: (params) => { if (typeof params.value === 'string') { return params.value; @@ -231,7 +102,7 @@ function GridFilterDateInput( return ( = { renderEditCell: (params) => { return ; }, - filterOperators: getDateFilterOperators(true), + filterOperators: getGridDateOperators(true).map((item) => ({ + ...item, + InputComponent: GridFilterDateInput, + InputComponentProps: { showTime: true }, + })), valueFormatter: (params) => { if (typeof params.value === 'string') { return params.value; diff --git a/docs/data/migration/migration-data-grid-v6/migration-data-grid-v6.md b/docs/data/migration/migration-data-grid-v6/migration-data-grid-v6.md index f0c60e7c976a..bca3c434b9b1 100644 --- a/docs/data/migration/migration-data-grid-v6/migration-data-grid-v6.md +++ b/docs/data/migration/migration-data-grid-v6/migration-data-grid-v6.md @@ -229,6 +229,9 @@ Below are described the steps you need to make to migrate from v6 to v7. | `unstable_gridTabIndexColumnHeaderFilterSelector` | `gridTabIndexColumnHeaderFilterSelector` | - The filter panel no longer uses the native version of the [`Select`](https://mui.com/material-ui/react-select/) component for all components. +- The `filterModel` now supports `Date` objects as values for `date` and `dateTime` column types. + The `filterModel` still accepts strings as values for `date` and `dateTime` column types, + but all updates to the `filterModel` coming from the UI (e.g. filter panel) will set the value as a `Date` object.