2

When using renderEditCell in a column's definition, once the row is in edit mode, I face these two issues:

  1. None of the cells gets the focus
  2. Using the tab key on that cell has no effect: it should move to the next cell

If I comment out the renderEditCell property, the first issue remains (but I think I can live with it), while the second issue is resolved.

My problem is that I need to render a custom component to allow editing long texts.

I've reproduced the issue in this sandbox.

What I've tried so far in addition to googling (with no results):

  • Using the useGridApiContext and apiRef.current.setEditCellValue.
  • Forcing the tabIndex prop; only later, I figured I should not touch it because the field is already in a wrapper element with a tabIndex anyway.
  • Reading the docs too many times. The Accessibility page does not go in details when it comes to the "edit" mode.
Olivier Tassinari
  • 8,238
  • 4
  • 23
  • 23
Andrea Sciamanna
  • 1,404
  • 1
  • 15
  • 30
  • I am facing the same issue... Once I've migrated to v6, tab indexing in custom cells stopped working... To be honest, there is more then one issue with the v5->v6 migration that isn't documented properly and is just plainly wrong: setRowMode was replaced with startRowEditMode but migration points to startRowMode (missing "Edit" at the end)... – Filip Malek May 19 '23 at 08:03
  • Were you able to figure this out? I have custom renderEditCell components for each column... tabbing doesn't work at all (oddly, it does nothing). I've tried many things and can't get it going. I'm using x-data-grid 6.0. – friedmud Jun 11 '23 at 13:23

1 Answers1

0

https://github.com/mui/mui-x/issues/9406

The custom edit component must focus the input manually when hasFocus is true. This fixes your second issue.

const LongTextCellEdit: FC<LongTextCellEditProps> = ({
  params,
  textFieldProps
}) => {
  const apiRef = useGridApiContext();
  const { id, value: valueProp, field, error } = params;
  const [value, setValue] = React.useState(valueProp);
  const reference = React.useRef();

  useEffect(() => {
    setValue(valueProp);
  }, [valueProp]);

  useEffect(() => {
    if (params.hasFocus) {
      reference.current!.focus();
    }
  }, [params.hasFocus]);

  const handleValueChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
    const newValue = event.target.value; // The new value entered by the user
    apiRef.current.setEditCellValue({
      id,
      field,
      value: newValue,
      debounceMs: 200
    });

    setValue(newValue);
  };

  return (
    <Stack direction="column" width="100%" alignSelf="flex-start">
      <TextareaAutosize
        ref={reference}
        minRows={1}
        maxRows={10}
        {...textFieldProps}
        style={{ width: "100%" }}
        value={value}
        onChange={handleValueChange}
      />
      {!!error && <Alert color="error">{error}</Alert>}
    </Stack>
  );
};