I saw option onBlurResetsInput to set whether to clear input on blur or not.
And I also want to do that after choosing a value. Please show me how to do?
Thanks in advance!
Hey @cnits,
just had this issue myself.
You'll have to save the selected value somewhere (preferably in your state) and then pass it as value element to the Select on the next render.
Kind Regards,
Florian
This would be useful – sometimes with a searchable Select the user may want to place the cursor in the currently selected value to alter it, or select part of the text to change/remove it.
The display value is misleading because it _appears_ as though you can do this ^, but in fact you cannot. It might be worthwhile to add a keepInputValue
.
Another way to potentially go about this would be to provide a custom renderer for the _input_ itself.
Either way you'd want to use the input as the display for the selected value instead of the ValueRenderer. Although, there might be a clever way to approach this that supports both the ValueRenderer
prop as well as keeping the selected value in the input.
any update on this?
@cnits @jeremymarc See my comment on #2075
We already have backspaceRemoves that when explicitly set to false should satisfy your use case, right?
@mshwery
This would be useful – sometimes with a searchable Select the user may want to place the cursor in the currently selected value to alter it, or select part of the text to change/remove it.
I think the ability to re-edit the input after selecting a value would be an excellent enhancement to the project. I will put up a PR to this effect if you don't mind.
@mcpolandc @gwyneplaine I don't think "backspaceRemoves" is what we are talking about ; I would like when clicking in the search field to select the current value so you can delete it by typing any key, something like that:
Input field without focus
https://www.dropbox.com/s/6u5nl8cfu02ay41/Screenshot%202017-12-07%2013.12.44.png?dl=0
Input field with focus:
https://www.dropbox.com/s/k3ic6h4slxe41cl/Screenshot%202017-12-07%2013.13.05.png?dl=0
That was the purpose of my PR #2075
anything changed to this issue? It would be awesome to have options to edit the selected text to make new search (doesnt have to write from scratch the names to search)
I've been having a similar issue to @jeremymarc - here's an example app: https://codesandbox.io/s/q27lnon96
And here's the behaviour:
(The GIF isn't showing mouse clicks for some reason, apologies!)
Whether backspaceRemoves
is set to false or true, the value still vanishes when Select
loses focus (Although it's clearly still preserved in the state of the component, as it re-appears when Select
gets focus back).
Also running into this issue - the behavior right now creates some usability issues. backspaceRemoves
doesn't really address the problem.
Some use cases:
backspaceRemoves
is set to true
), or nothing happens (if backspaceRemoves
is set to false
). Both of these behaviors are unexpected.Related issues (possibly duplicates): https://github.com/JedWatson/react-select/issues/164#issuecomment-366284492, #250, #2054, #2296
I am also in need of this feature. The blinking cursor is causing an illusion of a text input interaction, degrading the usability of this component. The intent for my use case is to emulate the behavior of a text input with autocomplete.
@DaveOdden (and others) For V2- I was able to enable editable text by setting the inputValue
prop on the Select
to be whatever text I wanted to show in the input, which works, except for when I select a value in the dropdown, which appears to clear the input, but in actuality sets the opacity to 0. For that, I had to set opacity: 1 !important;
in my own CSS for .myContainer input
. It took days to figure this out but the input now behaves much better as you can click on the text, highlight it, the cursor appears at the end of the input like a normal text field.
The other thing I had to do was to make sure not to set blank text on the input when the input is focused- the current behavior just clears the input when you click on it, because there is a menu-close
action happening which unintentionally sets a blank string on the input. For that, in my onInputChange
handler, I do not set my local react state value when !value && action === 'menu-close
, and simply return
out of the callback so my component is not setting the inputValue to be an empty String.
Any update here?
When I select a value in the dropdown, which appears to clear the input, but in actuality sets the opacity to 0. For that, I had to set opacity: 1 !important; in my own CSS for .myContainer input.
I ran into this, too. Wondering why this is, and what the intended way of turning it off is.
I believe it's a bug, not a question, so please remove question
tag and add bug
tag to this issue!
+1 for keeping the input value on focus loss
+1
I fixed it by this way
1- onInputChange:
handleSearch = (value, {action}) => {
if (action === 'menu-close' || action === 'input-blur' || action === 'set-value') {return}
else { this.setState({searchValue: value})}
};
2- onChange: (so it sets the value of your input same as you selected)
handleSelect = ({label, value, ...rest}) => {
this.setState({ searchValue: e.value })
}
and as @lusa mentioned we need to set opacity: 1 !important
of the input field.
set inputId
on props to easily change the opacity.
display: none
to selectValue. so i can see just my normal input.@TITAN9389 could you please post an example, I can not fix it.
@TITAN9389 could you please post an example, I can not fix it.
@croxluis okay will try to write a codesandbox example in few hours.
@croxluis here is example , a little bit messy but what you ask is in index.js
https://codesandbox.io/s/rroxqy2nlo
@TITAN9389 Great, exactly what I was looking for, thank you! One thing though that I noticed is that this solution does not seem to work well with the isClearable property: that is, the clear button is never displayed. Ideally, it would be nice if a clear button could be displayed as long as something is shown in the entry box.
@TITAN9389 Great, exactly what I was looking for, thank you! One thing though that I noticed is that this solution does not seem to work well with the isClearable property: that is, the clear button is never displayed. Ideally, it would be nice if a clear button could be displayed as long as something is shown in the entry box.
You're Welcome @Alatius
umm i'm not sure about that i need to try and see it, in case if you find a way to do it before me, feel free to share :) (i'm bit busy these days)
@TITAN9389 I sort of solved it. The problem was that no option actually was selected: I had to modify the handleSearchSelection function, to set searchSelection to the selected object, not just its value. See https://codesandbox.io/s/346918zzp1
So once an option is selected, the clear button will appear. This is a partial solution to what I wanted, but to make the clear button appear when just something is entered (but nothing actually selected in the menu) will demand hacking the Select component code, so I'll leave that for now.
@TITAN9389 This really deserves a +1! Thank you so much, this finally finally fixed my problem. onblur! Should have thought of that.
Greetings all, I have addressed this in several other places and started a discussion about making this potentially easier but the approach is fairly straight forward to roll on your own.
const Input = props => <components.Input {...props, isHidden: false } />
const [value, setValue] = useState();
const [inputValue, setInputValue] = useState("");
const onInputChange = (inputValue, { action }) => {
// onBlur => setInputValue to last selected value
if (action === "input-blur") {
setInputValue(value ? value.label : "");
}
// onInputChange => update inputValue
else if (action === "input-change") {
setInputValue(inputValue);
}
};
const onChange = (option) => {
setValue(option);
setInputValue(option ? option.label : "");
};
controlShouldRenderValue
Putting this all together looks like this...
const Input = props => <components.Input {...props, isHidden: false } />
const [value, setValue] = useState();
const [inputValue, setInputValue] = useState("");
const onInputChange = (inputValue, { action }) => {
// onBlur => setInputValue to last selected value
if (action === "input-blur") {
setInputValue(value ? value.label : "");
}
// onInputChange => update inputValue
else if (action === "input-change") {
setInputValue(inputValue);
}
};
const onChange = (option) => {
setValue(option);
setInputValue(option ? option.label : "");
};
return (<Select
value={value}
inputValue={inputValue}
onInputChange={onInputChange}
onChange={onChange}
controlShouldRenderValue={false}
components={{ Input }}
...
/>)
Here is the corresponding sandbox which allows the user to freely type and edit into the input as would be expected behavior of an input.
That said, there is a dedicated discussion to share more thoughts if this were to be an internal prop to manage this, but in lieu of that, it is possible to accomplish this nonetheless.
As such, I will be closing this issue as more discussion about this can be had in the discussions channel with the provided link above.
Most helpful comment
@DaveOdden (and others) For V2- I was able to enable editable text by setting the
inputValue
prop on theSelect
to be whatever text I wanted to show in the input, which works, except for when I select a value in the dropdown, which appears to clear the input, but in actuality sets the opacity to 0. For that, I had to setopacity: 1 !important;
in my own CSS for.myContainer input
. It took days to figure this out but the input now behaves much better as you can click on the text, highlight it, the cursor appears at the end of the input like a normal text field.The other thing I had to do was to make sure not to set blank text on the input when the input is focused- the current behavior just clears the input when you click on it, because there is a
menu-close
action happening which unintentionally sets a blank string on the input. For that, in myonInputChange
handler, I do not set my local react state value when!value && action === 'menu-close
, and simplyreturn
out of the callback so my component is not setting the inputValue to be an empty String.