Double Values, one invisible
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',
},
]`
I'm in the same situation and just now struggling to find a way to do it...
Sorry, I don't understand the question. Could you rephrase, please?
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 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.
it's a very common case though ... I guess smthg's possible with the suggestionIndex arg passed in the onSuggestionSelected hook
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.