Skip to content

Commit

Permalink
[docs] Fix EditingWithDatePickers demo (#15967)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-rajat19 authored and web-flow committed Dec 31, 2024
1 parent 6071413 commit d488547
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
42 changes: 25 additions & 17 deletions docs/data/data-grid/custom-columns/EditingWithDatePickers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ 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';
import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
/**
* `date` column
*/
Expand All @@ -44,31 +42,41 @@ const dateColumnType = {
},
};

const GridEditDateInput = styled(InputBase)({
fontSize: 'inherit',
padding: '0 9px',
});

function WrappedGridEditDateInput(props) {
const { InputProps, focused, ...other } = props;
return <GridEditDateInput fullWidth {...InputProps} {...other} />;
}

function GridEditDateCell({ id, field, value, colDef }) {
function GridEditDateCell({ id, field, value, colDef, hasFocus }) {
const apiRef = useGridApiContext();

const inputRef = React.useRef(null);
const Component = colDef.type === 'dateTime' ? DateTimePicker : DatePicker;

const handleChange = (newValue) => {
apiRef.current.setEditCellValue({ id, field, value: newValue });
};

useEnhancedEffect(() => {
if (hasFocus) {
inputRef.current.focus();
}
}, [hasFocus]);

return (
<Component
value={value}
autoFocus
onChange={handleChange}
slots={{ textField: WrappedGridEditDateInput }}
slotProps={{
textField: {
inputRef,
variant: 'standard',
fullWidth: true,
sx: {
padding: '0 9px',
justifyContent: 'center',
},
InputProps: {
disableUnderline: true,
sx: { fontSize: 'inherit' },
},
},
}}
/>
);
}
Expand Down
32 changes: 25 additions & 7 deletions docs/data/data-grid/custom-columns/EditingWithDatePickers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ 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';
import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
/**
* `date` column
*/
Expand Down Expand Up @@ -67,21 +64,42 @@ function GridEditDateCell({
field,
value,
colDef,
hasFocus,
}: GridRenderEditCellParams<any, Date | null, string>) {
const apiRef = useGridApiContext();

const inputRef = React.useRef<HTMLInputElement>(null);
const Component = colDef.type === 'dateTime' ? DateTimePicker : DatePicker;

const handleChange = (newValue: unknown) => {
apiRef.current.setEditCellValue({ id, field, value: newValue });
};

useEnhancedEffect(() => {
if (hasFocus) {
inputRef.current!.focus();
}
}, [hasFocus]);

return (
<Component
value={value}
autoFocus
onChange={handleChange}
slots={{ textField: WrappedGridEditDateInput }}
slotProps={{
textField: {
inputRef,
variant: 'standard',
fullWidth: true,
sx: {
padding: '0 9px',
justifyContent: 'center',
},
InputProps: {
disableUnderline: true,
sx: { fontSize: 'inherit' },
},
},
}}
/>
);
}
Expand Down

0 comments on commit d488547

Please # to comment.