Ali*_*lin 5 reactjs material-ui
有没有办法在material-ui自动完成选项之间添加分隔线?
我只找到了这个解决方法,但我认为这不是最好的方法:
const options = [
{
label: 'Opt 1',
value: 'OPT1'
},
{
label: 'Divider',
value: 'DIV',
isDivider: true
},
{
label: 'Opt 2',
value: 'OPT2'
},
{
label: 'Opt 3',
value: 'OPT3'
}
];
...
// used to remove the opacity of disabled items (in my case dividers)
autocompleteOption: {
'&[aria-disabled="true"]': {
opacity: 1
}
}
...
<Autocomplete
options={options}
multiple
getOptionLabel={(option) => option.label}
getOptionSelected={(option, value) => isEqual(value, option)}
classes={{
option: classes.autocompleteOption
}}
getOptionDisabled={(option) => option.isDivider}
renderOption={(option) => {
if (option.isDivider) {
return <Divider className={classes.divider} />;
}
return option.label;
}}
...
/>
Run Code Online (Sandbox Code Playgroud)
你还有其他建议吗 ?
感谢您的时间 :)
与其创建虚拟选项,不如标记后面应有分隔符的选项怎么样?
例如,假设您想在第三个和第四个选项之间添加分隔线。将第三个选项设置isDivider为true
options[2].isDivider = true
Run Code Online (Sandbox Code Playgroud)
然后将其插入<Divider />到 renderOption 中,如下所示:
renderOption={(props, option: any) => (
<>
<Li {...props}>{option.title}</Li>
{option.isDivider ? <Divider /> : null}
</>
)}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2471 次 |
| 最近记录: |