-
-
Notifications
You must be signed in to change notification settings - Fork 93
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
[core] Make mergeReactProps
work with non-native event handlers
#1440
Conversation
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
); | ||
} | ||
|
||
function mergeEventHandlers(theirHandler: Function, ourHandler: Function) { | ||
return (event: unknown) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michaldudak I was wondering if this was a valid use-case: if
you use useRender (or Slot) to create a custom component
with event handlers that match ours like this, and want to conditionally cancel "our" handler:
<MySlider
onValueChange={(_value, event) => {
if (/* something */) {
event.preventBaseUIDefault()
}
}}
render={
<Slider.Root />
}
/>
But I realized (generally) <NumberField.Root onValueChange={(_value, event) => { event.preventBaseUIDefault() }} />
isn't possible either
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This API was designed to handle native events only. It's meant to prevent our logic from running on DOM elements (which only accept native events). If there's a real-world scenario that requires skipping logic in our custom handlers, we may revisit it. Most (if not all) of our events are called in reaction to something happening, and there's nothing to prevent. Considering the example you posted: onValueChange
is called as a last step of setValue
, so it can't prevent any code from being run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good ~ I tested it with the original sandbox: https://codesandbox.io/p/devbox/agitated-darkness-djktpw?file=%2Fsrc%2FApp.tsx
I suppose we can revisit the issue of merging and cancelling our custom handlers once #1418 is completed
Fixed the
mergeReactProps
utility to handle event handlers that don't have the event as the first parameter. It now checks if the parameter is an object and contains thenativeEvent
field before adding thepreventBaseUIHandler
method.Additionally, I refactored the implementation to be more readable.
Fixes #1430