css - 将所选元素的颜色更改为黑色并在 React Picky 中保持占位符的颜色为灰色

标签 css reactjs

对多选下拉菜单使用 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

关于css - 将所选元素的颜色更改为黑色并在 React Picky 中保持占位符的颜色为灰色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55415141/

相关文章:

css - 在CSS Flexbox中,为什么没有“justify-items”和“justify-self”属性?

css - 如何更改 LESS CSS 编译器的编译顺序

javascript - 将图像与其 ALT 链接

reactjs - React + Flux - 应该将数据存储在组件状态还是 Prop 中?

javascript - 无法过滤数组以在 react 应用程序上获取电影

reactjs - 使用 'babel' 加载程序时,Node.js 的 Firebase 库会中断。具体是: "Uncaught TypeError: Cannot read property ' navigator' of undefined"

jquery - 当图像更改时,我的旋转木马正在改变高度

javascript - 不正确的高度iphone

javascript - redux bindActionCreators 任何操作都会被调度

reactjs - React - axios - 被 CORS 策略阻止。如何解锁