-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Dont't clear Input on onBlur #588
Comments
I'm open to adding an |
Hi @JedWatson, thanks, I've created the PR as suggested. And great job on |
Any update in v3 ? |
@JedWatson How to not clear the input value on blur? |
Here's a solution that worked for me const App = () => {
const [input, setInput] = useState("");
const [inputSave, setSave] = useState("");
return (
<Select
placeholder={inputSave} // when blurred & value == "" this shows
value="" // always show placeholder
inputValue={input} // allows you continue where you left off
onInputChange={setInput} // allows for actually typing
onMenuClose={() => setSave(input)} // before the input is cleared, save its value here
onFocus={() => {
setInput(inputSave); // keeps the input
setSave(""); // prevents undesired placeholder value
}}
blurInputOnSelect // actually allows for ^^ to work
/>
)
} This keeps the search on select, meaning that when you type input, select, the select bar will keep a placeholder of the input and then when you click on it again it will continue where you left off -- you can see an example here |
A little less hacky way would be to watch for the actual user-initiated input changes by checking the 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); // <---
}}
/>
);
} |
@AlexLomm thanks for your solution. However, if you select a value from the options now, the value will not be displayed anymore. In your CodeSandBox and in my app is it the same behaviour. |
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);
}
}}
/>
);
} |
@javascript-Alex it's worked for me, why works? |
any idea how to do this when usint Controller from |
i tried this but every keystroke blurred the input... |
I noticed that when
Select
is blurred, theonBlur
event clears the input text field: https://github.com/JedWatson/react-select/blob/master/src/Select.js#L215How can I disable this behaviour and keep whatever the user has started typing ?
The text was updated successfully, but these errors were encountered: