Skip to content

[InputBase] Allow undefined in onBlur event type #45994

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No 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? No Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

unclebay143
Copy link

Fixes #45982: Add undefined to onBlur event type in InputBase

Problem:
InputBase calls onBlur with undefined in some cases (e.g., when disabled), but the type didn’t reflect this.

Solution:
Updated the type to:

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

@mui-bot
Copy link

mui-bot commented Apr 24, 2025

Netlify deploy preview

https://deploy-preview-45994--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against 6d77f24

@zannager zannager requested a review from ZeeshanTamboli April 24, 2025 14:33
@ZeeshanTamboli ZeeshanTamboli changed the title fix(InputBase): allow undefined in onBlur event type [InputBase] Allow undefined in onBlur event type Apr 26, 2025
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

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

@unclebay143 Thanks for opening the PR, but the solution isn't correct.

Only the event parameter type should be undefined not the onBlur prop. So, we need to change it to:

- onBlur?: React.FocusEventHandler<HTMLInputElement | HTMLTextAreaElement>;
+   onBlur?: {
+   bivarianceHack(
+    event: React.FocusEvent<HTMLInputElement | HTMLTextAreaElement> | undefined,
+   ): void;
+ }['bivarianceHack'];

We can't do:

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

because SyntheticEvent doesn't support undefined.

But this would be a breaking change because now the developer would need to access the event properties using the optional chaining operator event?.target.

I'll add a comment in #45982 that this is a breaking change so we won't be able to release this in a minor or patch version.

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

Successfully merging this pull request may close these issues.

[material-ui][InputBase] onBlur called with undefined event contrary to type definition
4 participants