简体   繁体   English

MaterialUI 自动完成 - 选择选项时避免清除输入文本过滤器

[英]MaterialUI Autocomplete - Avoid clearing input text filter when option is selected

I've imported the Autocomplete component from MaterialUI in my React project and using it as a multiple select with checkboxes: https://material-ui.com/components/autocomplete/#checkboxes我在 React 项目中从 MaterialUI 导入了自动完成组件,并将其用作带有复选框的多选: https : //material-ui.com/components/autocomplete/#checkboxes

I noticed that when I type into the input to filter the list and then select a value, the filter inserted by the user resets.我注意到当我输入输入来过滤列表然后选择一个值时,用户插入的过滤器会重置。 I want to avoid this and continue to multi-select with the filter instead of reinsert it every time.我想避免这种情况并继续使用过滤器进行多选,而不是每次都重新插入它。 I didn't find any props in the component API to solve this.我没有在组件 API 中找到任何道具来解决这个问题。

Any suggestion?有什么建议吗?

That's my component code:这是我的组件代码:

const VirtualAutocomplete = (props) => {
    const classes = useStyles();
    const textClasses = textStyles();

    return (
        <Autocomplete
            id={props.id}
            style={{ width: 'auto' }}
            value={props.value}
            limitTags={4}
            noOptionsText="No records found."
            classes={classes}
            disableCloseOnSelect
            ListboxComponent={ListboxComponent}
            renderGroup={renderGroup}
            onChange={props.onChange}
            options={props.options}
            filterOptions={startsWith}
            multiple={props.multiple}
            renderInput={(params) =>
                <ThemeProvider theme={theme}>
                    <TextField {...params}
                        variant='outlined'
                        classes={{ root: textClasses.formControlRoot }}
                        InputLabelProps={{ classes: { root: textClasses.labelRoot } }}
                        label={props.label}
                    />
                </ThemeProvider>
            }
            renderOption={(option, { selected }) => (
                <Fragment>
                    <Checkbox
                        icon={icon}
                        checkedIcon={checkedIcon}
                        style={{ marginRight: 8 }}
                        checked={selected}
                    />
                    {option}
                </Fragment>
            )}
        />
    );
}

Create a state that holds input value.创建一个保存输入值的状态。 Then on TextField onChange pass the function to change this state.然后在TextField onChange传递函数来改变这个状态。 Then on Autocomplete pass the props inputValue with that state content.然后在Autocomplete传递带有该状态内容的道具inputValue You can also use disableCloseOnSelect props to Autocomplete so options box doesnt close on option selected.您还可以使用disableCloseOnSelect道具来Autocomplete因此选项框不会在选择选项时关闭。

Take a look at their docs about those props https://material-ui.com/pt/api/autocomplete/看看他们关于这些道具的文档https://material-ui.com/pt/api/autocomplete/

Here is a example using their demo: https://codesandbox.io/s/material-demo-forked-pdh81?file=/demo.js:746-766这是使用他们的演示的示例: https : //codesandbox.io/s/material-demo-forked-pdh81?file=/ demo.js: 746-766

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM