-
-
Notifications
You must be signed in to change notification settings - Fork 32.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
[Switch] inputProps can take additional props #18902
Conversation
No bundle size changes comparing 192b426...b725602 |
@oliviertassinari , here's sandbox for the issue that this PR is resolving: |
Is this related to #18874? It sounds like something we should have a systematic solution for, not a one case exception. |
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.
Thanks for working on this.
Could you add a test to showcase what this fixes? It's not obvious to me why we need this change.
@@ -21,6 +21,11 @@ export interface SwitchBaseProps | |||
value?: unknown; | |||
} | |||
|
|||
export interface SwitchBaseComponentProps extends React.HTMLAttributes<HTMLInputElement> { | |||
// Accommodate arbitrary additional props coming from the `inputProps` prop | |||
[arbitrary: string]: any; |
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 problem is that this will not catch excess props that are actually wrong (such as those from typos or accidental wrong names.
The codesandbox shows the following pattern: <Switch
checked={checked}
onChange={toggleChecked}
inputProps={{ "data-testid": "test" }}
/> The pull request seems to apply the same solution to the SwitchBase as we use with the InputBase: @eps1lon On one hand, it would be more consistent, but on the other hand, we have microsoft/TypeScript#28960 and the fact that we encourage custom input component implementation that we don't with the switch, radio, and checkbox. What would be best? |
I need to think about this more. I think I'd rather encourage explicit |
@eps1lon @oliviertassinari @Natalia504 What do you think of adding something like this to the index.d.ts:
Then for instance inputProps could be changed to |
@afholderman I believe the InputBase case is a bit different to the SwitchBase, it's not uncommon to use a different implementation of the |
Maybe we should give up on the changes, and ask developers to manually cast? |
@oliviertassinari passing data attributes down is a supported use case by React so we should try to find a solution for this issue. Especially with growing adoption of testing-library as a hooks compatible replacement for enzyme which supports the use of |
@afholderman Why using a test-id over the accessible feature of the element (name, label, etc)? |
It has been a month. I believe we can close the pull request with #18902 (comment) as a recommended approach :). |
Just ran into this issue. And even from the suggestions here I am not exactly sure how to get around it. Do you have an example with the casting explicit My current work around by applying the component: <Switch
data-testid="my-test-id"
/> test: clickSwitch: () => {
const wrapperNode = getByTestId('my-test-id')
const switchNode = wrapperNode.childNodes[0].childNodes[0]
fireEvent.click(switchNode as HTMLElement)
}, |
This change allows for additional attributes to be forwarded to the
input
element viainputProps
.This currently exists in
InputBaseComponentProps
here: https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/InputBase/InputBase.d.ts#L54-L58