Skip to content

[material-ui][InputBase] onBlur called with undefined event contrary to type definition #45982

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
1 task done
sydneyjodon-wk opened this issue Apr 22, 2025 · 5 comments
Open
1 task done

Comments

@sydneyjodon-wk
Copy link
Contributor

sydneyjodon-wk commented Apr 22, 2025

Hello 👋

Search keywords

undefined event onBlur

Latest version

  • I have tested the latest version

Steps to reproduce

Steps:

  1. Open this link to live example: https://stackblitz.com/edit/react-zufzmfhc?file=Demo.tsx
  2. Type something in the input
  3. See in the console that the event passed to onBlur is undefined if the InputBase is disabled

Current behavior

InputBase is calling onBlur with an undefined event in certain cases, but the type definition for onBlur does not have a nullable event:

/**
* Callback fired when the `input` is blurred.
*
* Notice that the first argument (event) might be undefined.
*/
onBlur?: React.FocusEventHandler<HTMLInputElement | HTMLTextAreaElement>;

Even though there is a comment, undefined is not an option for the events.

Expected behavior

I would have expected the behavior of the onBlur to match the type definition and no undefined event to be passed in. Would it be possible to either update the type definition to include undefined or update the onBlur call to pass in an event for InputBase?

Context

We are running with strict null safety and this is causing an issue.

Your environment

See sandbox: https://stackblitz.com/edit/react-zufzmfhc?file=Demo.tsx

Thank you!

@sydneyjodon-wk sydneyjodon-wk added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Apr 22, 2025
@ZeeshanTamboli
Copy link
Member

I think we can add undefined to onBlur parameter type since we can’t pass the event to it inside useEffect.

Hopefully it won’t be a breaking change. Want to give it a try and open a PR?

@ZeeshanTamboli ZeeshanTamboli added typescript package: material-ui Specific to @mui/material and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Apr 23, 2025
@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Apr 26, 2025

As expected, this would be a breaking change (See #45994 (review)) Won't just the JSDoc already mentioning that the event might be undefined work for you?

Unfortunately, we can't add this change.

@sydneyjodon-wk
Copy link
Contributor Author

@ZeeshanTamboli thank you for trying! Our code is expecting the typing to be honored strictly so the fact that undefined is being passed in when the type says that it never will be is throwing for us so just having a doc comment isn't enough. It's okay though - we will work around it on our end. Would it be possible to consider adding this to the next major?

@ZeeshanTamboli
Copy link
Member

Would it be possible to consider adding this to the next major?

Yes, we can.

@sydneyjodon-wk
Copy link
Contributor Author

Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants