0

I'm using React with Material-ui and the Autocomplete component documented here - https://material-ui.com/components/autocomplete/ with downshift.

                <Downshift id="downshift-options">
                {({
                      clearSelection,
                      getInputProps,
                      getItemProps,
                      getLabelProps,
                      getMenuProps,
                      highlightedIndex,
                      inputValue,
                      isOpen,
                      openMenu,
                      selectedItem,
                  }) => {
                    const {onSelect, onBlur, onChange, onFocus, ...inputProps} = getInputProps({
                        onChange: event => {
                            if (event.target.value === '') {
                                clearSelection();
                            }
                        },
                        onSelect: event => {
                            if (event.target.id) {
                                this.props.onSelect(event.target.value);
                            }

                        },
                        onFocus: openMenu,
                        placeholder: 'Type to search',
                    });

                    return (
                        <div className={classes.container}>
                            {renderInput({
                                fullWidth: true,
                                classes,
                                label: "Assigned Rider",
                                InputLabelProps: getLabelProps({shrink: true}),
                                InputProps: {onBlur, onChange, onFocus, onSelect},
                                inputProps,
                            })}

                            <div {...getMenuProps()}>
                                {isOpen ? (
                                    <Paper className={classes.paper} square>
                                        {getSuggestions(this.props.suggestions, inputValue, {showEmpty: true}).map((suggestion, index) =>
                                            renderSuggestion({
                                                suggestion,
                                                index,
                                                itemProps: getItemProps({item: suggestion.label}),
                                                highlightedIndex,
                                                selectedItem,
                                            }),
                                        )}
                                    </Paper>
                                ) : null}
                            </div>
                        </div>
                    );
                }}
            </Downshift>

With onSelect, I can retrieve the value of the selection. I'd like to be able to retrieve the key instead.

function renderSuggestion(suggestionProps) {
    const { suggestion, index, itemProps, highlightedIndex, selectedItem } = suggestionProps;
    const isHighlighted = highlightedIndex === index;
    const isSelected = (selectedItem || '').indexOf(suggestion.label) > -1;

    return (
        <MenuItem
            {...itemProps}
            key={suggestion.uuid}
            value={suggestion.uuid}
            selected={isHighlighted}
            component="div"
            style={{
                fontWeight: isSelected ? 500 : 400,
            }}
        >
            {suggestion.label}
        </MenuItem>
    );
}

Here I set the uuid as the key for each selection.

My ultimate aim is to be able to make a selection and retrieve a uuid instead of the value itself.

Although I can use the value returned to match against a list of items, I want to be sure that if there end up being any duplicate entries, it doesn't cause problems.

Link to my full code for the component is here - https://github.com/theocranmore/bloodbike/blob/master/react_app/src/components/UsersSelect.js#L143

Thank you!

themusicalduck
  • 105
  • 1
  • 7

2 Answers2

1

I don't know why you need an id but for my own getting the object itself will suffice.

<Autocomplete .....
    onChange={(event, newValue) => {
        console.log(newValue); //this will give you the selected value dictionary (source)
    }}
Sam
  • 86
  • 2
  • 10
1

You can retrieve the entire value and then access your desired key (option.id):

const options = [
      { id: 0, value: "foo" },
      { id: 1, value: "goo" },
    ];

<Autocomplete
  options={options}
  onChange={(event, option) => {
    console.log(option.id); // 1
  }}
  renderInput={(params) => <TextField {...params} />}
/>;
Daniela
  • 151
  • 1
  • 1
  • 8