在我将页面背景颜色从白色更改为自定义后,我想使用react-select并遇到一系列问题。 (问题在白色背景上没有在react-select的github页面上那么明显)
我正在通过样式道具进行上述操作,因为className道具无法正常工作。
这是样式道具。
const colourStyles = {
control: styles => ({ ...styles, backgroundColor: '#023950', color: 'white', border: 0 }),
option: (styles) => {
return {
backgroundColor: '#023950',
color: 'white'
};
},
singleValue: styles => ({ ...styles, color: 'white' }),
};
以下是问题列表,以及是否有人可以帮助您解决这些问题。请参考所附图片。
答案 0 :(得分:4)
在下面的live example中,您将找到针对您不同观点的答案。
您可以通过编辑如下样式的JS样式来解决您提到的前4点:
const customStyles = {
control: (base, state) => ({
...base,
background: "#023950",
// match with the menu
borderRadius: state.isFocused ? "3px 3px 0 0" : 3,
// Overwrittes the different states of border
borderColor: state.isFocused ? "yellow" : "green",
// Removes weird border around container
boxShadow: state.isFocused ? null : null,
"&:hover": {
// Overwrittes the different states of border
borderColor: state.isFocused ? "red" : "blue"
}
}),
menu: base => ({
...base,
// override border radius to match the box
borderRadius: 0,
// beautify the word cut by adding a dash see https://caniuse.com/#search=hyphens for the compatibility
hyphens: "auto",
// kill the gap
marginTop: 0,
textAlign: "left",
// prevent menu to scroll y
wordWrap: "break-word"
}),
menuList: base => ({
...base,
// kill the white space on first and last option
padding: 0
})
};
对于最后一个,由于应该使用className
道具通过JS对选择进行样式设置,因此只会像上述here那样在外部容器上放置一个类。如果确实需要,您仍可以在组件前加上classNamePrefix
前缀,但这实际上并不能帮助您进行样式设置。