-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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] Pass slot props to columnHeaders
slot
#12768
[DataGrid] Pass slot props to columnHeaders
slot
#12768
Conversation
Deploy preview: https://deploy-preview-12768--material-ui-x.netlify.app/ |
@@ -75,6 +75,7 @@ function GridHeaders() { | |||
columnVisibility={columnVisibility} | |||
columnGroupsHeaderStructure={columnGroupsHeaderStructure} | |||
hasOtherElementInTabSequence={hasOtherElementInTabSequence} | |||
{...rootProps.slotProps?.columnHeaders} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We tend to forget to pass slot props.
Is this a problem we want to solve in a general way?
A few ideas:
-
Slot
component:<Slot slotName="columnHeaders" ref={columnsContainerRef} visibleColumns={visibleColumns} // ... /> function Slot<TSlotName extends keyof GridSlotProps & keyof GridSlotsComponent>({ slotName, ...props }: GridSlotProps[TSlotName] & { slotName: TSlotName }) { const rootProps = useGridRootProps(); const Component = rootProps.slots[slotName] as React.ElementType; return <Component {...props} {...rootProps.slotProps?.[slotName]} />; }
-
useSlot
hook:const [Slot, slotProps] = useSlot('columnHeaders'); // + ESLint rule to enforce the usage of `slotProps` <Slot slotName="columnHeaders" ref={columnsContainerRef} visibleColumns={visibleColumns} {...slotProps} /> const useSlot = <TSlot extends keyof GridSlotProps & keyof GridSlotsComponent>(slot: TSlot) => { const rootProps = useGridRootProps(); const Component = rootProps.slots[slot]; const slotProps = rootProps.slotProps?.[slot]; return [Component, slotProps] as const; };
cc @mui/xgrid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we can make an eslint rule for 2, we can probably make one to just enfore it with <rootProps.slots.x {...rootProps.slotProps.x} />
. I would prefer that. After that option 2. After that option 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the pickers and tree view we are always using useSlotProps
(except on one instance of a transition component where it's not really doable).
That way we always have the same DX and we didn't forget to pass any slot props for a very long time.
This is a hook from the core team so it also improve the consistency of the codebase and allow to support callback version of the slotProps (which you don't from what I can see).
Is it also worth exporting |
@trbagley-gresham You can use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume the suggested improvement of the larger problem will be handled separately, right?
Fixes #12749