react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

useSwitch ignores onChange prop

Open rotous opened this issue 2 years ago • 1 comments

🐛 Bug Report

When using the useSwitch hook to build a switch, the AriaSwitchProps defines an onChange-handler that should be called when the switch gets toggled. However this handler is ignored when passed as a prop.

🤔 Expected Behavior

The onChange handler that is passed as a prop should be called with the new selected state as argument.

😯 Current Behavior

The passed onChange handler is ignored.

💁 Possible Solution

Call the user defined onChange handler in the onChange handler that is used to set the new state.

rotous avatar Sep 22 '22 13:09 rotous

It should be getting called. Here's a working codesandbox: https://codesandbox.io/s/react-aria-switch-example-42ydb6?file=/src/App.tsx

Are you able to reproduce the issue you're seeing in a codesandbox, or post a code snippet?

reidbarber avatar Sep 22 '22 15:09 reidbarber

Ah, my bad. I didn't pass the props to useToggleState(). Thanks for the quick response and the codesandbox!

rotous avatar Sep 22 '22 19:09 rotous