如何在react-select下拉组件左侧添加自定义图标
react-select 选择框左侧图标实现方案
我尝试在react-select组件中为选择框添加左侧图标,但发现仅使用react-select的默认props无法实现该效果,需要实现的效果参考:
直接在react-select组件根标签内写入图标节点的写法无效,组件不会默认渲染非约定属性的自定义子节点。要实现左侧固定图标效果,需要通过react-select提供的组件自定义能力,重写Control组件注入图标节点。
实现步骤
- 自定义Control包装组件,在组件原生内容前插入左侧图标节点
- 补充对应样式,为左侧图标预留空间,避免图标和选择框内容重叠
- 将自定义Control组件传入
components配置项即可生效
修正后完整代码
import React, { forwardRef } from "react"; import Select, { ClearIndicatorProps, components, DropdownIndicatorProps, ControlProps } from "react-select"; import Icon from "@greenatom/icons"; import { ColourOption } from "../data"; import { COLORS } from "../Pallette/Colors"; import { CustomSelect, IconWrap } from "./style"; import { IProps } from "./types"; const TSelect = [ { value: "chocolate", label: "Chocolate" }, { value: "strawberry", label: "Strawberry" }, { value: "vanilla", label: "Vanilla" } ]; const DropdownIndicator = ( props: DropdownIndicatorProps<ColourOption, true> ) => ( <components.DropdownIndicator {...props}> <Icon nameOfIcon="broken-arrow-down-2" width={20} height={20} fill={COLORS.darkgrayscale1} /> </components.DropdownIndicator> ); const ClearIndicator = (props: ClearIndicatorProps<ColourOption, true>) => ( <components.ClearIndicator {...props}> <Icon nameOfIcon="broken-close-circle" width={16} height={16} /> </components.ClearIndicator> ); // 自定义Control组件,注入左侧图标 const Control = ({ children, icon, ...props }: ControlProps<ColourOption, true> & { icon?: React.ReactNode }) => ( <components.Control {...props}> {icon && <IconWrap className="select-left-icon">{icon}</IconWrap>} {children} </components.Control> ); const SelectButton = forwardRef<HTMLButtonElement, IProps>( ( { children, icon, viewType = "primary", isPressed, isDisabled, className, onClick, ...props }, ref ) => { const handleClick = (event: React.MouseEvent<HTMLButtonElement>): void => { onClick && !isDisabled && onClick(event); }; return ( <CustomSelect classNamePrefix="Select" options={TSelect} isClearable components={{ DropdownIndicator, ClearIndicator, Control: (controlProps) => <Control {...controlProps} icon={icon} /> }} defaultValue={{value: "p2l", label: "P2-l"}} ref={ref} isDisabled={isDisabled} onClick={handleClick} {...props} /> ); } ); SelectButton.displayName = "SelectButton"; export default SelectButton;
配套样式参考
需要给图标和选择框控制区补充基础样式,避免布局错位:
/* 给Control区域左侧预留图标位置 */ .select__control { position: relative; padding-left: 40px; } /* 左侧图标定位 */ .select-left-icon { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); display: flex; align-items: center; justify-content: center; pointer-events: none; /* 避免图标阻挡输入框点击事件 */ }
原代码存在的其他问题:
defaultInputValue仅设置输入框的临时搜索值,要设置默认选中项需要使用defaultValue或value属性;根组件未传入ref和isDisabled等透传属性,会导致对应功能失效。
内容的提问来源于stack exchange,提问作者ToDDiTg
相关产品推荐
相关产品推荐

