【问题标题】:React-Select, Multi Select and Text OverflowReact-Select、多选和文本溢出
【发布时间】:2016-02-02 15:44:54
【问题描述】:

我正在使用带有多选功能的 React-Select 组件。我面临的一个问题是,如果用户选择 3 或 4 个选项,UI 看起来很糟糕,因为文本开始溢出,这会导致组件水平和垂直增长。

我希望组件的大小保持不变,如果用户选择更多选项,那么它只会显示“...”(省略号),而不是尝试显示新选择的选项。

我想要的行为更符合这个组件

http://instructure-react.github.io/react-select-box/

看看它如何处理多选。

我现在不想更换组件,因为我们已经使用 React-Select 进行了大量测试。

你能给我一些指导方针,告诉我如何在不删除 react-select 的情况下实现这一点。

【问题讨论】:

  • 您应该添加一些 css 以将 react-select 限制在其父容器中。例如max-width:100% 并为其父容器提供一些宽度。
  • 我不知道应该添加什么 CSS 来获得所需的行为。这个想法是,与其无限增长,不如只在选择 3 个(或 x)项后显示省略号。
  • @Knows Not Much 有什么解决方案吗?

标签: reactjs react-select


【解决方案1】:

我已经成功实现了省略号效果并将显示保留在一行, 这是一个工作示例 https://codesandbox.io/s/v638kx67w7 希望这会有所帮助

【讨论】:

  • 当您有更长的标签超出框并且您再次单击插入符号时,值容器的全部内容将向左移动。不知何故,在文本右侧创建了一个新的输入元素。你能解决这个问题吗?
  • @GregHolst 我通过设置 searchable={true} 然后提供只呈现 null (Input: () => null) 的 Input 组件来解决此问题,因此它不会被添加到文本的右侧。虽然这与 caviats 一起提供,但您可以解决它。
【解决方案2】:

我解决了这个问题而没有像这样丢失Input 组件;

import Select, { components as RSComponents } from "react-select";
   

const ValueContainer = ({ selectProps, children, ...props }) => {
    let [values, input] = children;

    if (Array.isArray(values)) {
      values = selectProps.value.map((x) => x.label).join(', ');
    }

    return (
      <RSComponents.ValueContainer {...props}>
        <div style={{
          maxWidth: "80%",
          whiteSpace: "nowrap",
          textOverflow: "ellipsis",
          overflow: "hidden",
        }}>
          {values}
         </div>
        {input}
      </RSComponents.ValueContainer>
    );
};

const customStyles = useMemo(() => ({
    valueContainer: (provided, state) => ({
        ...provided,
        whiteSpace: "nowrap",
        overflow: "hidden",
        flexWrap: 'nowrap',
    }),
    input: (provided, state) => ({
        ...provided,
        minWidth: '20%'
    }),
}), []);

<Select
    components={{ ValueContainer }}
    isMulti
    styles={customStyles}
    ...
/>

【讨论】:

    【解决方案3】:

    这是为给定的 react-select 元素生成的 Html

    . react-select-box-container {
      position: relative;
      width: 240px;
      display: inline-block;
      background-color: #fff;
      border-radius: 4px;
      text-align: left;
      box-shadow: 0 0 2px rgba(0, 0, 0, .3);
    }
    
    .react-select-box {
      padding: 15px 0;
      display: inline-block;
      cursor: pointer;
      border: none;
      width: 100%;
      text-align: left;
      background-color: transparent;
    }
    
    .react-select-box:focus {
      outline: 0;
      box-shadow: 0 0 4px #0493D1;
    }
    
    .react-select-box:before {
      content: ' ';
      z-index: 1;
      position: absolute;
      height: 20px;
      top: 15px;
      right: 34px;
      border-left: 1px solid #CBD2D7;
    }
    
    .react-select-box:after {
      content: ' ';
      position: absolute;
      z-index: 1;
      top: 23px;
      right: 13px;
      border-top: 6px solid #7B8E9B;
      border-left: 5px solid transparent;
      border-right: 5px solid transparent;
    }
    
    .react-select-box-label,
    .react-select-box-option {
      line-height: 16px;
      font-size: 12px;
      font-weight: bold;
      color: #7B8E9B;
    }
    
    .react-select-box-label {
      padding: 0 40px 0 20px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      color: #0493D1;
    }
    
    .react-select-box-empty .react-select-box-label {
      color: #7B8E9B;
    }
    
    .react-select-box-click-outside-layer {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      z-index: 2;
    }
    
    .react-select-box-clear {
      position: absolute;
      top: 15px;
      right: 0;
      width: 35px;
      height: 20px;
      background-color: #fff;
      text-indent: -9999em;
      z-index: 3;
      border: none;
    }
    
    .react-select-box-clear:before {
      content: '×';
      position: absolute;
      top: 2px;
      left: 10px;
      z-index: 1;
      background-color: #7B8E9B;
      border-radius: 100%;
      font-size: 13px;
      color: #fff;
      line-height: 1;
      width: 15px;
      height: 15px;
      text-indent: 0;
      text-align: center;
    }
    
    .react-select-box-clear:hover,
    .react-select-box-clear:focus {
      outline: 0;
    }
    
    .react-select-box-clear:hover:before,
    .react-select-box-clear:focus:before {
      background-color: #0493D1;
    }
    
    .react-select-box-hidden {
      display: none
    }
    
    .react-select-box-options {
      margin: 2px 0 0;
      position: absolute;
      padding: 10px 0;
      width: 240px;
      top: 100%;
      left: 0;
      z-index: 4;
      background-color: #fff;
      border-radius: 4px;
      box-shadow: 0 0 2px rgba(0, 0, 0, .3);
    }
    
    .react-select-box-options-list {
      list-style: none outside;
      margin: 0;
      padding: 0;
    }
    
    .react-select-box-option {
      padding: 10px 20px;
      margin: 0;
      cursor: pointer;
      display: block;
      line-height: 1.2;
      text-decoration: none;
    }
    
    .react-select-box-option:hover {
      color: #0493D1;
      background-color: #f4f4f4;
    }
    
    .react-select-box-option-selected {
      color: #CBD2D7;
    }
    
    .react-select-box-multi .react-select-box-option {
      padding-left: 42px;
      position: relative;
    }
    
    .react-select-box-multi .react-select-box-option:before {
      content: ' ';
      position: absolute;
      line-height: 1;
      text-align: center;
      left: 20px;
      top: 9px;
      border-radius: 3px;
      height: 12px;
      width: 12px;
      margin-right: 10px;
      border: 1px solid #7B8E9B;
      background: #f9f9f9;
      vertical-align: middle;
    }
    
    .react-select-box-multi .react-select-box-option-selected:before {
      content: '✓';
    }
    
    .react-select-box-multi .react-select-box-option-selected {
      color: #1F3344;
    }
    
    .react-select-box-option:focus,
    .react-select-box-option-focused {
      color: #0493D1;
      outline: 0;
      background-color: #DDE2E5;
    }
    
    .react-select-box-close {
      color: #0493D1;
      text-transform: uppercase;
      background-color: transparent;
      border: none;
      padding: 5px 0;
      display: block;
      text-align: center;
      width: 100%;
      font-weight: bold;
      cursor: pointer;
      outline: none;
    }
    
    .react-select-box-close:hover,
    .react-select-box-close:focus {
      text-decoration: underline;
    }
    
    .react-select-box-empty .react-select-box-close {
      color: #CBD2D7;
    }
    
    .react-select-box-native {
      position: absolute;
      left: -99999em;
    }
    <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>
    <div class="react-select-box-container react-select-box-multi react-select-box-empty">
      <button id="react-select-box-2" class="react-select-box" tabindex="0" aria-hidden="true">
            <div class="react-select-box-label">
              Favorite Colors
            </div></button>
    
      <div class="react-select-box-options react-select-box-hidden" aria-hidden="true" tabindex="0">
        <div class="react-select-box-off-screen">
          <a id="react-select-box-2-0" href="#" class="react-select-box-option" tabindex="-1">Red</a>
          <a id="react-select-box-2-1" href="#" class="react-select-box-option" tabindex="-1">Green</a>
          <a id="react-select-box-2-2" href="#" class="react-select-box-option" tabindex="-1">Blue</a>
        </div>
        <button class="react-select-box-close">Close</button>
      </div>
    
      <div class="react-select-box-native">
        <label for="react-select-box-2-native-select">Favorite Colors</label>
        <select id="react-select-box-2-native-select" multiple="multiple">
                <option value="red">
                  Red
                </option>
        
                <option value="green">
                  Green
                </option>
        
                <option value="blue">
                  Blue
                </option>
              </select>
      </div>
    </div>

    【讨论】:

    • 让我试试这个
    • 不会改变我的行为
    • @ryan4888 然后使用一些内联 css 样式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多