react-autosuggest icon indicating copy to clipboard operation
react-autosuggest copied to clipboard

Double Values, one invisible

Open repositoryofexcellence opened this issue 9 years ago • 6 comments

Hello. My question is, i want to display the value visible to the input, but want to pass the code by invisible to the ajax form too. Is that possible? My suggestion list is like this:

`const suggestions = [

{
    code: 'SP',
    label: 'Sahinn Paradise',
},
{
    code: 'EDH',
    label: 'Elvin Deluxe Hotel',
},

]`

repositoryofexcellence avatar Sep 17 '17 13:09 repositoryofexcellence

I'm in the same situation and just now struggling to find a way to do it...

julian69 avatar Sep 19 '17 02:09 julian69

Sorry, I don't understand the question. Could you rephrase, please?

moroshko avatar Sep 19 '17 02:09 moroshko

In my case, what I need is to display a value and send another one. I'm trying to do it by creating an input hidden, but I couldn't find yet a consistent way of handling that value (the hidden one). For instance, this is what I'm trying at this point:

getSuggestionValue(suggestion) {
  if (this.props.inputHidden) {
    this.setState({
      hiddenValue: suggestion[this.props.inputHidden.propFromListAsInputValue]
    });
  }

  return suggestion[this.props.propFromListAsInputName];
}

Is there an existing approach to this situation? Thanks!

PS: [this.props.inputHidden.propFromListAsInputValue] --> this is the way I'm setting the property I'm looking for (name, id, etc)

julian69 avatar Sep 19 '17 03:09 julian69

@julian69 Setting the state in getSuggestionValue is definitely the wrong approach. getSuggestionValue should be a pure function mapping a suggestion to its input value.

Autosuggest doesn't send anything. Its purpose is to help you to fill in the input faster. The return value of getSuggestionValue() will always become the input's value. It's up to you how you display the suggestions (using renderSuggestion). But, once you select a suggestion, getSuggestionValue() will determine the value of the input. You cannot change that.

I have a feeling that react-autosuggest might not be the best choice for your use case. Have a look at react-select.

moroshko avatar Sep 19 '17 04:09 moroshko

it's a very common case though ... I guess smthg's possible with the suggestionIndex arg passed in the onSuggestionSelected hook

matthieugayon avatar Nov 28 '17 19:11 matthieugayon

This seems like a very common use case. I needed a user selector that would display the user's name, but store the user's ID. To add a little complexity, I wanted material-ui controls and formik-controlled form fields.

I got this to work by using onSuggestionSelected(). Since that is being passed the selected object (not just the string-projected value), passing the value up the chain wasn't hard. As far as autosuggest is concerned, we're only dealing with the user's name.

Maybe the API was extended after you guys' conversation. In any case, if someone else stumbles upon this, it is certainly possible to show one thing and store another.

cmenge avatar May 01 '18 15:05 cmenge