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 Dec 31, 2024
1 parent d570c6d commit 390211c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 10 additions & 3 deletions docs/data/data-grid/custom-columns/EditingWithDatePickers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import { enUS as locale } from 'date-fns/locale';
import format from 'date-fns/format';

import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
/**
* `date` column
*/
Expand All @@ -41,22 +41,29 @@ const dateColumnType = {
},
};

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}
slotProps={{
textField: {
inputRef,
variant: 'standard',
fullWidth: true,
sx: {
Expand Down
12 changes: 10 additions & 2 deletions docs/data/data-grid/custom-columns/EditingWithDatePickers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import { enUS as locale } from 'date-fns/locale';
import format from 'date-fns/format';

import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
/**
* `date` column
*/
Expand Down Expand Up @@ -51,22 +51,30 @@ 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}
slotProps={{
textField: {
inputRef,
variant: 'standard',
fullWidth: true,
sx: {
Expand Down

0 comments on commit 390211c

Please sign in to comment.