question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Controller with a Custom Input that has no underlying Ref is not submitting by hitting <enter> key

See original GitHub issue

I 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

  1. Click on ‘maybe’
  2. Click inside the first input
  3. Type ‘foo’
  4. 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:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
bluebill1049commented, Aug 24, 2020

Try to follow below: every time the user click on the button store into the state.

const Input = ({value, onChange}) => {
  const [data, setData] = useState(value)
  return <button value={data} onClick={(e)=> { setData(e.target.value); onChange(e.target.value); }} } />
}
1reaction
bluebill1049commented, Aug 24, 2020

I think it’s easier to host the state in useState 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent users from submitting a form by hitting Enter
If you'd like to allow enter key on submit buttons <input|button type="submit"> too, then you can always refine the selector as below. $(document).on("keydown", ......
Read more >
Prevent enter key from submitting · Discussion #2549 - GitHub
Hi,. I'd like to prevent the enter key from submitting. I have a MUI Textfield that I'm using as an autocomplete. If the...
Read more >
The Enter Key should Submit Forms, Stop Suppressing it
Basically, if the user hits enter when a text field is focused, the browser should find the first submit button in the form...
Read more >
Focus management with Vue refs - Learn web development
We'll look at using Vue refs to handle this — an advanced feature that allows you to have direct access to the underlying...
Read more >
Input in Unity made easy (complete guide to the new system)
In this complete quick-start guide, you'll learn everything you need to know to get started with Unity's new Input System, step by step....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found