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

[DataGrid] Standardize/Reconcile all event handlers and params #1301

Closed
dtassone opened this issue Mar 26, 2021 · 1 comment · Fixed by #2312
Closed

[DataGrid] Standardize/Reconcile all event handlers and params #1301

dtassone opened this issue Mar 26, 2021 · 1 comment · Fixed by #2312
Assignees
Labels
breaking change component: data grid This is the name of the generic UI component, not the React module! discussion new feature New feature or request
Milestone

Comments

@dtassone
Copy link
Member

dtassone commented Mar 26, 2021

  1. Events should always be fired with the params of the entity it comes from, or with the params relative to its action
    ie onCellClick has GridCellParams
    And onFilterModelChange has GridFilterModelParams

Getting params, can be done using apiRef

  • apiRef.current.getCellParams(id: GridRowId, field: string): GridCellParams;
  • We should add apiRef.current.getColumnHeaderParams(field: string): GridColumnHeaderParams;

Then it should have the event as a second parameter.

  • This is because event is optional in some cases
    ie options.onColumnReorder(params: GridColumnParams, event?: React.SyntheticEvent);
// in hook
setColumnIndex = (field, newIndex, event?) => {
   apiRef.publishEvent(GRID_COLUMN_REORDER, apiRef.current.getColumnHeaderParams(field), event)
}

// in Api
setColumnIndex: (field, newIndex)=> void
  1. Events should be granular so it's easily catchable by our users.

Imagine I want to log the cell that users are trying to edit. I could subscribe to the double click event but there are many different ways of getting into edit mode, keyboard, calling the api directly, and one could do it on click...
With granular events, I can easily hook into the event and achieve the above.

export function EditCellWithMessageGrid() {
  const apiRef = useGridApiRef();
  const [message, setMessage] = React.useState('');

  React.useEffect(()=> {
    return apiRef.current.subscribeEvent('cellEnterEdit', (param: GridCellParams, event?: React.SyntheticEvent)=> {
      LogUser(`user ${username} is editing `, params) 
      setMessage(`Editing cell with value: ${param.value} at row: ${param.rowIndex}, column: ${param.field},
                        triggered by ${event!.type}
      `);
    });
  }, [apiRef])

  React.useEffect(()=> {
    return apiRef.current.subscribeEvent('cellExitEdit', ()=> {
      LogUser(`user ${username} stopped editing `, params) 
      setMessage('');
    });
  }, [apiRef])

  return (
    <div style={{ height: 400, width: '100%' }}>
      <XGrid
        {...baselineEditProps}
        apiRef={apiRef}
        autoHeight
      />
      { message && <Alert severity="info">{message}</Alert> }
    </div>
  );
}
@oliviertassinari
Copy link
Member

Closing for now, we have fixed 80% of the pain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change component: data grid This is the name of the generic UI component, not the React module! discussion new feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants