首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React-选择清除和下拉指示器顺序

问React-选择清除和下拉指示器顺序
EN

Stack Overflow用户
提问于 2020-02-18 20:51:01
回答 1查看 237关注 0票数 1

感谢作者的这个库,但我有一个问题:如何更改Clear和Dropdown指示器的顺序(我想:下拉列表优先,清除第二)?

我知道这个指示器在孩子道具中,但是如何改变渲染的顺序呢?或者我如何添加清除后下拉指示器的额外按钮?

EN

回答 1

Stack Overflow用户

发布于 2020-10-07 00:56:32

ClearIndicator和DropdownIndicator都是IndicatorsContainer的子类,所以这将是进行更新的地方。

一种方法是首先呈现一个DropdownIndicator,然后循环遍历IndicatorContainer子容器以找到该DropdownIndicator,并将其删除。

代码语言:javascript
复制
import React, { Children } from "react";
import Select, { components } from "react-select";

const IndicatorsContainer = ({ children, ...props }) => {
  const allowedChildren = Children.map(children, (child) => {
    return (child && child.type.name !== 'DropdownIndicator') ? child : null;
  });

  return (
    <components.IndicatorsContainer {...props}>
      <components.DropdownIndicator />
      {allowedChildren}
    </components.IndicatorsContainer>
  );
};

const CustomSelect = (props) => (
  <Select components={{ IndicatorsContainer }} {...props} />
)

export default CustomSelect;

注意:我在react-select issues页面here中发现了这个问题,其中用户要求将DropdownIndicator和IndicatorSeparator移到输入的左侧,这有点复杂,但有一个有效的codesandbox解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60281317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档