css - 将所选项目的颜色更改为黑色并在 React Picky 中保持占位符的颜色为灰色
问题描述
对多选下拉菜单使用 react picky。尝试将占位符颜色保持为灰色,同时选择后颜色为黑色。
html
<Picky multiple={true}
includeFilter={true}
onChange={this.onRulesSelectChange}
placeholder={"Please Select"} />
在 css 中覆盖 picky 的占位符类,但它也适用于选定的文本。所以文字也变灰了。
CSS
.picky__placeholder{
font-family: CSePRoman, Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 1rem;
color: grey;
line-height: 21px;
}
实际的
占位符 -> 灰色
选定文本 -> 灰色
预期的
占位符 -> 灰色
选定文本 -> 黑色
提前致谢!
解决方案
您刚刚选择了在元素上保持不变的类名称,无论您选择与否。如果您想将初始未选择的值着色为灰色,您可以这样做。
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
arrayValue: []
};
this.onRulesSelectChange = this.onRulesSelectChange.bind(this);
}
//Set the updated value array in your state
onRulesSelectChange(value) {
this.setState({ arrayValue: value });
}
render() {
return (
<div className="container">
<div className="row">
<div className="col">
<h3>Multi select</h3>
<Picky
//Conditionally assign a class based on size values selected
className={
this.state.arrayValue.length > 0 ? "items-selected" : "items-not-selected"
}
value={this.state.arrayValue}
options={??} //Put in your options as you did
multiple={true}
includeFilter={true}
onChange={this.onRulesSelectChange}
placeholder={"Please Select"}
/>
</div>
</div>
</div>
);
}
}
使用以下规则更新您的 css 文件
.not-selected span.picky__placeholder {
color: grey
}
查看此实时代码沙箱示例:https ://codesandbox.io/s/0zz0q9670
推荐阅读
- python - 除了 numpy 数组,Python 从 long 转换
- excel - VBA Excel:从特定单元格中搜索某行的某个变量值然后将值复制并粘贴到该列的宏
- database - 在数组中查找 ObjectId
- angular - 如何使用 Angular 2+ 中的指令获取图像 src 值?
- cordova - Cordova/JQuery 移动应用程序在构建后无法正确显示页面
- oracle - 通过 dblink (Oracle <> Postgres) 查询 JSONB
- javascript - url上的回调函数?
- r - KNN 可视化 - 如何围绕使用 R 连接到 N 个最近点的数据点绘制一个圆圈
- reactjs - Reactjs 随机颜色选择器 - 哇一定有更简单的方法吗?
- sql - oracle sql - 如何从某些范围内获取具有最大值的记录