-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
[utils] Add opt-in DataAttributesOverrides augmentation for slot props
#48554
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
Open
LukasTy
wants to merge
1
commit into
mui:master
Choose a base branch
from
LukasTy:claude/data-attributes-overrides
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
packages/mui-material/test/typescript/moduleAugmentation/dataAttributesOverrides.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import * as React from 'react'; | ||
| import Backdrop from '@mui/material/Backdrop'; | ||
|
|
||
| // Augment the shared `DataAttributesOverrides` interface to opt in to typed | ||
| // support for `data-testid` on every MUI slot prop. The augmentation flows | ||
| // through `SlotComponentProps` / `SlotComponentPropsWithSlotState` in | ||
| // `@mui/utils/types`, and via `SlotProps` in `@mui/material`, to every slot | ||
| // of every Material component that wires slot props through these helpers. | ||
| declare module '@mui/utils/types' { | ||
| interface DataAttributesOverrides { | ||
| 'data-testid'?: string; | ||
| } | ||
| } | ||
|
|
||
| // After augmentation: `data-testid` is assignable on any Material slot prop. | ||
| <Backdrop | ||
| open | ||
| slotProps={{ | ||
| root: { | ||
| 'data-testid': 'backdrop-root', | ||
| }, | ||
| }} | ||
| />; |
4 changes: 4 additions & 0 deletions
4
...ges/mui-material/test/typescript/moduleAugmentation/dataAttributesOverrides.tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "extends": "../../../../../tsconfig.json", | ||
| "files": ["dataAttributesOverrides.spec.tsx"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /** | ||
| * Module-augmentable interface that lets consumers opt in to typed support for | ||
| * `data-*` (and any other) attributes on MUI slot props. Empty by default — | ||
| * by design, MUI slot prop types do not include arbitrary `data-*` keys; the | ||
| * augmentation is the single switch consumers can flip to choose their level | ||
| * of strictness. | ||
| * | ||
| * Examples: | ||
| * | ||
| * // Strongly-typed: only `data-testid` becomes assignable on slots. | ||
| * declare module '@mui/utils/types' { | ||
| * interface DataAttributesOverrides { | ||
| * 'data-testid'?: string; | ||
| * } | ||
| * } | ||
| * | ||
| * // Loose: accept any `data-*` key on slots. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could the spec cover this as well? |
||
| * declare module '@mui/utils/types' { | ||
| * interface DataAttributesOverrides { | ||
| * [k: `data-${string}`]: string | number | boolean | undefined; | ||
| * } | ||
| * } | ||
| */ | ||
| export interface DataAttributesOverrides {} | ||
|
|
||
| /** | ||
| * Surface contributed to slot prop types by the `DataAttributesOverrides` | ||
| * augmentation. Empty by default; populated only when a consumer declares | ||
| * `data-*` keys via module augmentation. This is what `WithDataAttributes` | ||
| * intersects into the widened branch of every slot prop union exposed by | ||
| * `@mui/utils/types`. | ||
| */ | ||
| export type DataAttributes = DataAttributesOverrides; | ||
|
|
||
| /** | ||
| * Widens a slot-props type so that, when a consumer augments | ||
| * `DataAttributesOverrides`, the augmented keys become assignable to the | ||
| * widened branch. The default `DataAttributes` is empty, so this widening is | ||
| * a no-op until a consumer opts in. | ||
| * | ||
| * Implemented as a union between the original type and the intersected widened | ||
| * form — `T | (T & DataAttributes)` — so that pre-typed values remain | ||
| * assignable to the original branch without having to declare a `data-*` | ||
| * index signature themselves, while object literals can pick up the widened | ||
| * branch and include the augmented keys. | ||
| */ | ||
| export type WithDataAttributes<T> = T | (T & DataAttributes); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: could use JSdoc
@example