From dcdc59e7d045077ee6582f0aa64e8db84f21bb31 Mon Sep 17 00:00:00 2001 From: Lukas Tyla Date: Fri, 27 Dec 2024 15:07:37 +0200 Subject: [PATCH] [docs] Refactor Data Grid with Date Pickers example (#15992) --- .../custom-columns/EditingWithDatePickers.js | 47 +++++++---------- .../custom-columns/EditingWithDatePickers.tsx | 50 +++++++------------ .../src/PickersTextField/PickersTextField.tsx | 8 +-- 3 files changed, 41 insertions(+), 64 deletions(-) diff --git a/docs/data/data-grid/custom-columns/EditingWithDatePickers.js b/docs/data/data-grid/custom-columns/EditingWithDatePickers.js index 3418d179a1c7..22d625bd568c 100644 --- a/docs/data/data-grid/custom-columns/EditingWithDatePickers.js +++ b/docs/data/data-grid/custom-columns/EditingWithDatePickers.js @@ -15,11 +15,8 @@ import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; import { DatePicker } from '@mui/x-date-pickers/DatePicker'; import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker'; import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns'; -import InputBase from '@mui/material/InputBase'; import { enUS as locale } from 'date-fns/locale'; -import { styled } from '@mui/material/styles'; - -const dateAdapter = new AdapterDateFns({ locale }); +import format from 'date-fns/format'; /** * `date` column @@ -38,22 +35,12 @@ const dateColumnType = { })), valueFormatter: (value) => { if (value) { - return dateAdapter.format(value, 'keyboardDate'); + return format(value, 'MM/dd/yyyy', { locale }); } return ''; }, }; -const GridEditDateInput = styled(InputBase)({ - fontSize: 'inherit', - padding: '0 9px', -}); - -function WrappedGridEditDateInput(props) { - const { InputProps, focused, ...other } = props; - return ; -} - function GridEditDateCell({ id, field, value, colDef }) { const apiRef = useGridApiContext(); @@ -68,7 +55,20 @@ function GridEditDateCell({ id, field, value, colDef }) { value={value} autoFocus onChange={handleChange} - slots={{ textField: WrappedGridEditDateInput }} + slotProps={{ + textField: { + variant: 'standard', + fullWidth: true, + sx: { + padding: '0 9px', + justifyContent: 'center', + }, + InputProps: { + disableUnderline: true, + sx: { fontSize: 'inherit' }, + }, + }, + }} /> ); } @@ -87,18 +87,7 @@ function GridFilterDateInput(props) { value={item.value ? new Date(item.value) : null} autoFocus label={apiRef.current.getLocaleText('filterPanelInputLabel')} - slotProps={{ - textField: { - variant: 'standard', - }, - inputAdornment: { - sx: { - '& .MuiButtonBase-root': { - marginRight: -1, - }, - }, - }, - }} + slotProps={{ textField: { size: 'small' } }} onChange={handleFilterChange} /> ); @@ -121,7 +110,7 @@ const dateTimeColumnType = { })), valueFormatter: (value) => { if (value) { - return dateAdapter.format(value, 'keyboardDateTime'); + return format(value, 'MM/dd/yyyy hh:mm a', { locale }); } return ''; }, diff --git a/docs/data/data-grid/custom-columns/EditingWithDatePickers.tsx b/docs/data/data-grid/custom-columns/EditingWithDatePickers.tsx index a381b48b922b..92d5206e7ae5 100644 --- a/docs/data/data-grid/custom-columns/EditingWithDatePickers.tsx +++ b/docs/data/data-grid/custom-columns/EditingWithDatePickers.tsx @@ -20,12 +20,8 @@ import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; import { DatePicker } from '@mui/x-date-pickers/DatePicker'; import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker'; import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns'; -import InputBase, { InputBaseProps } from '@mui/material/InputBase'; import { enUS as locale } from 'date-fns/locale'; -import { styled } from '@mui/material/styles'; -import { TextFieldProps } from '@mui/material/TextField'; - -const dateAdapter = new AdapterDateFns({ locale }); +import format from 'date-fns/format'; /** * `date` column @@ -44,24 +40,12 @@ const dateColumnType: GridColTypeDef = { })), valueFormatter: (value) => { if (value) { - return dateAdapter.format(value, 'keyboardDate'); + return format(value, 'MM/dd/yyyy', { locale }); } return ''; }, }; -const GridEditDateInput = styled(InputBase)({ - fontSize: 'inherit', - padding: '0 9px', -}); - -function WrappedGridEditDateInput(props: TextFieldProps) { - const { InputProps, focused, ...other } = props; - return ( - - ); -} - function GridEditDateCell({ id, field, @@ -81,7 +65,20 @@ function GridEditDateCell({ value={value} autoFocus onChange={handleChange} - slots={{ textField: WrappedGridEditDateInput }} + slotProps={{ + textField: { + variant: 'standard', + fullWidth: true, + sx: { + padding: '0 9px', + justifyContent: 'center', + }, + InputProps: { + disableUnderline: true, + sx: { fontSize: 'inherit' }, + }, + }, + }} /> ); } @@ -102,18 +99,7 @@ function GridFilterDateInput( value={item.value ? new Date(item.value) : null} autoFocus label={apiRef.current.getLocaleText('filterPanelInputLabel')} - slotProps={{ - textField: { - variant: 'standard', - }, - inputAdornment: { - sx: { - '& .MuiButtonBase-root': { - marginRight: -1, - }, - }, - }, - }} + slotProps={{ textField: { size: 'small' } }} onChange={handleFilterChange} /> ); @@ -136,7 +122,7 @@ const dateTimeColumnType: GridColTypeDef = { })), valueFormatter: (value) => { if (value) { - return dateAdapter.format(value, 'keyboardDateTime'); + return format(value, 'MM/dd/yyyy hh:mm a', { locale }); } return ''; }, diff --git a/packages/x-date-pickers/src/PickersTextField/PickersTextField.tsx b/packages/x-date-pickers/src/PickersTextField/PickersTextField.tsx index daccf5ec262b..fb67d7e37883 100644 --- a/packages/x-date-pickers/src/PickersTextField/PickersTextField.tsx +++ b/packages/x-date-pickers/src/PickersTextField/PickersTextField.tsx @@ -161,9 +161,11 @@ const PickersTextField = React.forwardRef(function PickersTextField( ownerState={ownerState} {...other} > - - {label} - + {label != null && label !== '' && ( + + {label} + + )}