【问题标题】:React Select background color反应选择背景颜色
【发布时间】:2021-07-18 15:34:42
【问题描述】:

有什么建议为什么这段代码没有为 Select 设置背景颜色?

我在以下位置创建了一个示例

https://codesandbox.io/s/react-select-basic-forked-3z0kd?file=/src/index.js

【问题讨论】:

  • 道具是styles 而不是style
  • 我修改了上面的示例以使用“样式”但仍然无法正常工作

标签: reactjs react-native


【解决方案1】:

在你的课堂之外添加这个:

// BACKGROUND STYLES

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,
    // kill the gap
    marginTop: 0
  }),
  menuList: (base) => ({
    ...base,
    // kill the white space on first and last option
    padding: 0
  })
};

然后在您的 SELECT 上实现它:

<Select
          value={this.state.selectedOption}
          onChange={this.handleChange}
          options={this.options}
          styles={customStyles}
        />

这是您的代码,背景颜色工作: https://codesandbox.io/s/react-select-basic-forked-5uyfb?file=/src/index.js

干杯!

【讨论】:

  • 谢谢,这行得通。我会接受你的回答。只是一个问题:如果我只想在选择选项 1“Spring”时设置背景颜色,那么如何在 Style 中访问 this.options?
  • 很抱歉,我不知道如何回答您的问题...尝试发布另一个问题,希望人们能帮助您解决问题
  • 当然可以,再次感谢。
  • 感谢您接受我的回答,我赞成您的帖子作为回报:)
猜你喜欢
  • 2019-01-16
  • 2021-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
  • 2018-03-04
  • 2022-11-12
  • 2018-10-28
相关资源
最近更新 更多