Skip to content
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

[Select] Fix incorrect event.target type in onChange #15272

Merged
merged 5 commits into from
Apr 11, 2019

Conversation

sperry94
Copy link
Contributor

@sperry94 sperry94 commented Apr 9, 2019

This PR adds the TS demo for MaxWidthDialog. It was separated from the other PR (#15271) since more changes were involved.

@mui-pr-bot
Copy link

mui-pr-bot commented Apr 9, 2019

No bundle size changes comparing 1d6425d...5c04e3c

Generated by 🚫 dangerJS against 5c04e3c

@eps1lon eps1lon changed the title [docs]: Adding TS demo for MaxWidthDialog [docs] Add MaxWidthDialog TypeScript demo Apr 9, 2019
@eps1lon eps1lon added docs Improvements or additions to the documentation typescript labels Apr 9, 2019
@eps1lon eps1lon self-requested a review April 9, 2019 08:10
@eps1lon eps1lon mentioned this pull request Apr 9, 2019
If this is actually not safe then the error originates from the
write. We want casts (unsafe) where we might need to add
sanitization/validation
@@ -84,7 +86,7 @@ class MaxWidthDialog extends React.Component {
id: 'max-width',
}}
>
<MenuItem value={false}>false</MenuItem>
<MenuItem value="false">false</MenuItem>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why changing the type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This had to be changed because MenuItem only accepts string | number | string[] | undefined for its type. The change in the handleMaxWidthChange covers converting this back to a boolean when this value is chosen!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could MenuItem accept boolean too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be addressed in #15245.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😩 We're breaking quite a bit of DOM/type safety for some tiny conveniences. We could just use an empty string and check for that. Otherwise we need to override the actual DOM types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's passed straight down to the DOM node, yes. It's probably better to override this with unknown since it is also used as a child of Select. But it's also used without the Select context. In different contexts you might be missing the type checking. I think we should use either some type casting or add the runtime overhead that ensures a sound value type. The value of MenuItem should not be changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should we keep the value of MenuItem as false and cast it to unknown?

Also, will there be issues trying to read event.target.value?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

event.target.value

They always end up as strings in the the DOM. With the exception of Radio and RadioGroup no component reads this value though. We try to only use the value from props.

So should we keep the value of MenuItem as false and cast it to unknown?

I think the Select already passes the value as the second argument to onChange. That value is the value from the prop and not the DOM. You could use that instead and just force it into state via any/unknown.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I see. So the idea is to mostly use the value returned as the second param in the onChange event? I will push a change!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it depends. If you want the value you passed as a prop you use the second param (type unknown). If you want the value in the DOM you use event.target.value (type string).

@eps1lon eps1lon added on hold There is a blocker, we need to wait and removed on hold There is a blocker, we need to wait labels Apr 10, 2019
@eps1lon
Copy link
Member

eps1lon commented Apr 11, 2019

@sperry94 Sorry for causing more confusion than clarification. The underlying SelectInput component actually uses a React.ChangeEvent<{ name: string?, value: unknown }>. The second argument is the the child that triggered the change.

This should help typing Select components as well.

@eps1lon eps1lon force-pushed the maxWidthDialog-ts-docs branch from eb106d2 to ffc6f5e Compare April 11, 2019 09:46
@eps1lon eps1lon added the component: select This is the name of the generic UI component, not the React module! label Apr 11, 2019
@eps1lon eps1lon changed the title [docs] Add MaxWidthDialog TypeScript demo [Select] Fix incorrect event.target type in onChange Apr 11, 2019
@eps1lon eps1lon merged commit c5700e0 into mui:next Apr 11, 2019
@sperry94
Copy link
Contributor Author

@eps1lon No worries! I looked at the SelectInput code and now I see what’s going on! The target for the raised event is getting set to contain the value/name from the child.

@jonfreedman

This comment has been minimized.

@eps1lon

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: select This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation typescript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants