Controller with a Custom Input that has no underlying Ref is not submitting by hitting <enter> key
See original GitHub issueI have created a custom input that has choice buttons and saves the state of the last clicked button. I could have saved the state in a hidden input field but since the component and its onChange callback will get more complex in future I would like to avoid that. This is the implemenation:
const RadioButtonGroup = ({ value, label, options, onChange }) => {
const handleClick = (event) => {
event.preventDefault();
if (
options &&
event.currentTarget &&
event.currentTarget.dataset &&
event.currentTarget.dataset.index &&
onChange
) {
onChange(options[parseInt(event.currentTarget.dataset.index, 10)]);
}
};
return (
<div>
{label && <label htmlFor="name">{label}</label>}
<div>
{options?.map((option, index) => {
return (
<button onClick={handleClick} key={option} data-index={index}>
{option === value ? "*" : ""}
{option}
</button>
);
})}
</div>
</div>
);
};
Describe the bug
When using the submit button everything works fine. But when I hit the <enter>
key there are the following problems:
- The input switches to the first option
- The form would not submit
Also the reset button is not working.
To Reproduce
- Click on ‘maybe’
- Click inside the first input
- Type ‘foo’
- Hit
<enter>
key
Also feel free to click the reset button.
Codesandbox link (Required) https://codesandbox.io/s/react-hook-form-controlled-mixed-with-uncontrolled-forked-yrmp5?file=/src/index.js
Expected behavior I would think that the form should behave like clicking the submit button.
Desktop (please complete the following information):
- OS: OSX
- Browser: Chrome
- Version: 84.0.4147.135
Additional context It might be my naive Custom Input implementation, please advise me how to fix that and I’ll promise to contribute to your docs.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Try to follow below: every time the user click on the button store into the state.
I think it’s easier to host the state in
useState
👍