React-select: How to keep value in textbox search after choosing it

Created on 26 Jun 2017  Â·  26Comments  Â·  Source: JedWatson/react-select

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!

categorquestion issureviewed

Most helpful comment

@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.

All 26 comments

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?

2075

@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:

feb-06-2018 10-44-11

(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:

  1. User clicks into text field that has a value already set and sees a blinking cursor, indicating it's an editable text field. Then the user presses the backspace key.

    • Expected behavior: the last character of the string should be deleted (normal behavior with a blinking caret)

    • Actual behavior: the entire string is deleted (if backspaceRemoves is set to true), or nothing happens (if backspaceRemoves is set to false). Both of these behaviors are unexpected.

  2. The user clicks into the text field, sees a blinking cursor, and presses the left arrow key.

    • Expected behavior: the blinking caret should shift one character to the left

    • Actual behavior: no response

  3. The user clicks into text field, sees the blinking caret at the end of the line, and then tries to click in the middle of the string to change the caret location.

    • Expected behavior: the blinking caret changes to the middle of the string, where the user clicked.

    • Actual behavior: no response

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