【发布时间】:2020-01-20 23:40:34
【问题描述】:
我正在尝试实现具有checkbox 的Auto complete。
https://material-ui.com/components/autocomplete/#autocomplete
但是当我在final-form 中实现相同的组件时,我无法检查我的选项,为什么?
这是我的代码 https://codesandbox.io/s/relaxed-breeze-hv58o
<Autocomplete
{...rest}
multiple={multiple}
disableCloseOnSelect={true}
options={
multiple
? maxReached
? []
: options.filter(option => !value.includes(option.value))
: options
}
defaultValue={
multiple
? options.filter(option => value.includes(option.value))
: options.find(option => option.value === value)
}
onChange={
multiple
? (_e, values) => {
setMaxReached(value.length >= max - 1);
onChange(values.map(option => option.value));
}
: (_e, option) => onChange(option.value)
}
getOptionLabel={option => option.label}
noOptionsText={
maxReached
? formatMessage({ id: "components.autocomplete.max" }, { max })
: formatMessage({ id: "components.autocomplete.no" })
}
renderOption={(option, { selected }) => (
<React.Fragment>
<Checkbox
icon={icon}
checkedIcon={checkedIcon}
style={{ marginRight: 8 }}
checked={selected}
/>
{option.label}
</React.Fragment>
)}
renderInput={params => (
<TextField
{...params}
{...restInput}
label={label}
placeholder={placeholder || ""}
helperText={
hasError ? formatMessage({ id: error }, { label }) : helperText
}
error={hasError}
fullWidth
/>
)}
/>
);
};
【问题讨论】:
-
值
selected的类型是什么?它是一个布尔值吗? -
我认为是的...我只是从这里复制任何粘贴material-ui.com/components/autocomplete/#autocomplete
-
您应该尝试对值进行硬编码以开始查看是否是问题所在。只需将
selected替换为true,然后让我们从那里开始。 -
另外,
selected的值需要来自某种状态,无论是组件状态还是使用 Redux。 -
是的,它是布尔值..
issue is this function not called again renderOption={(option, { selected }) => ( <React.Fragment> <Checkbox icon={icon。当用户检查任何项目时
标签: javascript reactjs material-ui react-final-form final-form