反应选择多个选项,将对象分组为值

时间:2019-09-25 16:21:32

标签: javascript reactjs react-select

因此,我正在尝试将react-select组件添加到我的项目中。我正在使用分组选项,并试图选择多个选项

这是我的组件代码

class QueueFilter extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      options: [
        {
          label: 'Partner',
          options: [
            {
              value: {
                id: 'ABCDSC',
                value: 'Partner1'
              },
              label: 'Partner1'
            },
            {
              value: {
                id: 'ABCDSC',
                value: 'Partner2'
              },
              label: 'Partner2'
            }
          ]
        },
        {
          label: 'Study',
          options: [
            {
              value: {
                id: 'ABCDSC123',
                value: 'Study1'
              },
              label: 'Study1'
            }
          ]
        }
      ],
      selectedFilters: []
    };
    this.fecthQueueFilters = this.fecthQueueFilters.bind(this);
    this.onFilterChange = this.onFilterChange.bind(this);
    this.applyFilter = this.applyFilter.bind(this);
  }

  componentDidMount(prevState, prevProps) {
    if (prevProps.queueId !== this.props.queueId) {
      this.fetchQueueFilters(this.props.queueId);
    }
  }


  onFilterChange(selectedFilters) {
    this.setState({ selectedFilters });
  }

  fecthQueueFilters(queueId) {

  }

  applyFilter() {

  }

  render() {

    const groupStyles = {
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'space-between',
    };

    const groupBadgeStyles = {
      backgroundColor: '#EBECF0',
      borderRadius: '2em',
      color: '#172B4D',
      display: 'inline-block',
      fontSize: 12,
      fontWeight: 'normal',
      lineHeight: '1',
      minWidth: 1,
      padding: '0.16666666666667em 0.5em',
      textAlign: 'center',
    };

    const formatGroupLabel = data => (
      <div style={groupStyles}>
        <span>{data.label}</span>
        <span style={groupBadgeStyles}>{data.options.length}</span>
      </div>
    );

    const Input = (props) => {
      if (props.isHidden) {
        return <components.Input {...props} />;
      }
      return (
        <div style={{ border: '1px dotted black' }}>
          <components.Input {...props} />
        </div>
      );
    };

    return (
      <Container fluid className="main-header">
        <Row>
          <Col xs="1">Queue Filters:</Col>
          <Col auto>
            <Select
              options={this.state.options}
              isMulti
              isClearable
              formatGroupLabel={formatGroupLabel}
              components={{Input}}
              closeMenuOnSelect={false}
              value={this.state.selectedFilters}
              onChange={this.onFilterChange}
            />
          </Col>
          <Col xs="1">
            <Button type="button" onClick={this.applyFilter} color="success">
              <Filter />
            </Button>
          </Col>
        </Row>
      </Container>
    );
  }
}

QueueFilter.propTypes = {
  queueId: PropTypes.string
};

export default QueueFilter;

但是当我选择多个选项时,选择字段中只会显示1个

enter image description here

控制台显示已选择2个选项

enter image description here

而且,控制台中有此警告

enter image description here

如果我将options值更改为简单字符串而不是对象

this.state = {
  options: [
    {
      label: 'Partner',
      options: [
        {
          value: 'ABCDSC:Partner1',
          label: 'Partner1'
        },
        {
          value: 'ABCDSC:Partner2',
          label: 'Partner2'
        }
      ]
    },
    {
      label: 'Study',
      options: [
        {
          value: 'ABCDSC123:Study1',
          label: 'Study1'
        }
      ]
    }
  ],
  selectedFilters: []
};

好吧,它可以按预期工作,但是我宁愿有对象值。

有什么想法要实现吗?

1 个答案:

答案 0 :(得分:1)

您可以使用道具getOptionValue

<Select
  getOptionValue={option => option.value.value}