-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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] Add typing to slot props #11687
Comments
The main downside of typing the Thinks like the following examples fire a TS error: const MyCustomToolbar = ({ shouldRenderToolbar, ...other }: GridToolbarProps & { shouldRenderToolbar: boolean }) => {
if (!shouldRenderToolbar) return null;
return <GridToolbar {...other} />
}
const App = () => {
const [shouldRenderToolbar, setShouldRenderToolbar] = React.useState(false);
return (
<React.Fragment>
<button onClick={() => setShouldRenderToolbar(prev => !prev)}>Toggle toolbar visibility</button>
<DataGrid slots={{ toolbar: MyCustomToolbar }} />
</React.Fragment>
);
} Same idea if you want to change the DOM element at the root of a slot. On the pickers, we are typing most of our slots, but I removed the typing from some slots like the On the core, as far as I know, the slots are never typed (I would have missed to exceptions). I don't know what the best solution for the grid is, but be careful to not just type all the slots super strictly without taking the downsides into account. |
For the custom components/props, that's why we have the props overrides, isn't it?: https://github.com/mui/mui-x/blob/next/packages/grid/x-data-grid/src/models/gridSlotsComponentsProps.ts So IIUC users can do something like this: declare module '@mui/x-data-grid' {
interface ToolbarPropsOverrides {
something: number;
}
} |
Yes it's the goal of these overrides, but they come with two major limitations:
|
Wouldn't it make sense to recommend to users to cast their components upon use instead? const App = () => {
return (
<React.Fragment>
<DataGrid slots={{ toolbar: MyCustomToolbar as DataGridProps['slots']['toolbar'] }} />
</React.Fragment>
);
} |
It can be a solution yes, but you are loosing a lot of TS capacity if you have to do it. Honestly, I'm not sure that typing the slots too strictly is the right approach, and I would be curious to know exactly the core team never type them. |
But right now we are skipping TS checking altogether because we type as Besides, if users want convenience and opt-out of safety, they can also simply use |
@romgrk |
I think we should recommend doing this: // Internal code:
type Component<T> = (props: T) => {}
type CellProps = { id: string }
type CellSlot = Component<CellProps>
type Slots = {
cell: CellSlot,
}
// User code:
function CustomCell(props: CellProps & { custom: number }) {
return {}
}
const slots: Slots = {
cell: CustomCell as CellSlot,
} |
Another strong argument to add typings is that with our upcoming objective to use the grid without MUI bundling, we're bound to use the slots more extensively and increase the number of them. It's going to be much more safe to add new design systems if we can count on the typings to help both the implementation of adding new design systems and ensuring breaking changes don't break things silently. |
Agree, I experienced this problem in #8067 (comment) |
is there some manual or doc for doing these customizations ? I have the pro but there is not a good documentation only for the basic things |
Currently all our slots are typed as
React.JSXElementConstructor<any>
. The downsides are:I propose that we add typing to all our slots before the v7 release.
@mui/xgrid 👍 / 👎 ?
Search keywords:
The text was updated successfully, but these errors were encountered: