Skip to content

Comments

[InputBase] Allow undefined in onBlur event type#45994

Closed
unclebay143 wants to merge 1 commit intomui:masterfrom
unclebay143:fix/inputbase-onblur-event-type
Closed

[InputBase] Allow undefined in onBlur event type#45994
unclebay143 wants to merge 1 commit intomui:masterfrom
unclebay143:fix/inputbase-onblur-event-type

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.

@ZeeshanTamboli
Copy link
Member

Closing this since this is a breaking change. We can add it in the next major.

@unclebay143
Copy link
Author

That make sense, thanks for the feedback, looking forward to the fix in the major.

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

Labels

breaking change Introduces changes that are not backward compatible. typescript

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