Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material-UI Data-Grid: How pass parameter to custom toolbar?

I am defining a custom toolbar in datagrid (Material-UI) following (see: https://material-ui.com/components/data-grid/rendering/#toolbar) I started from this example: https://codesandbox.io/s/x58yv?file=/demo.js

I want to show or hide toolbar with transition. As we can't passing custom props to component:

   <DataGrid
    ...
    components={{ Toolbar: CustomToolbar }}
    ...
   </DataGrid>

So i have used context API like this:

   export function CustomToolbar(props: GridBaseComponentProps) {
    const classes = useToolbarStyles();
    const toolbarContext = useContext(ToolbarContext);
    return (
        <Collapse in={toolbarContext.toolbar}>
            <GridToolbarContainer className={classes.root}>
                <GridColumnsToolbarButton />
                <GridFilterToolbarButton />
                <GridDensitySelector />
                <GridToolbarExport />
            </GridToolbarContainer>
        </Collapse>
    );
   }

It works but is it any better solution ?

like image 431
Mr2dishes Avatar asked Mar 02 '21 20:03

Mr2dishes


1 Answers

You should be able to use the componentsProps prop of DataGrid to pass in any additional props (see https://material-ui.com/api/data-grid/), ex.

<DataGrid
    ...
    components={{ Toolbar: CustomToolbar }}
    componentsProps={{ toolbar: { myCustomProp: 8 } }}
    ...
</DataGrid>
like image 65
Alex Coleman Avatar answered Oct 21 '22 00:10

Alex Coleman