javascript - 如何使用 react-select 呈现 "N items selected"而不是 N 个选定项目的列表

标签 javascript reactjs react-select

我正在考虑使用 react-select作为城市选择器的选择器,用户可以在其中选择一个或多个城市来过滤一些数据。这是在我的页面中呈现的屏幕截图: city selector

城市列表可能很大,如果一次选择大量城市,我不希望选择器超出其蓝色容器。这是我现在模拟时发生的情况:

enter image description here

我不太喜欢它!我能想到的一种替代方法是呈现“选择的 4 个城市”而不是整个列表。这将在页面上具有可预测的大小。

这如何用 react-select 完成?

最佳答案

注意:此答案适用于 react-select v1。查看answer by NearHuscarl v3 的解决方案。

渲染“选择了 N 个项目”

这可以通过 valueRendererclassName 属性以及最少量的 CSS 来实现。

这里我正常显示前三个选择,然后在选择了 4 个以上项目时显示“已选择 N 个项目”。除了“已选择 N 个项目”之外,显示删除选择图标 (×) 是没有意义的,因此我也删除了它(使用 CSS)。

class App extends React.Component {
  state = {
    value: [],
  }
  className = () => {
    const baseClassName = 'my-react-select';
    
    if (this.state.value.length <= 3) {
      return baseClassName;
    }
    
    return `${baseClassName} ${baseClassName}--compact`;
  }
  handleChange = (value) => {
    this.setState({ value });
  }
  renderValue = (option) => {
    // The first three selections are rendered normally
    if (this.state.value.length <= 3) {
      return option.label;
    }

    // With more selections, render "N items selected".
    // Other than the first one are hidden in CSS.
    return <span>{this.state.value.length} items selected</span>;
  }
  render() {
    return (
      <Select
        className={this.className()}
        multi
        onChange={this.handleChange}
        options={[
          { value: 'zero',  label: 'Zero' },
          { value: 'one',   label: 'One' },
          { value: 'two',   label: 'Two' },
          { value: 'three', label: 'Three' },
          { value: 'four',  label: 'Four' },
          { value: 'five',  label: 'Five' },
          { value: 'six',   label: 'Six' },
          { value: 'seven', label: 'Seven' },
          { value: 'eight', label: 'Eight' },
          { value: 'nine',  label: 'Nine' },
        ]}
        value={this.state.value}
        valueRenderer={this.renderValue}
      />
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
.my-react-select {
  /* Custom styles */
}

.my-react-select--compact .Select-value:first-child {
  font-style: italic;
}
.my-react-select--compact .Select-value:first-child .Select-value-icon,
.my-react-select--compact .Select-value:nth-child(n+2) {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<script src="https://unpkg.com/prop-types@15.5.10/prop-types.js"></script>
<script src="https://unpkg.com/classnames@2.2.5/index.js"></script>
<script src="https://unpkg.com/react-input-autosize@2.0.0/dist/react-input-autosize.js"></script>
<script src="https://unpkg.com/react-select@1.3.0/dist/react-select.js"></script>

<link rel="stylesheet" href="https://unpkg.com/react-select@1.3.0/dist/react-select.css">

<div id="root"></div>


替代方法

查看您的屏幕截图,似乎有空间可以显示最多四个选择而不会使选择器溢出。 选择了4个以上的城市时,您可以正常显示前3个选择,然后再显示“+ n”。

  • A市
  • A市,B市
  • A市、B市、C市
  • 城市 A、城市 B、城市 C,以及另外 1 个
  • 城市 A、城市 B、城市 C,以及另外 2 个
  • 城市 A、城市 B、城市 C,以及另外 3 个
  • 等等

从用户体验的 Angular 来看,我认为正常显示前 3 个左右的选择是很好的。如果在选择第 4 个城市后,所有选择突然隐藏在文本“4 items selected”后面,这会让人感到困惑。

这个解决方案与第一个非常相似。 className Prop 现在只是一个字符串。 renderValue 方法和 CSS 选择器有点不同。

class App extends React.Component {
  state = {
    value: [],
  }
  handleChange = (value) => {
    this.setState({ value });
  }
  renderValue = (option) => {
    // The first three values are rendered normally
    if (this.state.value.indexOf(option) < 3) {
      return option.label;
    }

    // Render the rest as "+ N more". 
    // Other than the first one are hidden in CSS.
    return <span>+ {this.state.value.length - 3} more</span>;
  }
  render() {
    return (
      <Select
        className='my-react-select'
        multi
        onChange={this.handleChange}
        options={[
          { value: 'zero',  label: 'Zero' },
          { value: 'one',   label: 'One' },
          { value: 'two',   label: 'Two' },
          { value: 'three', label: 'Three' },
          { value: 'four',  label: 'Four' },
          { value: 'five',  label: 'Five' },
          { value: 'six',   label: 'Six' },
          { value: 'seven', label: 'Seven' },
          { value: 'eight', label: 'Eight' },
          { value: 'nine',  label: 'Nine' },
        ]}
        value={this.state.value}
        valueRenderer={this.renderValue}
      />
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
/* If you change the amount of how many selections are shown normally,
 * be sure to adjust these selectors accordingly. */
.my-react-select .Select-value:nth-child(4) {
  font-style: italic;
}
.my-react-select .Select-value:nth-child(4) .Select-value-icon,
.my-react-select .Select-value:nth-child(n+5) {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<script src="https://unpkg.com/prop-types@15.5.10/prop-types.js"></script>
<script src="https://unpkg.com/classnames@2.2.5/index.js"></script>
<script src="https://unpkg.com/react-input-autosize@2.0.0/dist/react-input-autosize.js"></script>
<script src="https://unpkg.com/react-select@1.3.0/dist/react-select.js"></script>

<link rel="stylesheet" href="https://unpkg.com/react-select@1.3.0/dist/react-select.css">

<div id="root"></div>


这是显示选择的另一种方法:

  • A市
  • A市,B市
  • A市、B市、C市
  • A 市、B 市、C 市、D 市
  • 城市 A、城市 B、城市 C,以及另外 2 个
  • 城市 A、城市 B、城市 C,以及另外 3 个
  • 等等

从 UX 的 Angular 来看,显示“+ 1 more”而不是显示值有点傻,所以在我看来这是最好的选择。

renderValue 方法再次有点不同。 CSS 选择器现在有点丑陋和复杂,但它们可以工作。

class App extends React.Component {
  state = {
    value: [],
  }
  handleChange = (value) => {
    this.setState({ value });
  }
  renderValue = (option) => {
    // The first four values are rendered normally
    if (this.state.value.length <= 4) {
      return option.label;
    }

    // The first 3 values are rendered normally when
    // more than 4 selections have been made
    if (this.state.value.indexOf(option) < 3) {
      return option.label;
    }

    // Render the rest as "+ N more".
    // Other than the first one are hidden in CSS.
    return <span>+ {this.state.value.length - 3} more</span>;
  }
  render() {
    return (
      <Select
        className='my-react-select'
        multi
        onChange={this.handleChange}
        options={[
          { value: 'zero',  label: 'Zero' },
          { value: 'one',   label: 'One' },
          { value: 'two',   label: 'Two' },
          { value: 'three', label: 'Three' },
          { value: 'four',  label: 'Four' },
          { value: 'five',  label: 'Five' },
          { value: 'six',   label: 'Six' },
          { value: 'seven', label: 'Seven' },
          { value: 'eight', label: 'Eight' },
          { value: 'nine',  label: 'Nine' },
        ]}
        value={this.state.value}
        valueRenderer={this.renderValue}
      />
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
/* If you change the amount of how many selections are shown normally,
 * be sure to adjust these selectors accordingly. */
.my-react-select .Select-value:nth-child(4):not(:nth-last-child(2)) {
  font-style: italic;
}
.my-react-select .Select-value:nth-child(4):not(:nth-last-child(2)) .Select-value-icon,
.my-react-select .Select-value:nth-child(n+5) {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<script src="https://unpkg.com/prop-types@15.5.10/prop-types.js"></script>
<script src="https://unpkg.com/classnames@2.2.5/index.js"></script>
<script src="https://unpkg.com/react-input-autosize@2.0.0/dist/react-input-autosize.js"></script>
<script src="https://unpkg.com/react-select@1.3.0/dist/react-select.js"></script>

<link rel="stylesheet" href="https://unpkg.com/react-select@1.3.0/dist/react-select.css">

<div id="root"></div>

关于javascript - 如何使用 react-select 呈现 "N items selected"而不是 N 个选定项目的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48323716/

相关文章:

javascript - 是否可以通过 poi_label 过滤建筑物层?

javascript - 如何获取innerHTML的id值?

javascript - HTML5 围绕它的中心点旋转文本

javascript - 当键不存在时对象键查找速度变慢

javascript - 为什么我们不在 render 方法中编写 axios.get(blabla) 呢?

javascript - SASS child rule setting inside based on parent 下?

reactjs - React 在组件内将 prop 的一部分加粗

reactjs - 引用 React Select

javascript - 有什么方法可以防止 react 选择(Select.Async)在失去焦点时删除搜索输入值?

javascript - 无法更改 react 选择元素的宽度