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

TextField onKeyDown typescript event type is incorrect #12946

Closed
2 tasks done
Swazimodo opened this issue Sep 20, 2018 · 3 comments
Closed
2 tasks done

TextField onKeyDown typescript event type is incorrect #12946

Swazimodo opened this issue Sep 20, 2018 · 3 comments
Labels
component: text field This is the name of the generic UI component, not the React module! typescript

Comments

@Swazimodo
Copy link

Swazimodo commented Sep 20, 2018

  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

A function with an event arg of type KeyboardEvent should for the onKeyDown event handler.

Current Behavior

This event handler expects KeyboardEvent which is counter intuitive.

Steps to Reproduce

<TextField
   onKeyDown={(event: KeyboardEvent<HTMLInputElement>) => {}}
/>

Context

I'm using typescript in our environment and some TextField events like onChange will use HTMLInputElement type but the key down event expected HTMLDivElement. I would have expected all event handlers to use the same types.

Your Environment

Tech Version
Material-UI v^1.1.0
React v^16.3.2
Chrome v68.0.3440.106
TypeScript v2.8.4
@lukePeavey
Copy link
Contributor

@Swazimodo
I took a look at the TextField props ... it doesn't expose onKeyUp/onKeyDown props for the input, like it does with onChange. So when you use these props on TextField they are spread to the root div element, not passed down to the input.

You can use InputProps to access the Input's onKeyUp/onKeyDown props, which have the expected types.

<TextField
  InputProps={{
    onKeyDown: (event: React.KeyboardEvent<HTMLInputElement>) => {},
  }}
/>

@eps1lon
Copy link
Member

eps1lon commented Oct 2, 2018

@Swazimodo Is right that it does attach those listeners to a div but this is only relevant for currentTarget. The target will still be the input element: https://codesandbox.io/s/p90k1rwpym

As a workaround you can do what @lukePeavey suggested.

Doing this correct in our type declarations would require quite some work so I'm more than happy to accept PRs.

@eps1lon
Copy link
Member

eps1lon commented Jun 13, 2019

Accepted answer: #12946 (comment)

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

No branches or pull requests

4 participants