Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Fix EditingWithDatePickers demo #15967

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions docs/data/data-grid/custom-columns/EditingWithDatePickers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import InputBase from '@mui/material/InputBase';
import { enUS as locale } from 'date-fns/locale';
import { styled } from '@mui/material/styles';

import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';

const dateAdapter = new AdapterDateFns({ locale });

/**
Expand Down Expand Up @@ -54,21 +56,29 @@ function WrappedGridEditDateInput(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();
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
enableAccessibleFieldDOMStructure={false}
onChange={handleChange}
slots={{ textField: WrappedGridEditDateInput }}
slotProps={{ textField: { inputRef } }}
/>
);
}
Expand Down
12 changes: 11 additions & 1 deletion docs/data/data-grid/custom-columns/EditingWithDatePickers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ 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';
import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';

const dateAdapter = new AdapterDateFns({ locale });

Expand Down Expand Up @@ -67,21 +68,30 @@ function GridEditDateCell({
field,
value,
colDef,
hasFocus,
}: GridRenderEditCellParams<any, Date | null, string>) {
const apiRef = useGridApiContext();

const inputRef = React.useRef<HTMLInputElement>();
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
enableAccessibleFieldDOMStructure={false}
onChange={handleChange}
slots={{ textField: WrappedGridEditDateInput }}
slotProps={{ textField: { inputRef } }}
/>
);
}
Expand Down
Loading