【问题标题】:react-select styling issues with style props样式道具的反应选择样式问题
【发布时间】:2019-01-16 12:09:57
【问题描述】:

我想使用 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' }),
};

以下是问题列表,如果有人可以帮助解决这些问题。请参考附图。

  1. 注意下拉菜单和选项之间的差距(当您单击下拉菜单打开选项时)
  2. 顶部和底部有一个白色间隙(在选项本身内)。这与第 1 点中提到的下拉列表的差距不同。该差距是透明的,显示了背后的内容。这个是白色的。
  3. 长文本导致选项导致整个选项框出现奇怪的空白问题。有没有办法剪辑文本并将其变成省略号,而不是让选项框变宽并进行水平滚动?
  4. 与上述问题有关。如何关闭水平滚动。想要文本剪辑。
  5. 关于使用 className 属性的问题,该类确实被应用了。但是,仅适用于最顶层 div 中的 1 个。它不适用于子 div,它们最终在 backgroundColor 中保持白色。

react-select-styling-issues

【问题讨论】:

    标签: reactjs react-select


    【解决方案1】:

    这段代码解决了我的问题

    const colourStyles = {
     menuList: styles => ({
    ...styles,
    background: 'papayawhip'
       }),
    option: (styles, {isFocused, isSelected}) => ({
    ...styles,
    background: isFocused
        ? 'hsla(291, 64%, 42%, 0.5)'
        : isSelected
            ? 'hsla(291, 64%, 42%, 1)'
            : undefined,
    zIndex: 1
    }),
    menu: base => ({
    ...base,
    zIndex: 100
      })
    }
    
    const options = [
    {value: 'chocolate', label: 'Chocolate'},
    {value: 'strawberry', label: 'Strawberry'},
    ]
    
    <Select
      // defaultValue={[colourOptions[2], colourOptions[3]]}
         name="colors"
         options={options}
         className="basic-multi-select"
         classNamePrefix="select"
         styles={colourStyles}
     />
    

    谢谢..

    【讨论】:

      【解决方案2】:

      你应该试试

      const colourStyles = {
      menuList: styles => ({
          ...styles,
          background: 'papayawhip'
      }),
      option: (styles, {isFocused, isSelected}) => ({
          ...styles,
          background: isFocused
              ? 'hsla(291, 64%, 42%, 0.5)'
              : isSelected
                  ? 'hsla(291, 64%, 42%, 1)'
                  : undefined,
          zIndex: 1
      }),
      menu: base => ({
          ...base,
          zIndex: 100
      })
      }
      
          const options = [
          {value: 'chocolate', label: 'Chocolate'},
          {value: 'strawberry', label: 'Strawberry'},
          ]
      
      <Select
         // defaultValue={[colourOptions[2], colourOptions[3]]}
            name="colors"
            options={options}
            className="basic-multi-select"
            classNamePrefix="select"
            styles={colourStyles}
         />
      

      【讨论】:

        【解决方案3】:

        在下面live example你会找到你不同观点的答案。

        您提到的前 4 点可以通过编辑 style-in-JS 来解决,如下所示:

         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 设置 select 样式,因此只会在外部容器上放置一个类,如 here 所述。如果你真的想要,你仍然可以在组件前面加上 classNamePrefix,但它不会真正帮助你进行样式设置。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-27
          • 2017-12-30
          • 1970-01-01
          • 1970-01-01
          • 2020-07-05
          • 1970-01-01
          相关资源
          最近更新 更多