0

我正在使用带有 Material-ui 的 React 和此处记录的自动完成组件 - https://material-ui.com/components/autocomplete/和降档。

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

使用 onSelect,我可以检索选择的值。我希望能够取回密钥。

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>
    );
}

在这里,我将 uuid 设置为每个选择的键。

我的最终目标是能够进行选择并检索 uuid 而不是值本身。

虽然我可以使用返回的值来匹配项目列表,但我想确保如果最终有任何重复条目,它不会导致问题。

我的组件完整代码的链接在这里 - https://github.com/theocranmore/bloodbike/blob/master/react_app/src/components/UsersSelect.js#L143

谢谢!

4

2 回答 2

1

您可以检索整个值,然后访问所需的密钥(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} />}
/>;
于 2020-06-02T16:45:54.080 回答
1

我不知道你为什么需要一个 id,但对于我自己来说,获取对象本身就足够了。

<Autocomplete .....
    onChange={(event, newValue) => {
        console.log(newValue); //this will give you the selected value dictionary (source)
    }}
于 2019-11-23T22:49:45.843 回答