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

[data grid] Disable submit button if DataGrid is in edit mode #14827

Closed
alejandrolm96 opened this issue Oct 4, 2024 · 10 comments
Closed

[data grid] Disable submit button if DataGrid is in edit mode #14827

alejandrolm96 opened this issue Oct 4, 2024 · 10 comments
Labels
component: data grid This is the name of the generic UI component, not the React module! customization: logic Logic customizability feature: Editing Related to the data grid Editing feature status: waiting for author Issue with insufficient information support: docs-feedback Feedback from documentation page

Comments

@alejandrolm96
Copy link

alejandrolm96 commented Oct 4, 2024

Related page

https://mui.com/material-ui/

Kind of issue

Unclear explanations

Issue description

Disabling a Button During DataGrid "Edit" Mode

Context

Hi, not sure if I'm posting this in the right section.

I’m working with a DataGrid component in ReactJS and have a "Submit" button below the table for submitting data. I would like to disable this button whenever the DataGrid is in "Edit" mode. Specifically:

  • When a user clicks on a row to edit it, the "Submit" button should be disabled.
  • Once the user exits the "Edit" mode (clicking away the table), the "Submit" button should be re-enabled.
  • The "Submit" button is not part of the DataGrid.

Example:

image001

I have read the documentation but I could not find any possible solution for this.

Search keywords: datagrid

Search keywords:

@alejandrolm96 alejandrolm96 added status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: docs-feedback Feedback from documentation page labels Oct 4, 2024
@zannager zannager added the component: data grid This is the name of the generic UI component, not the React module! label Oct 4, 2024
@zannager zannager transferred this issue from mui/material-ui Oct 4, 2024
@michelengelen michelengelen changed the title [docs] DataGrid - Disable submit button is DataGrid is in edit mode / ReactJS [data grid] Disable submit button is DataGrid is in edit mode Oct 7, 2024
@michelengelen michelengelen added status: waiting for author Issue with insufficient information feature: Editing Related to the data grid Editing feature customization: logic Logic customizability and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Oct 7, 2024
@michelengelen
Copy link
Member

Hey @alejandrolm96 ... this can be done using the event callbacks we provide as props:

  • onCellEditStart / onCellEditStop
  • onRowEditStart / onRowEditStop

Here is an example of it:

export default function DisableStopEditModeOnFocusOut() {
  const [isEditing, setIsEditing] = React.useState(false);
  const toggleIsEditing = () => {
    setIsEditing((prev) => !prev);
  };
  return (
    <>
      <div style={{ height: 300, width: '100%' }}>
        <DataGrid
          rows={rows}
          columns={columns}
          onCellEditStop={toggleIsEditing}
          onCellEditStart={toggleIsEditing}
        />
      </div>
      <Button disabled={isEditing}>Submit</Button>
    </>
  );
}

@alejandrolm96 alejandrolm96 changed the title [data grid] Disable submit button is DataGrid is in edit mode [data grid] Disable submit button if DataGrid is in edit mode Oct 7, 2024
@alejandrolm96
Copy link
Author

alejandrolm96 commented Oct 7, 2024

@michelengelen

Thanks for your reply,
I will try it, Thanks!

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Oct 7, 2024
@michelengelen michelengelen added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Oct 7, 2024
Copy link

The issue has been inactive for 7 days and has been automatically closed.

@alejandrolm96
Copy link
Author

Hi, how can I re-open this issue?

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Oct 21, 2024
@github-actions github-actions bot reopened this Oct 21, 2024
@alejandrolm96
Copy link
Author

@michelengelen
Hi,

I tested what you mentioned above, and it successfully disables the button. However, I'm not sure how to handle errors properly.

For example, I have a constant variable called columns, and one of the columns uses preProcessEditCellProps to manage errors for a specific field. The issue is that when an error occurs, the button remains enabled.

Additionally, if there is an error in that field, clicking away from the cell does not exit edit mode; the cell remains in edit mode.

const columns: GridColDef[] = [
... other columns
{
      flex: 1,
      align: 'right',
      headerAlign: 'right',
      editable: true,
      type: 'number',
      cellClassName: (params) => {
        const { value, waterQualityTypeId } = params.row;

        const hasError = hasValueError(value, waterQualityTypeId);
        
        return hasError ? 'cell--warning' : '';
      },
      preProcessEditCellProps: (params: GridPreProcessEditCellProps) => {
        const { value } = params.props;
        const waterQualityTypeId = params.row.waterQualityTypeId;
        const error = hasValueError(value, waterQualityTypeId);

        return { ...params.props, error };
      },
    },
  ] as GridColDef[];

Example video:

example.video.mov

As you can see, the error occurs because I have a validation that requires the number to be less than or equal to 20. The goal is to catch this error and disable the button when the validation fails.

@michelengelen
Copy link
Member

Hey @alejandrolm96 ... the cell not exiting the edit mode is expected when it got validated with an error. Not sure if we can opt out of that ... @arminmeh?

The other part is a bit harder to do.
Your best (but probably perf wise worst) shot would be to introduce a new state (hasError) and set it every time you call preProcessEditCellProps. Example:

// e.g. on `colDef.type === 'number'`
preProcessEditCellProps: (params: GridPreProcessEditCellProps) => {
  const { value } = params.props;
  const error = value < 10;

  setHasError(error);

  return { ...params.props, error };
},

With this you can then update your toggleIsEditing function:

const toggleIsEditing = () => {
  setDisabledButton((prev) => hasError || !prev);
};

@michelengelen
Copy link
Member

Ideal would be if we could somehow get the current state of the cell in edit mode from the grid and determine it that way ... can we support something like this @arminmeh?

@michelengelen
Copy link
Member

@MBilalShafi do you have any idea how we could implement something like this?

@arminmeh
Copy link
Contributor

the cell not exiting the edit mode is expected when it got validated with an error. Not sure if we can opt out of that

if there is an error, edit state will not be updated (mentioned here as well)

since the requirement is that the submit button changes the state while user is changing the value, I would suggest to customize the edit component and add onChange listeners which will update the error state that will be used by the submit button

would this work @alejandrolm96

@arminmeh arminmeh added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Oct 24, 2024
Copy link

The issue has been inactive for 7 days and has been automatically closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module! customization: logic Logic customizability feature: Editing Related to the data grid Editing feature status: waiting for author Issue with insufficient information support: docs-feedback Feedback from documentation page
Projects
None yet
Development

No branches or pull requests

4 participants