如何在react-select中删除栏?

时间:2019-01-04 23:02:30

标签: reactjs user-interface react-select

我正在尝试改善我的UI的反应选择。我在网上做了一些研究,但仍然无法弄清楚如何删除选择栏。

我可以设置控件组件的样式以删除栏吗?怎么样?

import React from 'react';
import chroma from 'chroma-js';

import { colourOptions } from './docs/data';
import Select from 'react-select';

const dot = (color = '#ccc') => ({
  alignItems: 'center',
  display: 'flex',

  ':before': {
    backgroundColor: color,
    borderRadius: 10,
    content: ' ',
    display: 'block',
    marginRight: 8,
    height: 10,
    width: 10,
  },
});

const colourStyles = {
  control: styles => ({ ...styles, backgroundColor: 'white' }),
  option: (styles, { data, isDisabled, isFocused, isSelected }) => {
    const color = chroma(data.color);
    return {
      ...styles,
      backgroundColor: isDisabled
        ? null
        : isSelected ? data.color : isFocused ? color.alpha(0.1).css() : null,
      color: isDisabled
        ? '#ccc'
        : isSelected
          ? chroma.contrast(color, 'white') > 2 ? 'white' : 'black'
          : data.color,
      cursor: isDisabled ? 'not-allowed' : 'default',
    };
  },
  input: styles => ({ ...styles, ...dot() }),
  placeholder: styles => ({ ...styles, ...dot() }),
  singleValue: (styles, { data }) => ({ ...styles, ...dot(data.color) }),
};

export default () => (
  <Select
    defaultValue={colourOptions[2]}
    label="Single select"
    options={colourOptions}
    styles={colourStyles}
  />
);

Edit react-codesandboxer-example

2 个答案:

答案 0 :(得分:4)

react-select允许我们通过做来控制组件

components={{
  IndicatorSeparator: () => null
}}

例如:

<Select
  id="search-commodity"
  options={comodityOptions}
  components={{
    IndicatorSeparator: () => null
  }}
/>

答案 1 :(得分:3)

您要样式化的组件是indicatorSeparator。例如,将其添加到您的样式中:

indicatorSeparator: (styles) => ({display:'none'})

我是怎么发现的?我将classNamePrefix添加到react-select属性中,然后使用检查器查看元素的类名称是什么。