【问题标题】:Styling a Select element from react-select从 react-select 设置 Select 元素的样式
【发布时间】:2020-07-10 07:30:46
【问题描述】:

我正在使用 react-select 库中的 select 元素,而在我的项目中,我正在使用 styled-components。 我想问我是否可以在我的 styles.ts 文件中设置它的样式。如果不可能的话,你们能提供一些关于如何做造型的建议吗??

在 React:FC 中

import Select from 'react-select'

...

const options = [
    { value: 'Income', label: 'Income' },
    { value: 'Expense', label: 'Expense' },
  ]

...

       <Form>
        <InputElement />
        <Select options={options} />
      </Form>

【问题讨论】:

    标签: reactjs styled-components react-select


    【解决方案1】:

    是的,可以提供您自己的自定义样式,react-select 提供了一种基于对象的方法来设置Select 组件的样式。

    参考docs

    这是一个简单的例子,

    const customStyles = {
      option: (provided, state) => ({
        ...provided,
        borderBottom: '1px dotted pink',
        color: state.isSelected ? 'red' : 'blue',
        padding: 20,
      }),
      control: () => ({
        // none of react-select's styles are passed to <Control />
        width: 200,
      }),
      singleValue: (provided, state) => {
        const opacity = state.isDisabled ? 0.5 : 1;
        const transition = 'opacity 300ms';
    
        return { ...provided, opacity, transition };
      }
    }
    
    const App = () => (
        <Select
          styles={customStyles} // pass the customStyles object to the styles prop
          options={...}
        />
    );
    

    通过提供给自定义style object 的键,Select 是非常可定制的。

    要记住的一点是,每个键都是一个样式函数,其中第一个参数是提供的样式,第二个参数是选择的状态,即isFocusedisSelected

    编辑- 虽然这是使用基于对象的方法提供自定义样式的建议方法。如果你真的想用styled-components 设置Select 组件的样式,这里有一个例子(你必须通过prop 和基于classNames 的样式提供classNamePrefix

    更多关于使用 classNames docs 进行样式设置的详细信息

    import Select from 'react-select';
    import styled from 'styled-components';
    
    const SelectElement = styled(Select)`
      .react-select-container {
        // custom styles
      }
    
      .react-select__control {
        // custom styles
      }
    
      .react-select__indicators {
        // custom styles
      }
    `;
    
    const MySelect = (props) => {
      return (
         <SelectElement classNamePrefix="react-select" options={options} {...props}/>
      )
    }
    

    【讨论】:

    • OP 指定他们想要使用样式化组件
    • 是的,我理解,但不幸的是,选择由多个单独的组件和通过基于对象的方法建议的样式方式组成。除非你想去className打猎。
    • 您不能只做ReactSelectStyled = styled(ReactSelect) 并期望一切正常。见docs
    • 我不是在争论您的方法是否更好,我只是说这不是 OP 所要求的,是的,您可以这样做 ReactSelectStyled = styled(ReactSelect) 并希望一切正常,如果您知道类名,干杯!
    • @ludwiguer 你去吧,用styled-components更新了样式的答案
    【解决方案2】:

    是的,你可以这样做

    import ReactSelect from 'react-select';
    import styled from 'styled-components';
    
    const ReactSelectStyled = styled(ReactSelect)`
       // you have to provide custom styles through class names
       // example
       .react-select__option {
          // custom styles
       }
    `;
    

    此外,您可以通过 classNamePrefixSelect 组件提供前缀来更改类名的前缀(即类名中的“react-select”部分)。

    【讨论】:

    • 使用这种方法,您必须去className 寻找并修改className 的样式。 react-select 提供自定义样式的 API。见docs
    • OP 询问是否可以使用 styled-components 来完成,答案是肯定的,这就是方法,即使您需要进行类名搜索
    • 请更新您的答案,提供更多关于具有类名的自定义样式的详细信息,因为在使用 styled-components 设置 Select 组件的样式时,绝对有必要获得所需的结果。
    • 我已经编辑了一些细节,如果看起来不适合你,你可以再做一次编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 2018-07-30
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    相关资源
    最近更新 更多