清除从外部操作中选择的多个选择字段值



我正在使用 https://github.com/JedWatson/react-select 在UI中显示一组单选/多选列表。现在我需要一次清除所有字段值。

在文档中,有一些选项可以在元素 - Clearable 属性中添加一个清除按钮。但是我想从容器组件级别的元素外部调用它,例如使用 redux 状态。

组件结构如下:

  renderSelectFilters() {
    const { filters } = this.props;
    return (
      filters.map((filter) => {
        if (filter.type == 'Checkbox' || filter.type == 'Select') {
          return (
            <SelectContainer
              key={filter.name}
              name={filter.name}
              options={filter.options}
              multi={filter.type == 'Checkbox' ? true : false}
              handleChange={this.handleChange}
              clearValues={this.clearValues}
              searchable={filter.type == 'Checkbox' ? true : false}
            />
            );
        }
      })
    )
  }
  clearFilters() {
      //stuff here
  }
  renderClearFiltersButton = () => { 
    return ( 
      <Button 
        text={'Clear Filters'} 
        onClick={this.clearFilters} 
      /> 
    ) 
  } 
   render() {
    return (
      <div className="filters-bar">
        <div className="some-class">
          {this.renderSelectFilters()}
          {this.renderClearFiltersButton()}
        </div>
      </div>
    )
  }

我已经检查了这个解决方案在保留过滤器的同时选择清除值,但它是关于设置现有值而不是完全删除该值。

我会将 react-select 值与 redux 同步,这样当您清除 redux 中的值时,它会在 react-select 时自动清除。

最新更新