css - 有条件地覆盖 CSS 中的 AntD Select 样式

标签 css reactjs conditional-statements

我有一个包含 antD Select 组件的表单。

          <Select
            name="SeasonId"
            onChange={onSeasonChange}
            placeholder="Choose Season"
          >
            // Select Options
          </Select>

enter image description here

我希望能够根据条件更改边框(和框阴影)的颜色。我可以通过将以下内容添加到单独的 CSS 文件中来更改/覆盖我需要的 css:

.ant-select-selector:hover {
  border-color: #1f9643e5 !important;
}

.ant-select-focused .ant-select-selector,
.ant-select-selector:focus,
.ant-select-selector:active,
.ant-select-open .ant-select-selector {
  border-color: #1f9643e5 !important;
  outline: 0 !important;
  -webkit-box-shadow: 0 0 0 2px rgba(49, 139, 54, 0.342) !important;
  box-shadow: 0 0 0 2px rgba(49, 139, 54, 0.342) !important;
}

这会将样式更改为我想要的样式:#

enter image description here

但是,如果代码中满足特定条件(即我有一个 isEditMode 状态 bool 值),我只想将此样式覆盖应用于 Select。我可能会遗漏一些明显的东西,但有兴趣听到任何建议。

最佳答案

为自定义 CSS 代码使用父类名称,并在条件为 true 时将该类名称应用于父元素。

例如, 在你的 CSS 文件中

.example.ant-select-selector:hover {
  border-color: #1f9643e5 !important;
}

在你的 JSX 文件中,

<div className = {`${conditon ? 'example' :''}`} >
   <Select />
</div>

关于css - 有条件地覆盖 CSS 中的 AntD Select 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71537943/

相关文章:

javascript - 如何将子 props 与传入的 Parent 状态断开?

javascript - 解构赋值默认值

java - jdk 1.8 String.equalsIgnoreCase 中的重复空检查

css - 如何在所选选项卡上的 React JS 中动态添加类名

html - 当鼠标悬停时,我想将背景图像带到整个 div

javascript - Angular-google-area-chart 图表背景颜色重叠

reactjs - 在 Material UI 中使用样式组件应用单选按钮颜色?

mysql - mysql中基于条件的子查询的输出

html - 缩小手机背景

css - 左侧和右侧容器不能改变右侧