是的,可以提供您自己的自定义样式,react-select 提供了一种基于对象的方法来设置Select 组件的样式。
参考docs
这是一个简单的例子,
const customStyles = {
option: (provided, state) => ({
...provided,
borderBottom: '1px dotted pink',
color: state.isSelected ? 'red' : 'blue',
padding: 20,
}),
control: () => ({
// none of react-select's styles are passed to <Control />
width: 200,
}),
singleValue: (provided, state) => {
const opacity = state.isDisabled ? 0.5 : 1;
const transition = 'opacity 300ms';
return { ...provided, opacity, transition };
}
}
const App = () => (
<Select
styles={customStyles} // pass the customStyles object to the styles prop
options={...}
/>
);
通过提供给自定义style object 的键,Select 是非常可定制的。
要记住的一点是,每个键都是一个样式函数,其中第一个参数是提供的样式,第二个参数是选择的状态,即isFocused、isSelected。
编辑- 虽然这是使用基于对象的方法提供自定义样式的建议方法。如果你真的想用styled-components 设置Select 组件的样式,这里有一个例子(你必须通过prop 和基于classNames 的样式提供classNamePrefix)
更多关于使用 classNames docs 进行样式设置的详细信息
import Select from 'react-select';
import styled from 'styled-components';
const SelectElement = styled(Select)`
.react-select-container {
// custom styles
}
.react-select__control {
// custom styles
}
.react-select__indicators {
// custom styles
}
`;
const MySelect = (props) => {
return (
<SelectElement classNamePrefix="react-select" options={options} {...props}/>
)
}