123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542 |
- import React, { Fragment, MouseEvent, ReactInstance, ReactNode } from 'react';
- import ReactDOM from 'react-dom';
- import cls from 'classnames';
- import PropTypes from 'prop-types';
- import ConfigContext, { ContextValue } from '../configProvider/context';
- import SelectFoundation, { SelectAdapter } from '@douyinfe/semi-foundation/select/foundation';
- import { cssClasses, strings, numbers } from '@douyinfe/semi-foundation/select/constants';
- import BaseComponent, { ValidateStatus } from '../_base/baseComponent';
- import { isEqual, isString, noop, get, isNumber, isFunction } from 'lodash';
- import Tag from '../tag/index';
- import TagGroup from '../tag/group';
- import OverflowList from '../overflowList/index';
- import Space from '../space/index';
- import Text from '../typography/text';
- import LocaleConsumer from '../locale/localeConsumer';
- import Popover, { PopoverProps } from '../popover/index';
- import { numbers as popoverNumbers } from '@douyinfe/semi-foundation/popover/constants';
- import Event from '@douyinfe/semi-foundation/utils/Event';
- import { FixedSizeList as List } from 'react-window';
- import { getOptionsFromGroup } from './utils';
- import VirtualRow from './virtualRow';
- import Input, { InputProps } from '../input/index';
- import Option, { OptionProps } from './option';
- import OptionGroup from './optionGroup';
- import Spin from '../spin';
- import Trigger from '../trigger';
- import { IconChevronDown, IconClear, IconSearch } from '@douyinfe/semi-icons';
- import { isSemiIcon, getFocusableElements, getActiveElement, getDefaultPropsFromGlobalConfig } from '../_utils';
- import { getUuidShort } from '@douyinfe/semi-foundation/utils/uuid';
- import '@douyinfe/semi-foundation/select/select.scss';
- import type { Locale } from '../locale/interface';
- import type { Position, TooltipProps } from '../tooltip';
- import type { Subtract } from 'utility-types';
- export type { OptionProps } from './option';
- export type { OptionGroupProps } from './optionGroup';
- export type { VirtualRowProps } from './virtualRow';
- const prefixcls = cssClasses.PREFIX;
- const key = 0;
- type ExcludeInputType = {
- value?: InputProps['value'];
- onFocus?: InputProps['onFocus'];
- onChange?: InputProps['onChange']
- }
- type OnChangeValueType = string | number | Record<string, any>;
- export interface optionRenderProps {
- key?: any;
- label?: React.ReactNode;
- value?: string | number;
- style?: React.CSSProperties;
- className?: string;
- selected?: boolean;
- focused?: boolean;
- show?: boolean;
- disabled?: boolean;
- onMouseEnter?: (e: React.MouseEvent) => any;
- onClick?: (e: React.MouseEvent) => any;
- [x: string]: any
- }
- export interface SelectedItemProps {
- value: OptionProps['value'];
- label: OptionProps['label'];
- _show?: boolean;
- _selected: boolean;
- _scrollIndex?: number
- }
- export interface TriggerRenderProps {
- value: SelectedItemProps[];
- inputValue: string;
- onSearch: (inputValue: string) => void;
- onClear: () => void;
- onRemove: (option: OptionProps) => void;
- disabled: boolean;
- placeholder: string;
- componentProps: Record<string, any>
- }
- export interface selectMethod {
- clearInput?: () => void;
- selectAll?: () => void;
- deselectAll?: () => void;
- focus?: () => void;
- close?: () => void;
- open?: () => void
- }
- export type SelectSize = 'small' | 'large' | 'default';
- export interface virtualListProps {
- itemSize?: number;
- height?: number;
- width?: string | number
- }
- export type RenderSingleSelectedItemFn = (optionNode: Record<string, any>) => React.ReactNode;
- export type RenderMultipleSelectedItemFn = (optionNode: Record<string, any>, multipleProps: { index: number; disabled: boolean; onClose: (tagContent: React.ReactNode, e: MouseEvent) => void }) => { isRenderInTag: boolean; content: React.ReactNode };
- export type RenderSelectedItemFn = RenderSingleSelectedItemFn | RenderMultipleSelectedItemFn;
- export type SelectProps = {
- 'aria-describedby'?: React.AriaAttributes['aria-describedby'];
- 'aria-errormessage'?: React.AriaAttributes['aria-errormessage'];
- 'aria-invalid'?: React.AriaAttributes['aria-invalid'];
- 'aria-labelledby'?: React.AriaAttributes['aria-labelledby'];
- 'aria-required'?: React.AriaAttributes['aria-required'];
- id?: string;
- autoFocus?: boolean;
- autoClearSearchValue?: boolean;
- arrowIcon?: React.ReactNode;
- borderless?: boolean;
- clearIcon?: React.ReactNode;
- defaultValue?: string | number | any[] | Record<string, any>;
- value?: string | number | any[] | Record<string, any>;
- placeholder?: React.ReactNode;
- onChange?: (value: SelectProps['value']) => void;
- multiple?: boolean;
- filter?: boolean | ((inpueValue: string, option: OptionProps) => boolean);
- max?: number;
- maxTagCount?: number;
- maxHeight?: string | number;
- style?: React.CSSProperties;
- className?: string;
- size?: SelectSize;
- disabled?: boolean;
- emptyContent?: React.ReactNode;
- expandRestTagsOnClick?: boolean;
- onDropdownVisibleChange?: (visible: boolean) => void;
- zIndex?: number;
- position?: Position;
- onSearch?: (value: string, event: React.KeyboardEvent | React.MouseEvent) => void;
- dropdownClassName?: string;
- dropdownStyle?: React.CSSProperties;
- dropdownMargin?: PopoverProps['margin'];
- ellipsisTrigger?: boolean;
- outerTopSlot?: React.ReactNode;
- innerTopSlot?: React.ReactNode;
- outerBottomSlot?: React.ReactNode;
- innerBottomSlot?: React.ReactNode;
- optionList?: OptionProps[];
- dropdownMatchSelectWidth?: boolean;
- loading?: boolean;
- defaultOpen?: boolean;
- validateStatus?: ValidateStatus;
- defaultActiveFirstOption?: boolean;
- onChangeWithObject?: boolean;
- suffix?: React.ReactNode;
- searchPosition?: string;
- searchPlaceholder?: string;
- prefix?: React.ReactNode;
- insetLabel?: React.ReactNode;
- insetLabelId?: string;
- inputProps?: Subtract<InputProps, ExcludeInputType>;
- showClear?: boolean;
- showArrow?: boolean;
- renderSelectedItem?: RenderSelectedItemFn;
- renderCreateItem?: (inputValue: OptionProps['value'], focus: boolean, style?: React.CSSProperties) => React.ReactNode;
- renderOptionItem?: (props: optionRenderProps) => React.ReactNode;
- onMouseEnter?: (e: React.MouseEvent) => any;
- onMouseLeave?: (e: React.MouseEvent) => any;
- clickToHide?: boolean;
- onExceed?: (option: OptionProps) => void;
- onCreate?: (option: OptionProps) => void;
- remote?: boolean;
- onDeselect?: (value: SelectProps['value'], option: Record<string, any>) => void;
- onSelect?: (value: SelectProps['value'], option: Record<string, any>) => void;
- allowCreate?: boolean;
- triggerRender?: (props: TriggerRenderProps) => React.ReactNode;
- onClear?: () => void;
- virtualize?: virtualListProps;
- onFocus?: (e: React.FocusEvent) => void;
- onBlur?: (e: React.FocusEvent) => void;
- onListScroll?: (e: React.UIEvent<HTMLDivElement>) => void;
- children?: React.ReactNode;
- preventScroll?: boolean;
- showRestTagsPopover?: boolean;
- restTagsPopoverProps?: PopoverProps
- } & Pick<
- TooltipProps,
- | 'spacing'
- | 'getPopupContainer'
- | 'motion'
- | 'autoAdjustOverflow'
- | 'mouseLeaveDelay'
- | 'mouseEnterDelay'
- | 'stopPropagation'
- > & React.RefAttributes<any>;
- export interface SelectState {
- isOpen: boolean;
- isFocus: boolean;
- options: Array<OptionProps>;
- selections: Map<OptionProps['label'], any>; // A collection of all currently selected items, k: label, v: {value,... otherProps}
- dropdownMinWidth: number;
- optionKey: number;
- inputValue: string;
- showInput: boolean;
- focusIndex: number;
- keyboardEventSet: any; // {}
- optionGroups: Array<any>;
- isHovering: boolean;
- isFocusInContainer: boolean;
- isFullTags: boolean;
- // The number of really-hidden items when maxTagCount is set
- overflowItemCount: number
- }
- // Notes: Use the label of the option as the identifier, that is, the option in Select, the value is allowed to be the same, but the label must be unique
- class Select extends BaseComponent<SelectProps, SelectState> {
- static contextType = ConfigContext;
- static Option = Option;
- static OptGroup = OptionGroup;
- static propTypes = {
- 'aria-describedby': PropTypes.string,
- 'aria-errormessage': PropTypes.string,
- 'aria-invalid': PropTypes.bool,
- 'aria-labelledby': PropTypes.string,
- 'aria-required': PropTypes.bool,
- autoFocus: PropTypes.bool,
- autoClearSearchValue: PropTypes.bool,
- borderless: PropTypes.bool,
- children: PropTypes.node,
- clearIcon: PropTypes.node,
- defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.array, PropTypes.object]),
- ellipsisTrigger: PropTypes.bool,
- value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.array, PropTypes.object]),
- placeholder: PropTypes.node,
- onChange: PropTypes.func,
- multiple: PropTypes.bool,
- // Whether to turn on the input box filtering function, when it is a function, it represents a custom filtering function
- filter: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
- // How many tags can you choose?
- max: PropTypes.number,
- // How many tabs are displayed at most, and the rest are displayed in + N
- maxTagCount: PropTypes.number,
- maxHeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
- style: PropTypes.object,
- className: PropTypes.string,
- size: PropTypes.oneOf<SelectProps['size']>(strings.SIZE_SET),
- disabled: PropTypes.bool,
- emptyContent: PropTypes.node,
- expandRestTagsOnClick: PropTypes.bool,
- onDropdownVisibleChange: PropTypes.func,
- zIndex: PropTypes.number,
- position: PropTypes.oneOf(strings.POSITION_SET),
- onSearch: PropTypes.func,
- getPopupContainer: PropTypes.func,
- dropdownClassName: PropTypes.string,
- dropdownStyle: PropTypes.object,
- dropdownMargin: PropTypes.oneOfType([PropTypes.number, PropTypes.object]),
- outerTopSlot: PropTypes.node,
- innerTopSlot: PropTypes.node,
- inputProps: PropTypes.object,
- outerBottomSlot: PropTypes.node,
- innerBottomSlot: PropTypes.node, // Options slot
- optionList: PropTypes.array,
- dropdownMatchSelectWidth: PropTypes.bool,
- loading: PropTypes.bool,
- defaultOpen: PropTypes.bool,
- validateStatus: PropTypes.oneOf(strings.STATUS),
- defaultActiveFirstOption: PropTypes.bool,
- triggerRender: PropTypes.func,
- stopPropagation: PropTypes.bool,
- searchPosition: PropTypes.string,
- // motion doesn't need to be exposed
- motion: PropTypes.bool,
- onChangeWithObject: PropTypes.bool,
- suffix: PropTypes.node,
- prefix: PropTypes.node,
- insetLabel: PropTypes.node,
- insetLabelId: PropTypes.string,
- showClear: PropTypes.bool,
- showArrow: PropTypes.bool,
- renderSelectedItem: PropTypes.func,
- allowCreate: PropTypes.bool,
- renderCreateItem: PropTypes.func,
- onMouseEnter: PropTypes.func,
- onMouseLeave: PropTypes.func,
- clickToHide: PropTypes.bool,
- onExceed: PropTypes.func,
- onCreate: PropTypes.func,
- remote: PropTypes.bool,
- onDeselect: PropTypes.func,
- // The main difference between onSelect and onChange is that when multiple selections are selected, onChange contains all options, while onSelect only contains items for the current operation
- onSelect: PropTypes.func,
- autoAdjustOverflow: PropTypes.bool,
- mouseEnterDelay: PropTypes.number,
- mouseLeaveDelay: PropTypes.number,
- spacing: PropTypes.oneOfType([PropTypes.number, PropTypes.object]),
- onBlur: PropTypes.func,
- onFocus: PropTypes.func,
- onClear: PropTypes.func,
- virtualize: PropTypes.object,
- renderOptionItem: PropTypes.func,
- onListScroll: PropTypes.func,
- arrowIcon: PropTypes.node,
- preventScroll: PropTypes.bool,
- // open: PropTypes.bool,
- // tagClosable: PropTypes.bool,
- };
- static __SemiComponentName__ = "Select";
-
- static defaultProps: Partial<SelectProps> = getDefaultPropsFromGlobalConfig(Select.__SemiComponentName__, {
- stopPropagation: true,
- motion: true,
- borderless: false,
- zIndex: popoverNumbers.DEFAULT_Z_INDEX,
- // position: 'bottomLeft',
- filter: false,
- multiple: false,
- disabled: false,
- defaultOpen: false,
- allowCreate: false,
- placeholder: '',
- onDropdownVisibleChange: noop,
- onChangeWithObject: false,
- onChange: noop,
- onSearch: noop,
- onMouseEnter: noop,
- onMouseLeave: noop,
- onDeselect: noop,
- onSelect: noop,
- onCreate: noop,
- onExceed: noop,
- onFocus: noop,
- onBlur: noop,
- onClear: noop,
- onListScroll: noop,
- maxHeight: numbers.LIST_HEIGHT,
- dropdownMatchSelectWidth: true,
- defaultActiveFirstOption: true, // In order to meet the needs of A11y, change to true
- showArrow: true,
- showClear: false,
- searchPosition: strings.SEARCH_POSITION_TRIGGER,
- remote: false,
- autoAdjustOverflow: true,
- autoClearSearchValue: true,
- arrowIcon: <IconChevronDown aria-label='' />,
- showRestTagsPopover: false,
- restTagsPopoverProps: {},
- expandRestTagsOnClick: false,
- ellipsisTrigger: false,
- // Radio selection is different from the default renderSelectedItem for multiple selection, so it is not declared here
- // renderSelectedItem: (optionNode) => optionNode.label,
- // The default creator rendering is related to i18, so it is not declared here
- // renderCreateItem: (input) => input
- })
- inputRef: React.RefObject<HTMLInputElement>;
- dropdownInputRef: React.RefObject<HTMLInputElement>;
- triggerRef: React.RefObject<HTMLDivElement>;
- optionContainerEl: React.RefObject<HTMLDivElement>;
- optionsRef: React.RefObject<any>;
- virtualizeListRef: React.RefObject<any>;
- selectOptionListID: string;
- selectID: string;
- clickOutsideHandler: (e: MouseEvent) => void;
- foundation: SelectFoundation;
- context: ContextValue;
- eventManager: Event;
- constructor(props: SelectProps) {
- super(props);
- this.state = {
- isOpen: false,
- isFocus: false,
- options: [], // All options
- selections: new Map(), // A collection of all currently selected items, k: label, v: {value,... otherProps}
- dropdownMinWidth: null,
- optionKey: key,
- inputValue: '',
- showInput: false,
- focusIndex: props.defaultActiveFirstOption ? 0 : -1,
- keyboardEventSet: {},
- optionGroups: [],
- isHovering: false,
- isFocusInContainer: false,
- isFullTags: false,
- overflowItemCount: 0
- };
- /* Generate random string */
- this.selectOptionListID = '';
- this.selectID = '';
- this.virtualizeListRef = React.createRef();
- this.inputRef = React.createRef();
- this.dropdownInputRef = React.createRef(); // only work when searchPosition = 'dropdown'
- this.triggerRef = React.createRef();
- this.optionsRef = React.createRef();
- this.optionContainerEl = React.createRef();
- this.clickOutsideHandler = null;
- this.onSelect = this.onSelect.bind(this);
- this.onClear = this.onClear.bind(this);
- this.onMouseEnter = this.onMouseEnter.bind(this);
- this.onMouseLeave = this.onMouseLeave.bind(this);
- this.renderOption = this.renderOption.bind(this);
- this.onKeyPress = this.onKeyPress.bind(this);
- this.eventManager = new Event();
- this.foundation = new SelectFoundation(this.adapter);
- }
- setOptionContainerEl = (node: HTMLDivElement) => (this.optionContainerEl = { current: node });
- get adapter(): SelectAdapter<SelectProps, SelectState> {
- const keyboardAdapter = {
- registerKeyDown: (cb: () => void) => {
- const keyboardEventSet = {
- onKeyDown: cb,
- };
- this.setState({ keyboardEventSet });
- },
- unregisterKeyDown: () => {
- this.setState({ keyboardEventSet: {} });
- },
- updateFocusIndex: (focusIndex: number) => {
- this.setState({ focusIndex });
- },
- scrollToFocusOption: () => { },
- };
- const filterAdapter = {
- updateInputValue: (value: string) => {
- this.setState({ inputValue: value });
- },
- toggleInputShow: (showInput: boolean, cb: (...args: any) => void) => {
- this.setState({ showInput }, () => {
- cb();
- });
- },
- focusInput: () => {
- const { preventScroll } = this.props;
- if (this.inputRef && this.inputRef.current) {
- this.inputRef.current.focus({ preventScroll });
- }
- },
- focusDropdownInput: () => {
- const { preventScroll } = this.props;
- if (this.dropdownInputRef && this.dropdownInputRef.current) {
- this.dropdownInputRef.current.focus({ preventScroll });
- }
- }
- };
- const multipleAdapter = {
- notifyMaxLimit: (option: OptionProps) => this.props.onExceed(option),
- getMaxLimit: () => this.props.max,
- registerClickOutsideHandler: (cb: (e: MouseEvent) => void) => {
- const clickOutsideHandler: (e: MouseEvent) => void = e => {
- const optionInstance = this.optionsRef && this.optionsRef.current;
- const triggerDom = (this.triggerRef && this.triggerRef.current) as Element;
- const optionsDom = ReactDOM.findDOMNode(optionInstance as ReactInstance);
- const target = e.target as Element;
- const path = (e as any).composedPath && (e as any).composedPath() || [target];
- if (!(optionsDom && optionsDom.contains(target)) &&
- !(triggerDom && triggerDom.contains(target)) &&
- !(path.includes(triggerDom) || path.includes(optionsDom))
- ) {
- cb(e);
- }
- };
- this.clickOutsideHandler = clickOutsideHandler;
- document.addEventListener('mousedown', clickOutsideHandler as any, false);
- },
- unregisterClickOutsideHandler: () => {
- if (this.clickOutsideHandler) {
- document.removeEventListener('mousedown', this.clickOutsideHandler as any, false);
- this.clickOutsideHandler = null;
- }
- },
- rePositionDropdown: () => {
- let { optionKey } = this.state;
- optionKey = optionKey + 1;
- this.setState({ optionKey });
- },
- notifyDeselect: (value: OptionProps['value'], option: OptionProps) => {
- delete option._parentGroup;
- this.props.onDeselect(value, option);
- },
- };
- return {
- ...super.adapter,
- ...keyboardAdapter,
- ...filterAdapter,
- ...multipleAdapter,
- on: (eventName, eventCallback) => this.eventManager.on(eventName, eventCallback),
- off: (eventName) => this.eventManager.off(eventName),
- once: (eventName, eventCallback) => this.eventManager.once(eventName, eventCallback),
- emit: (eventName) => this.eventManager.emit(eventName),
- // Collect all subitems, each item is visible by default when collected, and is not selected
- getOptionsFromChildren: (children = this.props.children) => {
- let optionGroups = [];
- let options = [];
- const { optionList } = this.props;
- if (optionList && optionList.length) {
- options = optionList.map((itemOpt, index) => ({
- _show: true,
- _selected: false,
- _scrollIndex: index,
- ...itemOpt
- }));
- optionGroups[0] = { children: options, label: '' };
- } else {
- const result = getOptionsFromGroup(children);
- optionGroups = result.optionGroups;
- options = result.options;
- }
- this.setState({ optionGroups });
- return options;
- },
- updateOptions: (options: OptionProps[]) => {
- this.setState({ options });
- },
- openMenu: (cb?: () => void) => {
- this.setState({ isOpen: true }, () => {
- cb?.();
- });
- },
- closeMenu: () => {
- this.setState({ isOpen: false });
- },
- getTriggerWidth: () => {
- const el = this.triggerRef.current;
- return el && el.getBoundingClientRect().width;
- },
- setOptionWrapperWidth: (width: number) => {
- this.setState({ dropdownMinWidth: width });
- },
- updateSelection: (selections: Map<OptionProps['label'], any>) => {
- this.setState({ selections });
- },
- // clone Map, important!!!, prevent unexpected modify on state
- getSelections: () => new Map(this.state.selections),
- notifyChange: (value: OnChangeValueType | OnChangeValueType[]) => {
- this.props.onChange(value);
- },
- notifySelect: (value: OptionProps['value'], option: OptionProps) => {
- delete option._parentGroup;
- this.props.onSelect(value, option);
- },
- notifyDropdownVisibleChange: (visible: boolean) => {
- this.props.onDropdownVisibleChange(visible);
- },
- notifySearch: (input: string, event: React.MouseEvent | React.KeyboardEvent) => {
- this.props.onSearch(input, event);
- },
- notifyCreate: (input: OptionProps) => {
- this.props.onCreate(input);
- },
- notifyMouseEnter: (e: React.MouseEvent<HTMLDivElement>) => {
- this.props.onMouseEnter(e);
- },
- notifyMouseLeave: (e: React.MouseEvent<HTMLDivElement>) => {
- this.props.onMouseLeave(e);
- },
- notifyFocus: (event: React.FocusEvent) => {
- this.props.onFocus(event);
- },
- notifyBlur: (event: React.FocusEvent) => {
- this.props.onBlur(event);
- },
- notifyClear: () => {
- this.props.onClear();
- },
- notifyListScroll: (e: React.UIEvent<HTMLDivElement>) => {
- this.props.onListScroll(e);
- },
- updateHovering: (isHovering: boolean) => {
- this.setState({ isHovering });
- },
- updateFocusState: (isFocus: boolean) => {
- this.setState({ isFocus });
- },
- updateOverflowItemCount: (overflowItemCount: number) => {
- this.setState({ overflowItemCount });
- },
- focusTrigger: () => {
- try {
- const { preventScroll } = this.props;
- const el = (this.triggerRef.current) as any;
- el.focus({ preventScroll });
- } catch (error) {
- }
- },
- getContainer: () => {
- return this.optionContainerEl && this.optionContainerEl.current;
- },
- getFocusableElements: (node: HTMLDivElement) => {
- return getFocusableElements(node);
- },
- getActiveElement: () => {
- return getActiveElement();
- },
- setIsFocusInContainer: (isFocusInContainer: boolean) => {
- this.setState({ isFocusInContainer });
- },
- getIsFocusInContainer: () => {
- return this.state.isFocusInContainer;
- },
- updateScrollTop: (index?: number) => {
- let optionClassName;
- if ('renderOptionItem' in this.props) {
- optionClassName = `.${prefixcls}-option-custom-selected`;
- if (index !== undefined) {
- optionClassName = `.${prefixcls}-option-custom:nth-child(${index + 1})`;
- }
- } else {
- optionClassName = `.${prefixcls}-option-selected`;
- if (index !== undefined) {
- optionClassName = `.${prefixcls}-option:nth-child(${index + 1})`;
- }
- }
- let destNode = document.querySelector(`#${prefixcls}-${this.selectOptionListID} ${optionClassName}`) as HTMLDivElement;
- if (Array.isArray(destNode)) {
- destNode = destNode[0];
- }
- if (destNode) {
- /**
- * Scroll the first selected item into view.
- * The reason why ScrollIntoView is not used here is that it may cause page to move.
- */
- const destParent = destNode.parentNode as HTMLDivElement;
- destParent.scrollTop = destNode.offsetTop -
- destParent.offsetTop -
- (destParent.clientHeight / 2) +
- (destNode.clientHeight / 2);
- }
- },
- };
- }
- componentDidMount() {
- this.foundation.init();
- this.selectOptionListID = getUuidShort();
- this.selectID = this.props.id || getUuidShort();
- }
- componentWillUnmount() {
- this.foundation.destroy();
- }
- componentDidUpdate(prevProps: SelectProps, prevState: SelectState) {
- const prevChildrenKeys = React.Children.toArray(prevProps.children).map((child: any) => child.key);
- const nowChildrenKeys = React.Children.toArray(this.props.children).map((child: any) => child.key);
- let isOptionsChanged = false;
- if (!isEqual(prevChildrenKeys, nowChildrenKeys) || !isEqual(prevProps.optionList, this.props.optionList)) {
- isOptionsChanged = true;
- this.foundation.handleOptionListChange();
- }
- // Add isOptionChanged: There may be cases where the value is unchanged, but the optionList is updated. At this time, the label corresponding to the value may change, and the selected item needs to be updated
- if (!isEqual(this.props.value, prevProps.value) || isOptionsChanged) {
- if ('value' in this.props) {
- this.foundation.handleValueChange(this.props.value as any);
- } else {
- this.foundation.handleOptionListChangeHadDefaultValue();
- }
- }
- }
- handleInputChange = (value: string, event: React.ChangeEvent<HTMLInputElement>) => this.foundation.handleInputChange(value, event);
- renderTriggerInput() {
- const { size, multiple, disabled, inputProps, filter } = this.props;
- const inputPropsCls = get(inputProps, 'className');
- const inputcls = cls(`${prefixcls}-input`, {
- [`${prefixcls}-input-single`]: !multiple,
- [`${prefixcls}-input-multiple`]: multiple,
- }, inputPropsCls);
- const { inputValue, focusIndex } = this.state;
- const selectInputProps: Record<string, any> = {
- value: inputValue,
- disabled,
- className: inputcls,
- onChange: this.handleInputChange,
- ...inputProps,
- };
- let style = {};
- // Multiple choice mode
- if (multiple) {
- style = {
- width: inputValue ? `${inputValue.length * 16}px` : '2px',
- };
- selectInputProps.style = style;
- }
- return (
- <Input
- ref={this.inputRef as any}
- size={size}
- aria-activedescendant={focusIndex !== -1 ? `${this.selectID}-option-${focusIndex}` : ''}
- onFocus={(e: React.FocusEvent<HTMLInputElement>) => {
- // if multiple and filter, when use tab key to let select get focus
- // need to manual update state isFocus to let the focus style take effect
- if (multiple && Boolean(filter)) {
- this.setState({ isFocus: true });
- }
- // prevent event bubbling which will fire trigger onFocus event
- e.stopPropagation();
- // e.nativeEvent.stopImmediatePropagation();
- }}
- onBlur={e => this.foundation.handleInputBlur(e)}
- {...selectInputProps}
- />
- );
- }
- renderDropdownInput() {
- const { size, multiple, disabled, inputProps, filter, searchPosition, searchPlaceholder } = this.props;
- const { inputValue, focusIndex } = this.state;
- const wrapperCls = cls(`${prefixcls}-dropdown-search-wrapper`, {
- });
- const inputPropsCls = get(inputProps, 'className');
- const inputCls = cls(`${prefixcls}-dropdown-input`, {
- [`${prefixcls}-dropdown-input-single`]: !multiple,
- [`${prefixcls}-dropdown-input-multiple`]: multiple,
- }, inputPropsCls);
- const selectInputProps: Record<string, any> = {
- value: inputValue,
- disabled,
- className: inputCls,
- onChange: this.handleInputChange,
- placeholder: searchPlaceholder,
- showClear: true,
- ...inputProps,
- /**
- * When searchPosition is trigger, the keyboard events are bound to the outer trigger div, so there is no need to listen in input.
- * When searchPosition is dropdown, the popup and the outer trigger div are not parent- child relationships,
- * and bubbles cannot occur, so onKeydown needs to be listened in input.
- * */
- onKeyDown: (e) => this.foundation._handleKeyDown(e)
- };
- return (
- <div className={wrapperCls}>
- <Input
- ref={this.dropdownInputRef}
- prefix={<IconSearch></IconSearch>}
- aria-activedescendant={focusIndex !== -1 ? `${this.selectID}-option-${focusIndex}` : ''}
- {...selectInputProps}
- />
- </div>
- );
- }
- close() {
- this.foundation.close();
- }
- open() {
- this.foundation.open();
- }
- clearInput() {
- this.foundation.clearInput();
- }
- selectAll() {
- this.foundation.selectAll();
- }
- deselectAll() {
- this.foundation.clearSelected();
- }
- focus() {
- this.foundation.focus();
- }
- onSelect(option: OptionProps, optionIndex: number, e: any) {
- this.foundation.onSelect(option, optionIndex, e);
- }
- onClear(e: React.MouseEvent) {
- e.nativeEvent.stopImmediatePropagation();
- this.foundation.handleClearClick(e as any);
- }
- search(value: string, event: React.ChangeEvent<HTMLInputElement>) {
- this.handleInputChange(value, event);
- }
- renderEmpty() {
- return <Option empty={true} emptyContent={this.props.emptyContent} />;
- }
- renderLoading() {
- const loadingWrapperCls = `${prefixcls}-loading-wrapper`;
- return (
- <div className={loadingWrapperCls}>
- <Spin />
- </div>
- );
- }
- renderOption(option: OptionProps, optionIndex: number, style?: React.CSSProperties) {
- const { focusIndex, inputValue } = this.state;
- const { renderOptionItem } = this.props;
- let optionContent;
- const isFocused = optionIndex === focusIndex;
- let optionStyle = style || {};
- if (option.style) {
- optionStyle = { ...optionStyle, ...option.style };
- }
- if (option._inputCreateOnly) {
- optionContent = this.renderCreateOption(option, isFocused, optionIndex, style);
- } else {
- // use another name to make sure that 'key' in optionList still exist when we call onChange
- if ('key' in option) {
- option._keyInOptionList = option.key;
- }
- optionContent = (
- <Option
- showTick
- {...option}
- selected={option._selected}
- onSelect={(v: OptionProps, e: MouseEvent) => this.onSelect(v, optionIndex, e)}
- focused={isFocused}
- onMouseEnter={() => this.onOptionHover(optionIndex)}
- style={optionStyle}
- key={option._keyInOptionList || option._keyInJsx || option.label as string + option.value as string + optionIndex}
- renderOptionItem={renderOptionItem}
- inputValue={inputValue}
- semiOptionId={`${this.selectID}-option-${optionIndex}`}
- >
- {option.label}
- </Option>
- );
- }
- return optionContent;
- }
- renderCreateOption(option: OptionProps, isFocused: boolean, optionIndex: number, style: React.CSSProperties) {
- const { renderCreateItem } = this.props;
- // default render method
- if (typeof renderCreateItem === 'undefined') {
- const defaultCreateItem = (
- <Option
- key={option.key || option.label as string + option.value as string}
- onSelect={(v: OptionProps, e: MouseEvent) => this.onSelect(v, optionIndex, e)}
- onMouseEnter={() => this.onOptionHover(optionIndex)}
- showTick
- {...option}
- focused={isFocused}
- style={style}
- >
- <LocaleConsumer<Locale['Select']> componentName="Select" >
- {(locale: Locale['Select']) => (
- <>
- <span className={`${prefixcls}-create-tips`}>{locale.createText}</span>
- {option.value}
- </>
- )}
- </LocaleConsumer>
- </Option>
- );
- return defaultCreateItem;
- }
- const customCreateItem = renderCreateItem(option.value, isFocused, style);
- return (
- // eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/interactive-supports-focus
- <div
- role="button"
- aria-label="Use the input box to create an optional item"
- onClick={e => this.onSelect(option, optionIndex, e)}
- key={option.key || option.label}
- >
- {customCreateItem}
- </div>
- );
- }
- onOptionHover(optionIndex: number) {
- this.foundation.handleOptionMouseEnter(optionIndex);
- }
- renderWithGroup(visibleOptions: OptionProps[]) {
- const content: JSX.Element[] = [];
- const groupStatus = new Map();
- visibleOptions.forEach((option, optionIndex) => {
- const parentGroup = option._parentGroup;
- const optionContent = this.renderOption(option, optionIndex);
- if (parentGroup && !groupStatus.has(parentGroup.label)) {
- const groupKey = typeof parentGroup.label === 'string' || typeof parentGroup.label === 'number'
- ? parentGroup.label
- : parentGroup.key;
- // when use with OptionGroup and group content not already insert
- const groupContent = <OptionGroup {...parentGroup} key={groupKey}/>;
- groupStatus.set(parentGroup.label, true);
- content.push(groupContent);
- }
- content.push(optionContent);
- });
- return content;
- }
- renderVirtualizeList(visibleOptions: OptionProps[]) {
- const { virtualize } = this.props;
- const { direction } = this.context;
- const { height, width, itemSize } = virtualize;
- return (
- <List
- ref={this.virtualizeListRef}
- height={height || numbers.LIST_HEIGHT}
- itemCount={visibleOptions.length}
- itemSize={itemSize}
- itemData={{ visibleOptions, renderOption: this.renderOption }}
- width={width || '100%'}
- style={{ direction }}
- >
- {VirtualRow}
- </List>
- );
- }
- renderOptions(children?: React.ReactNode) {
- const { dropdownMinWidth, options, selections } = this.state;
- const {
- maxHeight,
- dropdownClassName,
- dropdownStyle,
- outerTopSlot,
- innerTopSlot,
- outerBottomSlot,
- innerBottomSlot,
- loading,
- virtualize,
- multiple,
- emptyContent,
- searchPosition,
- filter,
- } = this.props;
- // Do a filter first, instead of directly judging in forEach, so that the focusIndex can correspond to
- const visibleOptions = options.filter(item => item._show);
- let listContent: JSX.Element | JSX.Element[] = this.renderWithGroup(visibleOptions);
- if (virtualize) {
- listContent = this.renderVirtualizeList(visibleOptions);
- }
- const style = { minWidth: dropdownMinWidth, ...dropdownStyle };
- const optionListCls = cls({
- [`${prefixcls}-option-list`]: true,
- [`${prefixcls}-option-list-chosen`]: selections.size,
- });
- const isEmpty = !options.length || !options.some(item => item._show);
- return (
- // eslint-disable-next-line jsx-a11y/no-static-element-interactions
- <div
- id={`${prefixcls}-${this.selectOptionListID}`}
- className={cls({
- // When emptyContent is null and the option is empty, there is no need for the drop-down option for the user,
- // so there is no need to set padding through this className
- [`${prefixcls}-option-list-wrapper`]: !(isEmpty && emptyContent === null),
- }, dropdownClassName)}
- style={style}
- ref={this.setOptionContainerEl}
- onKeyDown={e => this.foundation.handleContainerKeyDown(e)}
- >
- {outerTopSlot ? <div className={`${prefixcls}-option-list-outer-top-slot`} onMouseEnter={() => this.foundation.handleSlotMouseEnter()}>{outerTopSlot}</div> : null}
- {searchPosition === strings.SEARCH_POSITION_DROPDOWN && filter ? this.renderDropdownInput() : null}
- <div
- style={{ maxHeight: `${maxHeight}px` }}
- className={optionListCls}
- role="listbox"
- aria-multiselectable={multiple}
- onScroll={e => this.foundation.handleListScroll(e)}
- >
- {innerTopSlot ? <div className={`${prefixcls}-option-list-inner-top-slot`} onMouseEnter={() => this.foundation.handleSlotMouseEnter()}>{innerTopSlot}</div> : null}
- {loading ? this.renderLoading() : isEmpty ? this.renderEmpty() : listContent}
- {innerBottomSlot ? <div className={`${prefixcls}-option-list-inner-bottom-slot`} onMouseEnter={() => this.foundation.handleSlotMouseEnter()}>{innerBottomSlot}</div> : null}
- </div>
- {outerBottomSlot ? <div className={`${prefixcls}-option-list-outer-bottom-slot`} onMouseEnter={() => this.foundation.handleSlotMouseEnter()}>{outerBottomSlot}</div> : null}
- </div>
- );
- }
- renderSingleSelection(selections: Map<OptionProps['label'], any>, filterable: boolean) {
- let { renderSelectedItem, searchPosition } = this.props;
- const { placeholder } = this.props;
- const { showInput, inputValue } = this.state;
- let renderText: React.ReactNode = '';
- const selectedItems = [...selections];
- if (typeof renderSelectedItem === 'undefined') {
- renderSelectedItem = ((optionNode: OptionProps) => optionNode.label) as RenderSelectedItemFn;
- }
- if (selectedItems.length) {
- const selectedItem = selectedItems[0][1];
- renderText = (renderSelectedItem as RenderSingleSelectedItemFn)(selectedItem);
- }
- const showInputInTrigger = searchPosition === strings.SEARCH_POSITION_TRIGGER;
- const spanCls = cls({
- [`${prefixcls}-selection-text`]: true,
- [`${prefixcls}-selection-placeholder`]: !renderText && renderText !== 0,
- [`${prefixcls}-selection-text-hide`]: inputValue && showInput && showInputInTrigger, // show Input
- [`${prefixcls}-selection-text-inactive`]: !inputValue && showInput && showInputInTrigger, // Stack Input & RenderText(opacity 0.4)
- });
- const contentWrapperCls = `${prefixcls}-content-wrapper`;
- return (
- <>
- <div className={contentWrapperCls}>
- {
- <span className={spanCls} x-semi-prop="placeholder">
- {renderText || renderText === 0 ? renderText : placeholder}
- </span>
- }
- {filterable && showInput && showInputInTrigger ? this.renderTriggerInput() : null}
- </div>
- </>
- );
- }
- getTagItem = (item: any, i: number, renderSelectedItem: RenderSelectedItemFn) => {
- const { size, disabled: selectDisabled } = this.props;
- const label = item[0];
- const { value } = item[1];
- const disabled = item[1].disabled || selectDisabled;
- const onClose = (tagContent: React.ReactNode, e: MouseEvent) => {
- if (e && typeof e.preventDefault === 'function') {
- e.preventDefault(); // make sure that tag will not hidden immediately in controlled mode
- }
- this.foundation.removeTag({ label, value });
- };
- const { content, isRenderInTag } = (renderSelectedItem as RenderMultipleSelectedItemFn)(item[1], { index: i, disabled, onClose });
- const basic = {
- disabled,
- closable: !disabled,
- onClose,
- };
- if (isRenderInTag) {
- return (
- <Tag {...basic} color="white" size={size || 'large'} key={value} tabIndex={-1}>
- {content}
- </Tag>
- );
- } else {
- return <Fragment key={value}>{content}</Fragment>;
- }
- }
- renderTag(item: [React.ReactNode, any], i: number, isCollapseItem?: boolean) {
- const { size, disabled: selectDisabled } = this.props;
- let { renderSelectedItem } = this.props;
- const label = item[0];
- const { value } = item[1];
- const disabled = item[1].disabled || selectDisabled;
- const onClose = (tagContent: React.ReactNode, e: MouseEvent) => {
- if (e && typeof e.preventDefault === 'function') {
- e.preventDefault(); // make sure that tag will not hidden immediately in controlled mode
- }
- this.foundation.removeTag({ label, value });
- };
- if (typeof renderSelectedItem === 'undefined') {
- renderSelectedItem = (optionNode: OptionProps) => ({
- isRenderInTag: true,
- content: optionNode.label,
- });
- }
- const { content, isRenderInTag } = (renderSelectedItem as RenderMultipleSelectedItemFn)(item[1], { index: i, disabled, onClose });
- const basic = {
- disabled,
- closable: !disabled,
- onClose,
- };
- const realContent = isCollapseItem && !isFunction(this.props.renderSelectedItem)
- ? (
- <Text size='small' ellipsis={{ rows: 1, showTooltip: { type: 'popover', opts: { style: { width: 'auto', fontSize: 12 } } } }} >
- {content}
- </Text>
- )
- : content;
- if (isRenderInTag) {
- return (
- <Tag {...basic} color="white" size={size || 'large'} key={value} style={{ maxWidth: '100%' }}>
- {realContent}
- </Tag>
- );
- } else {
- return <Fragment key={value}>{realContent}</Fragment>;
- }
- }
- renderNTag(n: number, restTags: [React.ReactNode, any][]) {
- const { size, showRestTagsPopover, restTagsPopoverProps } = this.props;
- let nTag = (
- <Tag
- closable={false}
- size={size || 'large'}
- color='grey'
- className={`${prefixcls}-content-wrapper-collapse-tag`}
- key={`_+${n}`}
- style={{ marginRight: 0, flexShrink: 0 }}
- >
- +{n}
- </Tag>
- );
- if (showRestTagsPopover) {
- nTag = (
- <Popover
- showArrow
- content={
- <Space spacing={2} wrap style={{ maxWidth: '400px' }}>
- {restTags.map((tag, index) => (this.renderTag(tag, index)))}
- </Space>
- }
- trigger="hover"
- position="top"
- autoAdjustOverflow
- {...restTagsPopoverProps}
- key={`_+${n}_Popover`}
- >
- {nTag}
- </Popover>
- );
- }
- return nTag;
- }
- renderOverflow(items: [React.ReactNode, any][], index: number) {
- const isCollapse = true;
- return items.length && items[0]
- ? this.renderTag(items[0], index, isCollapse)
- : null;
- }
- handleOverflow(items: [React.ReactNode, any][]) {
- const { overflowItemCount, selections } = this.state;
- const { maxTagCount } = this.props;
- const newOverFlowItemCount = selections.size - maxTagCount > 0 ? selections.size - maxTagCount + items.length - 1 : items.length - 1;
- if (overflowItemCount !== newOverFlowItemCount) {
- this.foundation.updateOverflowItemCount(selections.size, newOverFlowItemCount);
- }
- }
- renderCollapsedTags(selections: [React.ReactNode, any][], length: number | undefined): React.ReactElement {
- const { overflowItemCount } = this.state;
- const normalTags = typeof length === 'number' ? selections.slice(0, length) : selections;
- return (
- <div className={`${prefixcls}-content-wrapper-collapse`}>
- <OverflowList
- items={normalTags}
- key={String(selections.length)}
- overflowRenderer={overflowItems => this.renderOverflow(overflowItems as [React.ReactNode, any][], length - 1)}
- onOverflow={overflowItems => this.handleOverflow(overflowItems as [React.ReactNode, any][])}
- visibleItemRenderer={(item, index) => this.renderTag(item as [React.ReactNode, any], index)}
- />
- {overflowItemCount > 0 && this.renderNTag(overflowItemCount, selections.slice(selections.length - overflowItemCount))}
- </div>
- );
- }
- renderOneLineTags(selectedItems: [React.ReactNode, any][], n: number | undefined): React.ReactElement {
- let { renderSelectedItem } = this.props;
- const { showRestTagsPopover, restTagsPopoverProps, maxTagCount } = this.props;
- const { isFullTags } = this.state;
- let tagContent: ReactNode;
- if (typeof renderSelectedItem === 'undefined') {
- renderSelectedItem = (optionNode: OptionProps) => ({
- isRenderInTag: true,
- content: optionNode.label,
- });
- }
- if (showRestTagsPopover) {
- // showRestTagsPopover = true,
- const mapItems = isFullTags ? selectedItems : selectedItems.slice(0, maxTagCount);
- const tags = mapItems.map((item, i) => {
- return this.getTagItem(item, i, renderSelectedItem);
- });
- tagContent = (
- <TagGroup<"custom">
- tagList={tags}
- maxTagCount={n}
- restCount={isFullTags ? undefined : (selectedItems.length - maxTagCount)}
- size="large"
- mode="custom"
- showPopover={showRestTagsPopover}
- popoverProps={restTagsPopoverProps}
- onPlusNMouseEnter={() => {
- this.foundation.updateIsFullTags();
- }}
- />
- );
- } else {
- // If maxTagCount is set, showRestTagsPopover is false/undefined,
- // then there is no popover when hovering, no extra Tags are displayed,
- // only the tags and restCount displayed in the trigger need to be passed in
- const mapItems = selectedItems.slice(0, maxTagCount);
- const tags = mapItems.map((item, i) => {
- return this.getTagItem(item, i, renderSelectedItem);
- });
- tagContent = (
- <TagGroup<"custom">
- tagList={tags}
- maxTagCount={n}
- restCount={selectedItems.length - maxTagCount}
- size="large"
- mode="custom"
- />
- );
- }
- return tagContent;
- }
- renderMultipleSelection(selections: Map<OptionProps['label'], any>, filterable: boolean) {
- let { renderSelectedItem, searchPosition } = this.props;
- const { placeholder, maxTagCount, expandRestTagsOnClick, ellipsisTrigger } = this.props;
- const { inputValue, isOpen } = this.state;
- const selectedItems = [...selections];
- if (typeof renderSelectedItem === 'undefined') {
- renderSelectedItem = (optionNode: OptionProps) => ({
- isRenderInTag: true,
- content: optionNode.label,
- });
- }
- const contentWrapperCls = cls({
- [`${prefixcls}-content-wrapper`]: true,
- [`${prefixcls}-content-wrapper-one-line`]: maxTagCount && !isOpen,
- [`${prefixcls}-content-wrapper-empty`]: !selectedItems.length,
- });
- const spanCls = cls({
- [`${prefixcls}-selection-text`]: true,
- [`${prefixcls}-selection-placeholder`]: !selectedItems.length,
- [`${prefixcls}-selection-text-hide`]: selectedItems && selectedItems.length,
- });
- const placeholderText = placeholder && !inputValue ? <span className={spanCls}>{placeholder}</span> : null;
- const n = selectedItems.length > maxTagCount ? maxTagCount : undefined;
- const NotOneLine = !maxTagCount;
- const oneLineTags = ellipsisTrigger ? this.renderCollapsedTags(selectedItems, n) : this.renderOneLineTags(selectedItems, n);
- const tagContent = NotOneLine || (expandRestTagsOnClick && isOpen)
- ? selectedItems.map((item, i) => this.renderTag(item, i))
- : oneLineTags;
- const showTriggerInput = filterable && searchPosition === strings.SEARCH_POSITION_TRIGGER;
- return (
- <>
- <div className={contentWrapperCls}>
- {selectedItems && selectedItems.length ? tagContent : placeholderText}
- {showTriggerInput ? this.renderTriggerInput() : null}
- </div>
- </>
- );
- }
- onMouseEnter(e: MouseEvent) {
- this.foundation.handleMouseEnter(e as any);
- }
- onMouseLeave(e: MouseEvent) {
- this.foundation.handleMouseLeave(e as any);
- }
- onKeyPress(e: React.KeyboardEvent) {
- this.foundation.handleKeyPress(e as any);
- }
- /* Processing logic when popover visible changes */
- handlePopoverVisibleChange(status) {
- const { virtualize } = this.props;
- const { selections } = this.state;
- if (!status) {
- return;
- }
- if (virtualize) {
- let minItemIndex = -1;
- selections.forEach(item => {
- const itemIndex = get(item, '_scrollIndex');
- /* When the itemIndex is legal */
- if (isNumber(itemIndex) && itemIndex >= 0) {
- minItemIndex = minItemIndex !== -1 && minItemIndex < itemIndex
- ? minItemIndex
- : itemIndex;
- }
- });
- if (minItemIndex !== -1) {
- try {
- this.virtualizeListRef.current.scrollToItem(minItemIndex, 'center');
- } catch (error) { }
- }
- } else {
- this.foundation.updateScrollTop();
- }
- }
- renderSuffix() {
- const { suffix } = this.props;
- const suffixWrapperCls = cls({
- [`${prefixcls}-suffix`]: true,
- [`${prefixcls}-suffix-text`]: suffix && isString(suffix),
- [`${prefixcls}-suffix-icon`]: isSemiIcon(suffix),
- });
- return <div className={suffixWrapperCls} x-semi-prop="suffix">{suffix}</div>;
- }
- renderPrefix() {
- const { prefix, insetLabel, insetLabelId } = this.props;
- const labelNode = (prefix || insetLabel) as React.ReactElement<any, any>;
- const prefixWrapperCls = cls({
- [`${prefixcls}-prefix`]: true,
- [`${prefixcls}-inset-label`]: insetLabel,
- [`${prefixcls}-prefix-text`]: labelNode && isString(labelNode),
- [`${prefixcls}-prefix-icon`]: isSemiIcon(labelNode),
- });
- return (
- <div className={prefixWrapperCls} id={insetLabelId} x-semi-prop="prefix,insetLabel">
- {labelNode}
- </div>
- );
- }
- renderSelection() {
- const {
- disabled,
- multiple,
- filter,
- style,
- id,
- size,
- className,
- validateStatus,
- showArrow,
- suffix,
- prefix,
- insetLabel,
- placeholder,
- triggerRender,
- arrowIcon,
- clearIcon,
- borderless,
- ...rest
- } = this.props;
- const { selections, isOpen, keyboardEventSet, inputValue, isHovering, isFocus, showInput, focusIndex } = this.state;
- const useCustomTrigger = typeof triggerRender === 'function';
- const filterable = Boolean(filter); // filter(boolean || function)
- const selectionCls = useCustomTrigger ?
- cls(className) :
- cls(prefixcls, className, {
- [`${prefixcls}-borderless`]: borderless,
- [`${prefixcls}-open`]: isOpen,
- [`${prefixcls}-focus`]: isFocus,
- [`${prefixcls}-disabled`]: disabled,
- [`${prefixcls}-single`]: !multiple,
- [`${prefixcls}-multiple`]: multiple,
- [`${prefixcls}-filterable`]: filterable,
- [`${prefixcls}-small`]: size === 'small',
- [`${prefixcls}-large`]: size === 'large',
- [`${prefixcls}-error`]: validateStatus === 'error',
- [`${prefixcls}-warning`]: validateStatus === 'warning',
- [`${prefixcls}-no-arrow`]: !showArrow,
- [`${prefixcls}-with-prefix`]: prefix || insetLabel,
- [`${prefixcls}-with-suffix`]: suffix,
- });
- const showClear = this.props.showClear &&
- (selections.size || inputValue) && !disabled && (isHovering || isOpen);
- const arrowContent = showArrow ? (
- <div className={`${prefixcls}-arrow`} x-semi-prop="arrowIcon">
- {arrowIcon}
- </div>
- ) : (
- <div className={`${prefixcls}-arrow-empty`} />
- );
- const clear = clearIcon ? clearIcon : <IconClear />;
- // semantics of onSearch are more in line with behavior, onChange is alias of onSearch, will be deprecate next major version
- const inner = useCustomTrigger ? (
- <Trigger
- value={Array.from(selections.values())}
- inputValue={inputValue}
- onChange={this.handleInputChange}
- onSearch={this.handleInputChange}
- onRemove={(item) => this.foundation.removeTag(item)}
- onClear={this.onClear}
- disabled={disabled}
- triggerRender={triggerRender}
- placeholder={placeholder as any}
- componentName="Select"
- componentProps={{ ...this.props }}
- />
- ) : (
- [
- <Fragment key="prefix">{prefix || insetLabel ? this.renderPrefix() : null}</Fragment>,
- <Fragment key="selection">
- <div className={cls(`${prefixcls}-selection`)}>
- {multiple ?
- this.renderMultipleSelection(selections, filterable) :
- this.renderSingleSelection(selections, filterable)}
- </div>
- </Fragment>,
- <Fragment key="suffix">{suffix ? this.renderSuffix() : null}</Fragment>,
- <Fragment key="clearicon">
- {/* eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions */}
- {showClear ? (<div className={cls(`${prefixcls}-clear`)} onClick={this.onClear}>{clear}</div>) : arrowContent}
- </Fragment>,
- ]
- );
- /**
- *
- * In disabled, searchable single-selection and display input, and searchable multi-selection
- * make combobox not focusable by tab key
- *
- * 在disabled,可搜索单选且显示input框,以及可搜索多选情况下
- * 让combobox无法通过tab聚焦
- */
- const tabIndex = (disabled || (filterable && showInput) || (filterable && multiple)) ? -1 : 0;
- return (
- /* eslint-disable-next-line jsx-a11y/aria-activedescendant-has-tabindex */
- <div
- role="combobox"
- aria-disabled={disabled}
- aria-expanded={isOpen}
- aria-controls={`${prefixcls}-${this.selectOptionListID}`}
- aria-haspopup="listbox"
- aria-label={selections.size ? 'selected' : ''} // if there is a value, expect the narration to speak selected
- aria-invalid={this.props['aria-invalid']}
- aria-errormessage={this.props['aria-errormessage']}
- aria-labelledby={this.props['aria-labelledby']}
- aria-describedby={this.props['aria-describedby']}
- aria-required={this.props['aria-required']}
- className={selectionCls}
- ref={ref => ((this.triggerRef as any).current = ref)}
- onClick={e => this.foundation.handleClick(e)}
- style={style}
- id={this.selectID}
- tabIndex={tabIndex}
- aria-activedescendant={focusIndex !== -1 ? `${this.selectID}-option-${focusIndex}` : ''}
- onMouseEnter={this.onMouseEnter}
- onMouseLeave={this.onMouseLeave}
- onFocus={e => this.foundation.handleTriggerFocus(e)}
- onBlur={e => this.foundation.handleTriggerBlur(e as any)}
- onKeyPress={this.onKeyPress}
- {...keyboardEventSet}
- {...this.getDataAttr(rest)}
- >
- {inner}
- </div>
- );
- }
- render() {
- const { direction } = this.context;
- const defaultPosition = direction === 'rtl' ? 'bottomRight' : 'bottomLeft';
- const {
- children,
- position = defaultPosition,
- zIndex,
- getPopupContainer,
- motion,
- autoAdjustOverflow,
- mouseLeaveDelay,
- mouseEnterDelay,
- spacing,
- stopPropagation,
- dropdownMargin,
- } = this.props;
- const { isOpen, optionKey } = this.state;
- const selection = this.renderSelection();
- return (
- <Popover
- getPopupContainer={getPopupContainer}
- motion={motion}
- margin={dropdownMargin}
- autoAdjustOverflow={autoAdjustOverflow}
- mouseLeaveDelay={mouseLeaveDelay}
- mouseEnterDelay={mouseEnterDelay}
- zIndex={zIndex}
- ref={this.optionsRef}
- content={() => this.renderOptions(children)}
- visible={isOpen}
- trigger="custom"
- rePosKey={optionKey}
- position={position}
- spacing={spacing}
- stopPropagation={stopPropagation}
- disableArrowKeyDown={true}
- onVisibleChange={status => this.handlePopoverVisibleChange(status)}
- afterClose={() => this.foundation.handlePopoverClose()}
- >
- {selection}
- </Popover>
- );
- }
- }
- export default Select;
|