当.Select.is-disabled为true时,我想将颜色设置为#aaa,但我无法覆盖颜色为黑色的.Select-placeholder样式。
.Select.is-disabled > .Select-control {
cursor: not-allowed;
color: #aaa !important;
}
.Select-placeholder {
color: black !important;
}
当.Select.is-disabled为true时,我想将颜色设置为#aaa,但我无法覆盖颜色为黑色的.Select-placeholder样式。
.Select.is-disabled > .Select-control {
cursor: not-allowed;
color: #aaa !important;
}
.Select-placeholder {
color: black !important;
}
您可以使用最新版本(2)的 react-select
提供的API,像这样:
const customStyles = {
// For the select it self, not the options of the select
control: (styles, { isDisabled}) => {
return {
...styles,
cursor: isDisabled ? 'not-allowed' : 'default',
// This is an example: backgroundColor: isDisabled ? 'rgba(206, 217, 224, 0.5)' : 'white'
color: isDisabled ? '#aaa' : 'white'
}
},
// For the options
option: (styles, { isDisabled}) => {
const color = chroma(data.color);
return {
...styles,
backgroundColor: isDisabled ? 'red' : blue,
color: '#FFF',
cursor: isDisabled ? 'not-allowed' : 'default',
};
},
};
然后在 <Select>
标签中像这样使用这些 styles
:
export default () => (
<Select
defaultValue={items[0]}
label="Single select"
options={items}
styles={customStyles}
/>
);
基本的CSS知识,与react
或react-select
无关。
.Select.is-disabled > .Select-control {
cursor: not-allowed;
color: #aaa !important;
}
.Select-placeholder {
color: black !important;
}
CSS 从上到下读取代码,!important 可以覆盖之前读取的所有内容。现在你有两个 !important,这使得最后一个覆盖了之前的所有内容。
要解决这个问题,可以删除颜色为黑色的 !important,或者像这样编辑你的 CSS:
.Select.is-disabled > .Select-control {
cursor: not-allowed;
color: #aaa !important;
}
.Select-placeholder {
color: black !important;
}
.Select.is-disabled {
color: #aaa !important;
}
它是一种快速的最后手段修复方法,会带来更多麻烦而不是帮助。
尝试重构你的CSS并摆脱所有!important语句。这里不需要important,只需要进行一些重构就可以让它正常工作。
以“自上而下”的思维方式重构你的CSS,我相信你能够自己解决问题!
但你可能现在已经创造了一个!important地狱。