-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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] Support data attributes #8845
Conversation
Netlify deploy previewNetlify deploy preview: https://deploy-preview-8845--material-ui-x.netlify.app/ Updated pagesNo updates. These are the results for the performance tests:
|
it('should accept data attributes props', () => { | ||
render( | ||
<div style={{ width: 300, height: 500 }}> | ||
<DataGrid | ||
rows={[{ name: 'Bob' }]} | ||
columns={[{ field: 'name' }]} | ||
data-custom-id="grid-1" | ||
/> | ||
</div>, | ||
); | ||
|
||
const grid = document.querySelector('[data-custom-id="grid-1"]') | ||
|
||
expect(grid).not.to.equal(null); | ||
}); |
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've added a test for this in the community version, but technically speaking the same test should be added in the pro & premium packages to cover the feature fully. But that wouldn't be very DRY. Suggestions welcome.
packages/grid/x-data-grid-premium/src/DataGridPremium/DataGridPremium.tsx
Outdated
Show resolved
Hide resolved
The changes here are going to conflict with #8942. |
This is ready for review. There is a CI failure, but it seems unrelated to this PR. |
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'm not sure if we're in the right direction. The MUI components, by convention, spread the remaining props to the root element, we don't do that. If we go with the approach implemented here we still won't support passing an id
value or using other attributes, e.g. hidden
. I think we should enhance the DataGridProps
interface to also allow any DIV attribute, then pass the attributes we don't use to the root element.
IIUC, you're saying we should do something like this? |
Yes, take all props, filter out those that are used internally (e.g. |
I've looked around and using the How about we define the list of HTML props (eg. |
Ping @m4theushw, any opinion on the comment above? |
I'm not a big fan of creating a list of HTML attributes to forward. The MUI components have this convention of forwarding the rest and I think it would be nice if we supported it too. I'm more on the side of having a list of DataGrid attributes to not forward. If it's not possible to enforce via TS that every prop must be filtered out from the rest then I think we need to accept it and be cautious when adding new props. Maybe a ESLint could be built to check this but it might be overkill. Don't take my opinion as the only approach to follow, I'm open for the opinions from other members. |
I prefer the solution currently implemented in this PR because it has a good complexity/maintainability cost vs benefit. We have received requests to support |
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.
The trade-off looks good to me 👍
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.
Looks good to me.
Should we use the groupDataProps
middleware to also accommodate aria-*
props? (Rename it to groupCustomProps
or something like that.)
Currently we hard code aria-label
and aria-labelledby
but probably it's an opportunity to make it generic too, as more aria-* props like aria-description
could be passed.
Example pain point: #9075
Makes sense to deal with |
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.
otherwise our linting/testing fails
Since we now support aria-* attributes by default, would doing something like this resolve the linting errors you mentioned (and possibly add a little bit better IDE auto-completion, for aria-* attributes)?
diff --git a/packages/grid/x-data-grid/src/models/props/DataGridProps.ts b/packages/grid/x-data-grid/src/models/props/DataGridProps.ts
index 006af94b0..2936f2233 100644
--- a/packages/grid/x-data-grid/src/models/props/DataGridProps.ts
+++ b/packages/grid/x-data-grid/src/models/props/DataGridProps.ts
@@ -116,7 +116,7 @@ export interface DataGridPropsWithComplexDefaultValueBeforeProcessing {
* The controlled model do not have a default value at the prop processing level, so they must be defined in `DataGridOtherProps`
* TODO: add multiSortKey
*/
-export interface DataGridPropsWithDefaultValues {
+export interface DataGridPropsWithDefaultValues extends React.AriaAttributes {
/**
* If `true`, the grid height is dynamic and follow the number of rows in the grid.
* @default false
@@ -677,14 +677,6 @@ export interface DataGridPropsWithoutDefaultValue<R extends GridValidRowModel =
* @param {GridCallbackDetails} details Additional details for this callback.
*/
onSortModelChange?: (model: GridSortModel, details: GridCallbackDetails) => void;
- /**
- * The label of the grid.
- */
- 'aria-label'?: string;
- /**
- * The id of the element containing a label for the grid.
- */
- 'aria-labelledby'?: string;
/**
* Set of columns of type [[GridColDef[]]].
*/
* Forwarded props for the grid root element. | ||
* @ignore - do not document. | ||
*/ | ||
forwardedProps?: Record<string, unknown>; |
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.
Shouldn't this be in DataGridPropsWithoutDefaultValue
?
Just a side note: I am amazed this is not actually possible (context), but it'd have be great to only allow This probably supports @m4theushw's argument of destructuring I am not sure though how practical is it to go in that direction right now. |
React.AriaAttributesI considered using it but it didn't like the solution, because it contains props such as DestructuringI'm not sure I see how it would help with the issues here. I don't think we can get a perfect (typed) solution here. |
There is a failing test but it seems unrelated to this PR. If no one else has comments, I'll merge as it is before the release. |
Closes #8605
This PR adds support for custom data attributes on the grid root element, which is useful for automation, testing & some external libraries.