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.

Dont't clear Input on onBlur

See original GitHub issue

I noticed that when Select is blurred, the onBlur event clears the input text field: https://github.com/JedWatson/react-select/blob/master/src/Select.js#L215

How can I disable this behaviour and keep whatever the user has started typing ?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
AlexLommcommented, Apr 7, 2021

A little less hacky way would be to watch for the actual user-initiated input changes by checking the action argument of the onInputChange().

Example:

export default function App() {
  const [input, setInput] = useState("");

  const options = [
    { label: "first option", value: 1 },
    { label: "second option", value: 2 },
    { label: "third option", value: 3 }
  ];

  return (
    <Select
      options={options}
      inputValue={input}
      onInputChange={(value, action) => {
        // only set the input when the action that caused the
        // change equals to "input-change" and ignore the other
        // ones like: "set-value", "input-blur", and "menu-close"
        if (action.action === "input-change") setInput(value); // <---
      }}
    />
  );
}

Edit relaxed-beaver-e15x0

7reactions
alexmuzenhardtcommented, Dec 9, 2021

If anybody else experienced the same problem I have written above, try this. The code below worked for me.

export default function App() {
  const [input, setInput] = useState("");

  const options = [
    { label: "first option", value: 1 },
    { label: "second option", value: 2 },
    { label: "third option", value: 3 }
  ];

  return (
    <Select
      options={options}
      inputValue={input}
      onInputChange={(value, action) => {
       if (action?.action !== 'input-blur' && action?.action !== 'menu-close') {
	setInput(value);
       }
      }}
    />
  );
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

don't clear input on select using React-Select - Stack Overflow
The problem is if you want to use the React-Select and you want persistent input value that doesn't get cleared on select or...
Read more >
react-select: Keep input value on blur and menu close
Activating extension 'vscode.typescript-language-features' failed: Could not find bundled tsserver.js.
Read more >
Focusing: focus/blur - The Modern JavaScript Tutorial
The focus event is called on focusing, and blur – when the element loses the focus. Let's use them for validation of an...
Read more >
react hook form onblur not working - You.com | The AI Search ...
So you have to setup the defaultValues in your <App /> component for all your child forms. Also you don't need useEffect in...
Read more >
HTMLElement.blur() - Web APIs - MDN Web Docs
The HTMLElement.blur() method removes keyboard focus from the current element. ... Remove focus from a text input ...
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