react select v2 (update)
这个新版本引入了一个新的styles-api
和一些其他重大变化.
自定义样式
使用stylesprops 使用自定义css设置单个组件的样式.
const colourStyles = {
control: styles => ({ ...styles, backgroundColor: 'white' }),
option: (styles, { data, isDisabled, isFocused, isSelected }) => {
const color = chroma(data.color);
return {
...styles,
backgroundColor: isDisabled ? 'red' : blue,
color: '#FFF',
cursor: isDisabled ? 'not-allowed' : 'default',
...
};
},
...
};
export default () => (
<Select
defaultValue={items[0]}
label="Single select"
options={items}
styles={colourStyles}
/>
);
现在,该项目的网站上有了更好的文档和更清晰的示例:
https://react-select.com/upgrade-guide#new-styles-api
https://react-select.com/home#custom-styles
https://react-select.com/styles#styles
react-select v1 ( old answer - deprecated )
您可以为组件提供一个自定义的类名props ,该props 将被添加到基础中.为外部容器 Select className.内置选项渲染器还支持自定义类名.
Add your custom
className
as a property to objects in the options array:
const options = [
{label: "one", value: 1, className: 'custom-class'},
{label: "two", value: 2, className: 'awesome-class'}
// more options...
];
...
<Select options={options} />
MenuRender属性可用于覆盖选项的默认下拉列表.
optionClassName String
用于选项的类名
示例:react-select/master/src/utils/defaultMenuRenderer.js