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] onBlur event argument might be undefined #16402

Closed
shartte opened this issue Jun 27, 2019 · 14 comments · Fixed by #18867
Closed

[TextField] onBlur event argument might be undefined #16402

shartte opened this issue Jun 27, 2019 · 14 comments · Fixed by #18867
Labels
component: text field This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation good first issue Great for first contributions. Enable to learn the contribution process.

Comments

@shartte
Copy link

shartte commented Jun 27, 2019

This seems like a revival of #9027, and causes event handlers which expect to be passed an event object to fail, since it'll be undefined.

The offending line of code is here:
https://github.com/mui-org/material-ui/blob/46af75d63b249e24872d481c99ceaa9d4a5f6e21/packages/material-ui/src/InputBase/InputBase.js#L223

This might have been introduced in the conversion to a function component, but I am unsure.
The previous fix was simply passing {} as the event object as it'll at least fix the direct issue of accessing properties of undefined.

@oliviertassinari
Copy link
Member

oliviertassinari commented Jun 28, 2019

@shartte This is a new logic that was introduced in #15446. I would propose one of these two options:

  1. We remove the onBlur call. It's a bug in React: No blur event fired when button is disabled/removed facebook/react#9142. Should we defer the problem to them?
  2. We update the TypeScript definitions to account for the fact that the event argument of blur might be missing

@oliviertassinari oliviertassinari added component: text field This is the name of the generic UI component, not the React module! discussion labels Jun 28, 2019
@eps1lon
Copy link
Member

eps1lon commented Jun 28, 2019

We update the TypeScript definitions to account for the fact that the event argument of blur might be missing

Sounds like the safer option to me.

@shartte
Copy link
Author

shartte commented Jun 28, 2019

Yeah, if the current behavior stays in place, it should at least be documented, if possible, also on the InputBase page (onBlur is currently absent there since it's forwarded to the native element I assume).
Previously this same issue was fixed by passing {} as the event object (see #9042). If the ref is still valid at that point, one might even try to pass a fake object ({target: ref.current}). I'd wager most onBlur handlers want to get at the value.

@oliviertassinari
Copy link
Member

oliviertassinari commented Jun 28, 2019

Previously this same issue was fixed by passing {} as the event object (see #9042).

@shartte I would suggest you had a closer look at the pull request. It's not an accurate fact.

@eps1lon I don't know, both options have their pros & cons. I can't resolve to one of these two options 🤷‍♂️. If you have a preference for n°2, why not!

@oliviertassinari
Copy link
Member

oliviertassinari commented Jun 28, 2019

@shartte Could you share with us the context in which you have noticed this behavior? I think that it will help us take a better tradeoff.

@shartte
Copy link
Author

shartte commented Jun 28, 2019

@oliviertassinari Ah you're right, I mistook the test code for the actual implementation. Sorry!
My onBlur handler was doing validation onBlur, which was easily skipped by checking whether the event was undefined. It was just something I wasn't expecting.

@joshwooding
Copy link
Member

Updating the typescript definition definitely makes sense if the current behaviour stays. Perhaps there should be a section on the documentation as well? So users not using typescript could potentially look there or maybe something in the prop description is better

@oliviertassinari
Copy link
Member

oliviertassinari commented Jul 1, 2019

I think that we can go with option n°2 with the TypeScript update, prop type description update and linking the React issue in a comment.

We can always revert later on to option n°1 if people still get errors in production because they try to access event.x in the blur event.

@oliviertassinari oliviertassinari added docs Improvements or additions to the documentation good first issue Great for first contributions. Enable to learn the contribution process. and removed discussion labels Jul 1, 2019
@joshwooding joshwooding added the hacktoberfest https://hacktoberfest.digitalocean.com/ label Sep 30, 2019
@joshwooding joshwooding removed the hacktoberfest https://hacktoberfest.digitalocean.com/ label Nov 3, 2019
@oliviertassinari oliviertassinari added typescript docs Improvements or additions to the documentation and removed docs Improvements or additions to the documentation typescript labels Nov 30, 2019
@oliviertassinari oliviertassinari changed the title InputBase calls onBlur without an event object [TextField] onBlur event argument might be undefined Nov 30, 2019
@oliviertassinari
Copy link
Member

I would propose the following diff:

diff --git a/packages/material-ui/src/InputBase/InputBase.js b/packages/material-ui/src/InputBase/InputBase.js
index e425bd307..5a18eb47d 100644
--- a/packages/material-ui/src/InputBase/InputBase.js
+++ b/packages/material-ui/src/InputBase/InputBase.js
@@ -548,7 +548,9 @@ InputBase.propTypes = {
    */
   name: PropTypes.string,
   /**
-   * @ignore
+   * Callback fired when the input is blurred.
+   *
+   * Notice that the first argument (event) might be undefined.
    */
   onBlur: PropTypes.func,
   /**

@shartte Do you want to work on it?

@abnersajr
Copy link
Contributor

@oliviertassinari I saw that he didn't answer so I decided to take this one.
Although, I didn't get the point to change the typescript definition. This uses a definition from React. What changes are you expecting on this type? I'm not an expert with advanced types.

@shartte
Copy link
Author

shartte commented Dec 16, 2019

@abnersajr The InputBase.ts.d has the following code:
https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/InputBase/InputBase.d.ts#L50

  onBlur?: React.FocusEventHandler<HTMLInputElement | HTMLTextAreaElement>;

The React types define FocusEventHandler as:

    type FocusEventHandler<T = Element> = EventHandler<FocusEvent<T>>;
    type EventHandler<E extends SyntheticEvent<any>> = { bivarianceHack(event: E): void }["bivarianceHack"];

That definition does not allow for the function parameter to be undefined, so Material-UI is using the wrong type. It could instead use (not tested, just off the top of my head):

  onBlur?: React.EventHandler<FocusEvent<HTMLInputElement | HTMLTextAreaElement>|undefined>;

@avasuro
Copy link

avasuro commented Aug 5, 2022

@oliviertassinari Looks like this is not closed... Typescript defintion is wrong - now onBlur handler (according to d.ts file) always receive event object: https://github.com/mui/material-ui/blob/master/packages/mui-material/src/InputBase/InputBase.d.ts#L134

@thecodingcrow
Copy link

@oliviertassinari please re-open this issue. In MUI 5.11.12 the event is not marked as nullable/optional

image

We actually just run into an runtime error because of this, thats how I ended up on this thread

@thecodingcrow
Copy link

image

Okay there is a hint hidden deep inside a .d.ts file.
But the typing is not correct, it would be great if the typing could be corrected here

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! docs Improvements or additions to the documentation good first issue Great for first contributions. Enable to learn the contribution process.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants