Skip to content

Commit dcdc59e

Browse files
authored
[docs] Refactor Data Grid with Date Pickers example (mui#15992)
1 parent d8dbace commit dcdc59e

File tree

3 files changed

+41
-64
lines changed

3 files changed

+41
-64
lines changed

docs/data/data-grid/custom-columns/EditingWithDatePickers.js

+18-29
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
1515
import { DatePicker } from '@mui/x-date-pickers/DatePicker';
1616
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
1717
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
18-
import InputBase from '@mui/material/InputBase';
1918
import { enUS as locale } from 'date-fns/locale';
20-
import { styled } from '@mui/material/styles';
21-
22-
const dateAdapter = new AdapterDateFns({ locale });
19+
import format from 'date-fns/format';
2320

2421
/**
2522
* `date` column
@@ -38,22 +35,12 @@ const dateColumnType = {
3835
})),
3936
valueFormatter: (value) => {
4037
if (value) {
41-
return dateAdapter.format(value, 'keyboardDate');
38+
return format(value, 'MM/dd/yyyy', { locale });
4239
}
4340
return '';
4441
},
4542
};
4643

47-
const GridEditDateInput = styled(InputBase)({
48-
fontSize: 'inherit',
49-
padding: '0 9px',
50-
});
51-
52-
function WrappedGridEditDateInput(props) {
53-
const { InputProps, focused, ...other } = props;
54-
return <GridEditDateInput fullWidth {...InputProps} {...other} />;
55-
}
56-
5744
function GridEditDateCell({ id, field, value, colDef }) {
5845
const apiRef = useGridApiContext();
5946

@@ -68,7 +55,20 @@ function GridEditDateCell({ id, field, value, colDef }) {
6855
value={value}
6956
autoFocus
7057
onChange={handleChange}
71-
slots={{ textField: WrappedGridEditDateInput }}
58+
slotProps={{
59+
textField: {
60+
variant: 'standard',
61+
fullWidth: true,
62+
sx: {
63+
padding: '0 9px',
64+
justifyContent: 'center',
65+
},
66+
InputProps: {
67+
disableUnderline: true,
68+
sx: { fontSize: 'inherit' },
69+
},
70+
},
71+
}}
7272
/>
7373
);
7474
}
@@ -87,18 +87,7 @@ function GridFilterDateInput(props) {
8787
value={item.value ? new Date(item.value) : null}
8888
autoFocus
8989
label={apiRef.current.getLocaleText('filterPanelInputLabel')}
90-
slotProps={{
91-
textField: {
92-
variant: 'standard',
93-
},
94-
inputAdornment: {
95-
sx: {
96-
'& .MuiButtonBase-root': {
97-
marginRight: -1,
98-
},
99-
},
100-
},
101-
}}
90+
slotProps={{ textField: { size: 'small' } }}
10291
onChange={handleFilterChange}
10392
/>
10493
);
@@ -121,7 +110,7 @@ const dateTimeColumnType = {
121110
})),
122111
valueFormatter: (value) => {
123112
if (value) {
124-
return dateAdapter.format(value, 'keyboardDateTime');
113+
return format(value, 'MM/dd/yyyy hh:mm a', { locale });
125114
}
126115
return '';
127116
},

docs/data/data-grid/custom-columns/EditingWithDatePickers.tsx

+18-32
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,8 @@ import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
2020
import { DatePicker } from '@mui/x-date-pickers/DatePicker';
2121
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
2222
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
23-
import InputBase, { InputBaseProps } from '@mui/material/InputBase';
2423
import { enUS as locale } from 'date-fns/locale';
25-
import { styled } from '@mui/material/styles';
26-
import { TextFieldProps } from '@mui/material/TextField';
27-
28-
const dateAdapter = new AdapterDateFns({ locale });
24+
import format from 'date-fns/format';
2925

3026
/**
3127
* `date` column
@@ -44,24 +40,12 @@ const dateColumnType: GridColTypeDef<Date, string> = {
4440
})),
4541
valueFormatter: (value) => {
4642
if (value) {
47-
return dateAdapter.format(value, 'keyboardDate');
43+
return format(value, 'MM/dd/yyyy', { locale });
4844
}
4945
return '';
5046
},
5147
};
5248

53-
const GridEditDateInput = styled(InputBase)({
54-
fontSize: 'inherit',
55-
padding: '0 9px',
56-
});
57-
58-
function WrappedGridEditDateInput(props: TextFieldProps) {
59-
const { InputProps, focused, ...other } = props;
60-
return (
61-
<GridEditDateInput fullWidth {...InputProps} {...(other as InputBaseProps)} />
62-
);
63-
}
64-
6549
function GridEditDateCell({
6650
id,
6751
field,
@@ -81,7 +65,20 @@ function GridEditDateCell({
8165
value={value}
8266
autoFocus
8367
onChange={handleChange}
84-
slots={{ textField: WrappedGridEditDateInput }}
68+
slotProps={{
69+
textField: {
70+
variant: 'standard',
71+
fullWidth: true,
72+
sx: {
73+
padding: '0 9px',
74+
justifyContent: 'center',
75+
},
76+
InputProps: {
77+
disableUnderline: true,
78+
sx: { fontSize: 'inherit' },
79+
},
80+
},
81+
}}
8582
/>
8683
);
8784
}
@@ -102,18 +99,7 @@ function GridFilterDateInput(
10299
value={item.value ? new Date(item.value) : null}
103100
autoFocus
104101
label={apiRef.current.getLocaleText('filterPanelInputLabel')}
105-
slotProps={{
106-
textField: {
107-
variant: 'standard',
108-
},
109-
inputAdornment: {
110-
sx: {
111-
'& .MuiButtonBase-root': {
112-
marginRight: -1,
113-
},
114-
},
115-
},
116-
}}
102+
slotProps={{ textField: { size: 'small' } }}
117103
onChange={handleFilterChange}
118104
/>
119105
);
@@ -136,7 +122,7 @@ const dateTimeColumnType: GridColTypeDef<Date, string> = {
136122
})),
137123
valueFormatter: (value) => {
138124
if (value) {
139-
return dateAdapter.format(value, 'keyboardDateTime');
125+
return format(value, 'MM/dd/yyyy hh:mm a', { locale });
140126
}
141127
return '';
142128
},

packages/x-date-pickers/src/PickersTextField/PickersTextField.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,11 @@ const PickersTextField = React.forwardRef(function PickersTextField(
161161
ownerState={ownerState}
162162
{...other}
163163
>
164-
<InputLabel htmlFor={id} id={inputLabelId} {...InputLabelProps}>
165-
{label}
166-
</InputLabel>
164+
{label != null && label !== '' && (
165+
<InputLabel htmlFor={id} id={inputLabelId} {...InputLabelProps}>
166+
{label}
167+
</InputLabel>
168+
)}
167169
<PickersInputComponent
168170
elements={elements}
169171
areAllSectionsEmpty={areAllSectionsEmpty}

0 commit comments

Comments
 (0)