index.tsx 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /* eslint-disable max-len */
  2. /* eslint-disable max-lines-per-function */
  3. import React, { Fragment, MouseEvent, ReactInstance } from 'react';
  4. import ReactDOM from 'react-dom';
  5. import cls from 'classnames';
  6. import PropTypes from 'prop-types';
  7. import ConfigContext, { ContextValue } from '../configProvider/context';
  8. import SelectFoundation, { SelectAdapter } from '@douyinfe/semi-foundation/select/foundation';
  9. import { cssClasses, strings, numbers } from '@douyinfe/semi-foundation/select/constants';
  10. import BaseComponent, { ValidateStatus } from '../_base/baseComponent';
  11. import { isEqual, isString, noop, get, isNumber } from 'lodash';
  12. import Tag from '../tag/index';
  13. import TagGroup from '../tag/group';
  14. import LocaleConsumer from '../locale/localeConsumer';
  15. import Popover from '../popover/index';
  16. import { numbers as popoverNumbers } from '@douyinfe/semi-foundation/popover/constants';
  17. import { FixedSizeList as List } from 'react-window';
  18. import { getOptionsFromGroup } from './utils';
  19. import VirtualRow from './virtualRow';
  20. import Input, { InputProps } from '../input/index';
  21. import Option, { OptionProps } from './option';
  22. import OptionGroup from './optionGroup';
  23. import Spin from '../spin';
  24. import Trigger from '../trigger';
  25. import { IconChevronDown, IconClear } from '@douyinfe/semi-icons';
  26. import { isSemiIcon, getFocusableElements, getActiveElement } from '../_utils';
  27. import { Subtract } from 'utility-types';
  28. import warning from '@douyinfe/semi-foundation/utils/warning';
  29. import { getUuidShort } from '@douyinfe/semi-foundation/utils/uuid';
  30. import '@douyinfe/semi-foundation/select/select.scss';
  31. import { Locale } from '../locale/interface';
  32. import { Position, TooltipProps } from '../tooltip';
  33. export { OptionProps } from './option';
  34. export { OptionGroupProps } from './optionGroup';
  35. export { VirtualRowProps } from './virtualRow';
  36. const prefixcls = cssClasses.PREFIX;
  37. const key = 0;
  38. type ExcludeInputType = {
  39. value?: InputProps['value'];
  40. onFocus?: InputProps['onFocus'];
  41. onChange?: InputProps['onChange'];
  42. }
  43. type OnChangeValueType = string | number | Record<string, any>;
  44. export interface optionRenderProps {
  45. key?: any;
  46. label?: React.ReactNode;
  47. value?: string | number;
  48. style?: React.CSSProperties;
  49. className?: string;
  50. selected?: boolean;
  51. focused?: boolean;
  52. show?: boolean;
  53. disabled?: boolean;
  54. onMouseEnter?: (e: React.MouseEvent) => any;
  55. onClick?: (e: React.MouseEvent) => any;
  56. [x: string]: any;
  57. }
  58. export interface selectMethod {
  59. clearInput?: () => void;
  60. selectAll?: () => void;
  61. deselectAll?: () => void;
  62. focus?: () => void;
  63. close?: () => void;
  64. open?: () => void;
  65. }
  66. export type SelectSize = 'small' | 'large' | 'default';
  67. export interface virtualListProps {
  68. itemSize?: number;
  69. height?: number;
  70. width?: string | number;
  71. }
  72. export type RenderSingleSelectedItemFn = (optionNode: Record<string, any>) => React.ReactNode;
  73. export type RenderMultipleSelectedItemFn = (optionNode: Record<string, any>, multipleProps: { index: number; disabled: boolean; onClose: (tagContent: React.ReactNode, e: MouseEvent) => void }) => { isRenderInTag: boolean; content: React.ReactNode };
  74. export type RenderSelectedItemFn = RenderSingleSelectedItemFn | RenderMultipleSelectedItemFn;
  75. export type SelectProps = {
  76. 'aria-describedby'?: React.AriaAttributes['aria-describedby'];
  77. 'aria-errormessage'?: React.AriaAttributes['aria-errormessage'];
  78. 'aria-invalid'?: React.AriaAttributes['aria-invalid'];
  79. 'aria-labelledby'?: React.AriaAttributes['aria-labelledby'];
  80. 'aria-required'?: React.AriaAttributes['aria-required'];
  81. id?: string;
  82. autoFocus?: boolean;
  83. autoClearSearchValue?: boolean;
  84. arrowIcon?: React.ReactNode;
  85. defaultValue?: string | number | any[] | Record<string, any>;
  86. value?: string | number | any[] | Record<string, any>;
  87. placeholder?: React.ReactNode;
  88. onChange?: (value: SelectProps['value']) => void;
  89. multiple?: boolean;
  90. filter?: boolean | ((inpueValue: string, option: OptionProps) => boolean);
  91. max?: number;
  92. maxTagCount?: number;
  93. maxHeight?: string | number;
  94. style?: React.CSSProperties;
  95. className?: string;
  96. size?: SelectSize;
  97. disabled?: boolean;
  98. emptyContent?: React.ReactNode;
  99. onDropdownVisibleChange?: (visible: boolean) => void;
  100. zIndex?: number;
  101. position?: Position;
  102. onSearch?: (value: string) => void;
  103. dropdownClassName?: string;
  104. dropdownStyle?: React.CSSProperties;
  105. outerTopSlot?: React.ReactNode;
  106. innerTopSlot?: React.ReactNode;
  107. outerBottomSlot?: React.ReactNode;
  108. innerBottomSlot?: React.ReactNode;
  109. optionList?: OptionProps[];
  110. dropdownMatchSelectWidth?: boolean;
  111. loading?: boolean;
  112. defaultOpen?: boolean;
  113. validateStatus?: ValidateStatus;
  114. defaultActiveFirstOption?: boolean;
  115. onChangeWithObject?: boolean;
  116. suffix?: React.ReactNode;
  117. prefix?: React.ReactNode;
  118. insetLabel?: React.ReactNode;
  119. insetLabelId?: string;
  120. inputProps?: Subtract<InputProps, ExcludeInputType>;
  121. showClear?: boolean;
  122. showArrow?: boolean;
  123. renderSelectedItem?: RenderSelectedItemFn;
  124. renderCreateItem?: (inputValue: OptionProps['value'], focus: boolean) => React.ReactNode;
  125. renderOptionItem?: (props: optionRenderProps) => React.ReactNode;
  126. onMouseEnter?: (e: React.MouseEvent) => any;
  127. onMouseLeave?: (e: React.MouseEvent) => any;
  128. clickToHide?: boolean;
  129. onExceed?: (option: OptionProps) => void;
  130. onCreate?: (option: OptionProps) => void;
  131. remote?: boolean;
  132. onDeselect?: (value: SelectProps['value'], option: Record<string, any>) => void;
  133. onSelect?: (value: SelectProps['value'], option: Record<string, any>) => void;
  134. allowCreate?: boolean;
  135. triggerRender?: (props?: any) => React.ReactNode;
  136. onClear?: () => void;
  137. virtualize?: virtualListProps;
  138. onFocus?: (e: React.FocusEvent) => void;
  139. onBlur?: (e: React.FocusEvent) => void;
  140. onListScroll?: (e: React.UIEvent<HTMLDivElement>) => void;
  141. children?: React.ReactNode;
  142. preventScroll?: boolean;
  143. } & Pick<
  144. TooltipProps,
  145. | 'spacing'
  146. | 'getPopupContainer'
  147. | 'motion'
  148. | 'autoAdjustOverflow'
  149. | 'mouseLeaveDelay'
  150. | 'mouseEnterDelay'
  151. | 'stopPropagation'
  152. > & React.RefAttributes<any>;
  153. export interface SelectState {
  154. isOpen: boolean;
  155. isFocus: boolean;
  156. options: Array<OptionProps>;
  157. selections: Map<OptionProps['label'], any>; // A collection of all currently selected items, k: label, v: {value,... otherProps}
  158. dropdownMinWidth: number;
  159. optionKey: number;
  160. inputValue: string;
  161. showInput: boolean;
  162. focusIndex: number;
  163. keyboardEventSet: any; // {}
  164. optionGroups: Array<any>;
  165. isHovering: boolean;
  166. isFocusInContainer: boolean;
  167. }
  168. // 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
  169. class Select extends BaseComponent<SelectProps, SelectState> {
  170. static contextType = ConfigContext;
  171. static Option = Option;
  172. static OptGroup = OptionGroup;
  173. static propTypes = {
  174. 'aria-describedby': PropTypes.string,
  175. 'aria-errormessage': PropTypes.string,
  176. 'aria-invalid': PropTypes.bool,
  177. 'aria-labelledby': PropTypes.string,
  178. 'aria-required': PropTypes.bool,
  179. autoFocus: PropTypes.bool,
  180. autoClearSearchValue: PropTypes.bool,
  181. children: PropTypes.node,
  182. defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.array, PropTypes.object]),
  183. value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.array, PropTypes.object]),
  184. placeholder: PropTypes.node,
  185. onChange: PropTypes.func,
  186. multiple: PropTypes.bool,
  187. // Whether to turn on the input box filtering function, when it is a function, it represents a custom filtering function
  188. filter: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
  189. // How many tags can you choose?
  190. max: PropTypes.number,
  191. // How many tabs are displayed at most, and the rest are displayed in + N
  192. maxTagCount: PropTypes.number,
  193. maxHeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  194. style: PropTypes.object,
  195. className: PropTypes.string,
  196. size: PropTypes.oneOf<SelectProps['size']>(strings.SIZE_SET),
  197. disabled: PropTypes.bool,
  198. emptyContent: PropTypes.node,
  199. onDropdownVisibleChange: PropTypes.func,
  200. zIndex: PropTypes.number,
  201. position: PropTypes.oneOf(strings.POSITION_SET),
  202. onSearch: PropTypes.func,
  203. getPopupContainer: PropTypes.func,
  204. dropdownClassName: PropTypes.string,
  205. dropdownStyle: PropTypes.object,
  206. outerTopSlot: PropTypes.node,
  207. innerTopSlot: PropTypes.node,
  208. inputProps: PropTypes.object,
  209. outerBottomSlot: PropTypes.node,
  210. innerBottomSlot: PropTypes.node, // Options slot
  211. optionList: PropTypes.array,
  212. dropdownMatchSelectWidth: PropTypes.bool,
  213. loading: PropTypes.bool,
  214. defaultOpen: PropTypes.bool,
  215. validateStatus: PropTypes.oneOf(strings.STATUS),
  216. defaultActiveFirstOption: PropTypes.bool,
  217. triggerRender: PropTypes.func,
  218. stopPropagation: PropTypes.bool,
  219. // motion doesn't need to be exposed
  220. motion: PropTypes.oneOfType([PropTypes.func, PropTypes.bool, PropTypes.object]),
  221. onChangeWithObject: PropTypes.bool,
  222. suffix: PropTypes.node,
  223. prefix: PropTypes.node,
  224. insetLabel: PropTypes.node,
  225. insetLabelId: PropTypes.string,
  226. showClear: PropTypes.bool,
  227. showArrow: PropTypes.bool,
  228. renderSelectedItem: PropTypes.func,
  229. allowCreate: PropTypes.bool,
  230. renderCreateItem: PropTypes.func,
  231. onMouseEnter: PropTypes.func,
  232. onMouseLeave: PropTypes.func,
  233. clickToHide: PropTypes.bool,
  234. onExceed: PropTypes.func,
  235. onCreate: PropTypes.func,
  236. remote: PropTypes.bool,
  237. onDeselect: PropTypes.func,
  238. // 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
  239. onSelect: PropTypes.func,
  240. autoAdjustOverflow: PropTypes.bool,
  241. mouseEnterDelay: PropTypes.number,
  242. mouseLeaveDelay: PropTypes.number,
  243. spacing: PropTypes.number,
  244. onBlur: PropTypes.func,
  245. onFocus: PropTypes.func,
  246. onClear: PropTypes.func,
  247. virtualize: PropTypes.object,
  248. renderOptionItem: PropTypes.func,
  249. onListScroll: PropTypes.func,
  250. arrowIcon: PropTypes.node,
  251. preventScroll: PropTypes.bool,
  252. // open: PropTypes.bool,
  253. // tagClosable: PropTypes.bool,
  254. };
  255. static defaultProps: Partial<SelectProps> = {
  256. stopPropagation: true,
  257. motion: true,
  258. zIndex: popoverNumbers.DEFAULT_Z_INDEX,
  259. // position: 'bottomLeft',
  260. filter: false,
  261. multiple: false,
  262. disabled: false,
  263. defaultOpen: false,
  264. allowCreate: false,
  265. placeholder: '',
  266. onDropdownVisibleChange: noop,
  267. onChangeWithObject: false,
  268. onChange: noop,
  269. onSearch: noop,
  270. onMouseEnter: noop,
  271. onMouseLeave: noop,
  272. onDeselect: noop,
  273. onSelect: noop,
  274. onCreate: noop,
  275. onExceed: noop,
  276. onFocus: noop,
  277. onBlur: noop,
  278. onClear: noop,
  279. onListScroll: noop,
  280. maxHeight: 300,
  281. dropdownMatchSelectWidth: true,
  282. defaultActiveFirstOption: true, // In order to meet the needs of A11y, change to true
  283. showArrow: true,
  284. showClear: false,
  285. remote: false,
  286. autoAdjustOverflow: true,
  287. autoClearSearchValue: true,
  288. arrowIcon: <IconChevronDown aria-label='' />
  289. // Radio selection is different from the default renderSelectedItem for multiple selection, so it is not declared here
  290. // renderSelectedItem: (optionNode) => optionNode.label,
  291. // The default creator rendering is related to i18, so it is not declared here
  292. // renderCreateItem: (input) => input
  293. };
  294. inputRef: React.RefObject<HTMLInputElement>;
  295. triggerRef: React.RefObject<HTMLDivElement>;
  296. optionContainerEl: React.RefObject<HTMLDivElement>;
  297. optionsRef: React.RefObject<any>;
  298. virtualizeListRef: React.RefObject<any>;
  299. selectOptionListID: string;
  300. selectID: string;
  301. clickOutsideHandler: (e: MouseEvent) => void;
  302. foundation: SelectFoundation;
  303. context: ContextValue;
  304. constructor(props: SelectProps) {
  305. super(props);
  306. this.state = {
  307. isOpen: false,
  308. isFocus: false,
  309. options: [], // All options
  310. selections: new Map(), // A collection of all currently selected items, k: label, v: {value,... otherProps}
  311. dropdownMinWidth: null,
  312. optionKey: key,
  313. inputValue: '',
  314. showInput: false,
  315. focusIndex: props.defaultActiveFirstOption ? 0 : -1,
  316. keyboardEventSet: {},
  317. optionGroups: [],
  318. isHovering: false,
  319. isFocusInContainer: false,
  320. };
  321. /* Generate random string */
  322. this.selectOptionListID = '';
  323. this.selectID = '';
  324. this.virtualizeListRef = React.createRef();
  325. this.inputRef = React.createRef();
  326. this.triggerRef = React.createRef();
  327. this.optionsRef = React.createRef();
  328. this.optionContainerEl = React.createRef();
  329. this.clickOutsideHandler = null;
  330. this.onSelect = this.onSelect.bind(this);
  331. this.onClear = this.onClear.bind(this);
  332. this.onMouseEnter = this.onMouseEnter.bind(this);
  333. this.onMouseLeave = this.onMouseLeave.bind(this);
  334. this.renderOption = this.renderOption.bind(this);
  335. this.onKeyPress = this.onKeyPress.bind(this);
  336. this.foundation = new SelectFoundation(this.adapter);
  337. warning(
  338. 'optionLabelProp' in this.props,
  339. '[Semi Select] \'optionLabelProp\' has already been deprecated, please use \'renderSelectedItem\' instead.'
  340. );
  341. warning(
  342. 'labelInValue' in this.props,
  343. '[Semi Select] \'labelInValue\' has already been deprecated, please use \'onChangeWithObject\' instead.'
  344. );
  345. }
  346. setOptionContainerEl = (node: HTMLDivElement) => (this.optionContainerEl = { current: node });
  347. get adapter(): SelectAdapter<SelectProps, SelectState> {
  348. const keyboardAdapter = {
  349. registerKeyDown: (cb: () => void) => {
  350. const keyboardEventSet = {
  351. onKeyDown: cb,
  352. };
  353. this.setState({ keyboardEventSet });
  354. },
  355. unregisterKeyDown: () => {
  356. this.setState({ keyboardEventSet: {} });
  357. },
  358. updateFocusIndex: (focusIndex: number) => {
  359. this.setState({ focusIndex });
  360. },
  361. // eslint-disable-next-line @typescript-eslint/no-empty-function
  362. scrollToFocusOption: () => {},
  363. };
  364. const filterAdapter = {
  365. updateInputValue: (value: string) => {
  366. this.setState({ inputValue: value });
  367. },
  368. toggleInputShow: (showInput: boolean, cb: (...args: any) => void) => {
  369. this.setState({ showInput }, () => {
  370. cb();
  371. });
  372. },
  373. focusInput: () => {
  374. const { preventScroll } = this.props;
  375. if (this.inputRef && this.inputRef.current) {
  376. this.inputRef.current.focus({ preventScroll });
  377. }
  378. },
  379. };
  380. const multipleAdapter = {
  381. notifyMaxLimit: (option: OptionProps) => this.props.onExceed(option),
  382. getMaxLimit: () => this.props.max,
  383. registerClickOutsideHandler: (cb: (e: MouseEvent) => void) => {
  384. const clickOutsideHandler: (e: MouseEvent) => void = e => {
  385. const optionInstance = this.optionsRef && this.optionsRef.current;
  386. const triggerDom = (this.triggerRef && this.triggerRef.current) as Element;
  387. // eslint-disable-next-line react/no-find-dom-node
  388. const optionsDom = ReactDOM.findDOMNode(optionInstance as ReactInstance);
  389. // let isInPanel = optionsDom && optionsDom.contains(e.target);
  390. // let isInTrigger = triggerDom && triggerDom.contains(e.target);
  391. if (optionsDom && !optionsDom.contains(e.target as Node) &&
  392. triggerDom && !triggerDom.contains(e.target as Node)) {
  393. cb(e);
  394. }
  395. };
  396. this.clickOutsideHandler = clickOutsideHandler;
  397. document.addEventListener('mousedown', clickOutsideHandler as any, false);
  398. },
  399. unregisterClickOutsideHandler: () => {
  400. if (this.clickOutsideHandler) {
  401. document.removeEventListener('mousedown', this.clickOutsideHandler as any, false);
  402. this.clickOutsideHandler = null;
  403. }
  404. },
  405. rePositionDropdown: () => {
  406. let { optionKey } = this.state;
  407. optionKey = optionKey + 1;
  408. this.setState({ optionKey });
  409. },
  410. notifyDeselect: (value: OptionProps['value'], option: OptionProps) => {
  411. delete option._parentGroup;
  412. this.props.onDeselect(value, option);
  413. },
  414. };
  415. return {
  416. ...super.adapter,
  417. ...keyboardAdapter,
  418. ...filterAdapter,
  419. ...multipleAdapter,
  420. // Collect all subitems, each item is visible by default when collected, and is not selected
  421. getOptionsFromChildren: (children = this.props.children) => {
  422. let optionGroups = [];
  423. let options = [];
  424. const { optionList } = this.props;
  425. if (optionList && optionList.length) {
  426. options = optionList.map((itemOpt, index) => ({
  427. _show: true,
  428. _selected: false,
  429. _scrollIndex: index,
  430. ...itemOpt
  431. }));
  432. optionGroups[0] = { children: options, label: '' };
  433. } else {
  434. const result = getOptionsFromGroup(children);
  435. optionGroups = result.optionGroups;
  436. options = result.options;
  437. }
  438. this.setState({ optionGroups });
  439. return options;
  440. },
  441. updateOptions: (options: OptionProps[]) => {
  442. this.setState({ options });
  443. },
  444. openMenu: () => {
  445. this.setState({ isOpen: true });
  446. },
  447. closeMenu: () => {
  448. this.setState({ isOpen: false });
  449. },
  450. getTriggerWidth: () => {
  451. const el = this.triggerRef.current;
  452. return el && el.getBoundingClientRect().width;
  453. },
  454. setOptionWrapperWidth: (width: number) => {
  455. this.setState({ dropdownMinWidth: width });
  456. },
  457. updateSelection: (selections: Map<OptionProps['label'], any>) => {
  458. this.setState({ selections });
  459. },
  460. // clone Map, important!!!, prevent unexpected modify on state
  461. getSelections: () => new Map(this.state.selections),
  462. notifyChange: (value: OnChangeValueType | OnChangeValueType[]) => {
  463. this.props.onChange(value);
  464. },
  465. notifySelect: (value: OptionProps['value'], option: OptionProps) => {
  466. delete option._parentGroup;
  467. this.props.onSelect(value, option);
  468. },
  469. notifyDropdownVisibleChange: (visible: boolean) => {
  470. this.props.onDropdownVisibleChange(visible);
  471. },
  472. notifySearch: (input: string) => {
  473. this.props.onSearch(input);
  474. },
  475. notifyCreate: (input: OptionProps) => {
  476. this.props.onCreate(input);
  477. },
  478. notifyMouseEnter: (e: React.MouseEvent<HTMLDivElement>) => {
  479. this.props.onMouseEnter(e);
  480. },
  481. notifyMouseLeave: (e: React.MouseEvent<HTMLDivElement>) => {
  482. this.props.onMouseLeave(e);
  483. },
  484. notifyFocus: (event: React.FocusEvent) => {
  485. this.props.onFocus(event);
  486. },
  487. notifyBlur: (event: React.FocusEvent) => {
  488. this.props.onBlur(event);
  489. },
  490. notifyClear: () => {
  491. this.props.onClear();
  492. },
  493. notifyListScroll: (e: React.UIEvent<HTMLDivElement>) => {
  494. this.props.onListScroll(e);
  495. },
  496. updateHovering: (isHovering: boolean) => {
  497. this.setState({ isHovering });
  498. },
  499. updateFocusState: (isFocus: boolean) => {
  500. this.setState({ isFocus });
  501. },
  502. focusTrigger: () => {
  503. try {
  504. const { preventScroll } = this.props;
  505. const el = (this.triggerRef.current) as any;
  506. el.focus({ preventScroll });
  507. } catch (error) {
  508. }
  509. },
  510. getContainer: () => {
  511. return this.optionContainerEl && this.optionContainerEl.current;
  512. },
  513. getFocusableElements: (node: HTMLDivElement) => {
  514. return getFocusableElements(node);
  515. },
  516. getActiveElement: () => {
  517. return getActiveElement();
  518. },
  519. setIsFocusInContainer: (isFocusInContainer: boolean) => {
  520. this.setState({ isFocusInContainer });
  521. },
  522. getIsFocusInContainer: () => {
  523. return this.state.isFocusInContainer;
  524. },
  525. updateScrollTop: (index?: number) => {
  526. // eslint-disable-next-line max-len
  527. let optionClassName = `.${prefixcls}-option-selected`;
  528. if (index !== undefined) {
  529. optionClassName = `.${prefixcls}-option:nth-child(${index})`;
  530. }
  531. let destNode = document.querySelector(`#${prefixcls}-${this.selectOptionListID} ${optionClassName}`) as HTMLDivElement;
  532. if (Array.isArray(destNode)) {
  533. // eslint-disable-next-line prefer-destructuring
  534. destNode = destNode[0];
  535. }
  536. if (destNode) {
  537. /**
  538. * Scroll the first selected item into view.
  539. * The reason why ScrollIntoView is not used here is that it may cause page to move.
  540. */
  541. const destParent = destNode.parentNode as HTMLDivElement;
  542. destParent.scrollTop = destNode.offsetTop -
  543. destParent.offsetTop -
  544. (destParent.clientHeight / 2) +
  545. (destNode.clientHeight / 2);
  546. }
  547. },
  548. };
  549. }
  550. componentDidMount() {
  551. this.foundation.init();
  552. this.selectOptionListID = getUuidShort();
  553. this.selectID = this.props.id || getUuidShort();
  554. }
  555. componentWillUnmount() {
  556. this.foundation.destroy();
  557. }
  558. componentDidUpdate(prevProps: SelectProps, prevState: SelectState) {
  559. const prevChildrenKeys = React.Children.toArray(prevProps.children).map((child: any) => child.key);
  560. const nowChildrenKeys = React.Children.toArray(this.props.children).map((child: any) => child.key);
  561. let isOptionsChanged = false;
  562. if (!isEqual(prevChildrenKeys, nowChildrenKeys) || !isEqual(prevProps.optionList, this.props.optionList)) {
  563. isOptionsChanged = true;
  564. this.foundation.handleOptionListChange();
  565. }
  566. // 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
  567. if (prevProps.value !== this.props.value || isOptionsChanged) {
  568. if ('value' in this.props) {
  569. this.foundation.handleValueChange(this.props.value as any);
  570. } else {
  571. this.foundation.handleOptionListChangeHadDefaultValue();
  572. }
  573. }
  574. }
  575. handleInputChange = (value: string) => this.foundation.handleInputChange(value);
  576. renderInput() {
  577. const { size, multiple, disabled, inputProps, filter } = this.props;
  578. const inputPropsCls = get(inputProps, 'className');
  579. const inputcls = cls(`${prefixcls}-input`, {
  580. [`${prefixcls}-input-single`]: !multiple,
  581. [`${prefixcls}-input-multiple`]: multiple,
  582. }, inputPropsCls);
  583. const { inputValue, focusIndex } = this.state;
  584. const selectInputProps: Record<string, any> = {
  585. value: inputValue,
  586. disabled,
  587. className: inputcls,
  588. onChange: this.handleInputChange,
  589. ...inputProps,
  590. };
  591. let style = {};
  592. // Multiple choice mode
  593. if (multiple) {
  594. style = {
  595. width: inputValue ? `${inputValue.length * 16}px` : '2px',
  596. };
  597. selectInputProps.style = style;
  598. }
  599. return (
  600. <Input
  601. ref={this.inputRef as any}
  602. size={size}
  603. aria-activedescendant={focusIndex !== -1 ? `${this.selectID}-option-${focusIndex}`: ''}
  604. onFocus={(e: React.FocusEvent<HTMLInputElement>) => {
  605. // if multiple and filter, when use tab key to let select get focus
  606. // need to manual update state isFocus to let the focus style take effect
  607. if (multiple && Boolean(filter)){
  608. this.setState({ isFocus: true });
  609. }
  610. // prevent event bubbling which will fire trigger onFocus event
  611. e.stopPropagation();
  612. // e.nativeEvent.stopImmediatePropagation();
  613. }}
  614. onBlur={e => this.foundation.handleInputBlur(e)}
  615. {...selectInputProps}
  616. />
  617. );
  618. }
  619. close() {
  620. this.foundation.close();
  621. }
  622. open() {
  623. this.foundation.open();
  624. }
  625. clearInput() {
  626. this.foundation.clearInput();
  627. }
  628. selectAll() {
  629. this.foundation.selectAll();
  630. }
  631. deselectAll() {
  632. this.foundation.clearSelected();
  633. }
  634. focus() {
  635. this.foundation.focus();
  636. }
  637. onSelect(option: OptionProps, optionIndex: number, e: any) {
  638. this.foundation.onSelect(option, optionIndex, e);
  639. }
  640. onClear(e: React.MouseEvent) {
  641. e.nativeEvent.stopImmediatePropagation();
  642. this.foundation.handleClearClick(e as any);
  643. }
  644. renderEmpty() {
  645. return <Option empty={true} emptyContent={this.props.emptyContent} />;
  646. }
  647. renderLoading() {
  648. const loadingWrapperCls = `${prefixcls}-loading-wrapper`;
  649. return (
  650. <div className={loadingWrapperCls}>
  651. <Spin />
  652. </div>
  653. );
  654. }
  655. renderOption(option: OptionProps, optionIndex: number, style?: React.CSSProperties) {
  656. const { focusIndex, inputValue } = this.state;
  657. const { renderOptionItem } = this.props;
  658. let optionContent;
  659. const isFocused = optionIndex === focusIndex;
  660. let optionStyle = style || {};
  661. if (option.style) {
  662. optionStyle = { ...optionStyle, ...option.style };
  663. }
  664. if (option._inputCreateOnly) {
  665. optionContent = this.renderCreateOption(option, isFocused, optionIndex, style);
  666. } else {
  667. // use another name to make sure that 'key' in optionList still exist when we call onChange
  668. if ('key' in option) {
  669. option._keyInOptionList = option.key;
  670. }
  671. optionContent = (
  672. <Option
  673. showTick
  674. {...option}
  675. selected={option._selected}
  676. onSelect={(v: OptionProps, e: MouseEvent) => this.onSelect(v, optionIndex, e)}
  677. focused={isFocused}
  678. onMouseEnter={() => this.onOptionHover(optionIndex)}
  679. style={optionStyle}
  680. key={option.key || option.label as string + option.value as string + optionIndex}
  681. renderOptionItem={renderOptionItem}
  682. inputValue={inputValue}
  683. id={`${this.selectID}-option-${optionIndex}`}
  684. >
  685. {option.label}
  686. </Option>
  687. );
  688. }
  689. return optionContent;
  690. }
  691. renderCreateOption(option: OptionProps, isFocused: boolean, optionIndex: number, style: React.CSSProperties) {
  692. const { renderCreateItem } = this.props;
  693. // default render method
  694. if (typeof renderCreateItem === 'undefined') {
  695. const defaultCreateItem = (
  696. <Option
  697. key={option.key || option.label as string + option.value as string}
  698. onSelect={(v: OptionProps, e: MouseEvent) => this.onSelect(v, optionIndex, e)}
  699. onMouseEnter={() => this.onOptionHover(optionIndex)}
  700. showTick
  701. {...option}
  702. focused={isFocused}
  703. style={style}
  704. >
  705. <LocaleConsumer<Locale['Select']> componentName="Select" >
  706. {(locale: Locale['Select']) => (
  707. <>
  708. <span className={`${prefixcls}-create-tips`}>{locale.createText}</span>
  709. {option.value}
  710. </>
  711. )}
  712. </LocaleConsumer>
  713. </Option>
  714. );
  715. return defaultCreateItem;
  716. }
  717. const customCreateItem = renderCreateItem(option.value, isFocused);
  718. return (
  719. // eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/interactive-supports-focus
  720. <div
  721. role="button"
  722. aria-label="Use the input box to create an optional item"
  723. onClick={e => this.onSelect(option, optionIndex, e)}
  724. key={option.key || option.label}
  725. >
  726. {customCreateItem}
  727. </div>
  728. );
  729. }
  730. onOptionHover(optionIndex: number) {
  731. this.foundation.handleOptionMouseEnter(optionIndex);
  732. }
  733. renderWithGroup(visibleOptions: OptionProps[]) {
  734. const content: JSX.Element[] = [];
  735. const groupStatus = new Map();
  736. visibleOptions.forEach((option, optionIndex) => {
  737. const parentGroup = option._parentGroup;
  738. const optionContent = this.renderOption(option, optionIndex);
  739. if (parentGroup && !groupStatus.has(parentGroup.label)) {
  740. // when use with OptionGroup and group content not already insert
  741. const groupContent = <OptionGroup {...parentGroup} key={parentGroup.label} />;
  742. groupStatus.set(parentGroup.label, true);
  743. content.push(groupContent);
  744. }
  745. content.push(optionContent);
  746. });
  747. return content;
  748. }
  749. renderVirtualizeList(visibleOptions: OptionProps[]) {
  750. const { virtualize } = this.props;
  751. const { direction } = this.context;
  752. const { height, width, itemSize } = virtualize;
  753. return (
  754. <List
  755. ref={this.virtualizeListRef}
  756. height={height || numbers.LIST_HEIGHT}
  757. itemCount={visibleOptions.length}
  758. itemSize={itemSize}
  759. itemData={{ visibleOptions, renderOption: this.renderOption }}
  760. width={width || '100%'}
  761. style={{ direction }}
  762. >
  763. {VirtualRow}
  764. </List>
  765. );
  766. }
  767. renderOptions(children?: React.ReactNode) {
  768. const { dropdownMinWidth, options, selections } = this.state;
  769. const {
  770. maxHeight,
  771. dropdownClassName,
  772. dropdownStyle,
  773. outerTopSlot,
  774. innerTopSlot,
  775. outerBottomSlot,
  776. innerBottomSlot,
  777. loading,
  778. virtualize,
  779. multiple,
  780. } = this.props;
  781. // Do a filter first, instead of directly judging in forEach, so that the focusIndex can correspond to
  782. const visibleOptions = options.filter(item => item._show);
  783. let listContent: JSX.Element | JSX.Element[] = this.renderWithGroup(visibleOptions);
  784. if (virtualize) {
  785. listContent = this.renderVirtualizeList(visibleOptions);
  786. }
  787. const style = { minWidth: dropdownMinWidth, ...dropdownStyle };
  788. const optionListCls = cls({
  789. [`${prefixcls}-option-list`]: true,
  790. [`${prefixcls}-option-list-chosen`]: selections.size,
  791. });
  792. const isEmpty = !options.length || !options.some(item => item._show);
  793. return (
  794. // eslint-disable-next-line jsx-a11y/no-static-element-interactions
  795. <div
  796. id={`${prefixcls}-${this.selectOptionListID}`}
  797. className={dropdownClassName}
  798. style={style}
  799. ref={this.setOptionContainerEl}
  800. onKeyDown={e => this.foundation.handleContainerKeyDown(e)}
  801. >
  802. {outerTopSlot}
  803. <div
  804. style={{ maxHeight: `${maxHeight}px` }}
  805. className={optionListCls}
  806. role="listbox"
  807. aria-multiselectable={multiple}
  808. onScroll={e => this.foundation.handleListScroll(e)}
  809. >
  810. {innerTopSlot}
  811. {loading ? this.renderLoading() : isEmpty ? this.renderEmpty() : listContent}
  812. {innerBottomSlot}
  813. </div>
  814. {outerBottomSlot}
  815. </div>
  816. );
  817. }
  818. renderSingleSelection(selections: Map<OptionProps['label'], any>, filterable: boolean) {
  819. let { renderSelectedItem } = this.props;
  820. const { placeholder } = this.props;
  821. const { showInput, inputValue } = this.state;
  822. let renderText: React.ReactNode = '';
  823. const selectedItems = [...selections];
  824. if (typeof renderSelectedItem === 'undefined') {
  825. renderSelectedItem = ((optionNode: OptionProps) => optionNode.label) as RenderSelectedItemFn;
  826. }
  827. if (selectedItems.length) {
  828. const selectedItem = selectedItems[0][1];
  829. renderText = (renderSelectedItem as RenderSingleSelectedItemFn)(selectedItem);
  830. }
  831. const spanCls = cls({
  832. [`${prefixcls}-selection-text`]: true,
  833. [`${prefixcls}-selection-placeholder`]: !renderText && renderText !== 0,
  834. [`${prefixcls}-selection-text-hide`]: inputValue && showInput, // show Input
  835. [`${prefixcls}-selection-text-inactive`]: !inputValue && showInput, // Stack Input & RenderText(opacity 0.4)
  836. });
  837. const contentWrapperCls = `${prefixcls}-content-wrapper`;
  838. return (
  839. <>
  840. <div className={contentWrapperCls}>
  841. {
  842. <span className={spanCls} x-semi-prop="placeholder">
  843. {renderText || renderText === 0 ? renderText : placeholder}
  844. </span>
  845. }
  846. {filterable && showInput ? this.renderInput() : null}
  847. </div>
  848. </>
  849. );
  850. }
  851. renderMultipleSelection(selections: Map<OptionProps['label'], any>, filterable: boolean) {
  852. let { renderSelectedItem } = this.props;
  853. const { placeholder, maxTagCount, size } = this.props;
  854. const { inputValue } = this.state;
  855. const selectDisabled = this.props.disabled;
  856. const renderTags = [];
  857. const selectedItems = [...selections];
  858. if (typeof renderSelectedItem === 'undefined') {
  859. renderSelectedItem = (optionNode: OptionProps) => ({
  860. isRenderInTag: true,
  861. content: optionNode.label,
  862. });
  863. }
  864. const mapItems = maxTagCount ? selectedItems.slice(0, maxTagCount) : selectedItems; // no need to render rest tag when maxTagCount is setting
  865. const tags = mapItems.map((item, i) => {
  866. const label = item[0];
  867. const { value } = item[1];
  868. const disabled = item[1].disabled || selectDisabled;
  869. const onClose = (tagContent: React.ReactNode, e: MouseEvent) => {
  870. if (e && typeof e.preventDefault === 'function') {
  871. e.preventDefault(); // make sure that tag will not hidden immediately in controlled mode
  872. }
  873. this.foundation.removeTag({ label, value });
  874. };
  875. const { content, isRenderInTag } = (renderSelectedItem as RenderMultipleSelectedItemFn)(item[1], { index: i, disabled, onClose });
  876. const basic = {
  877. disabled,
  878. closable: !disabled,
  879. onClose,
  880. };
  881. if (isRenderInTag) {
  882. return (
  883. <Tag {...basic} color="white" size={size || 'large'} key={value} tabIndex={-1}>
  884. {content}
  885. </Tag>
  886. );
  887. } else {
  888. return <Fragment key={value}>{content}</Fragment>;
  889. }
  890. });
  891. const contentWrapperCls = cls({
  892. [`${prefixcls}-content-wrapper`]: true,
  893. [`${prefixcls}-content-wrapper-one-line`]: maxTagCount,
  894. [`${prefixcls}-content-wrapper-empty`]: !tags.length,
  895. });
  896. const spanCls = cls({
  897. [`${prefixcls}-selection-text`]: true,
  898. [`${prefixcls}-selection-placeholder`]: !tags.length,
  899. [`${prefixcls}-selection-text-hide`]: tags && tags.length,
  900. // [prefixcls + '-selection-text-inactive']: !inputValue && !tags.length,
  901. });
  902. const placeholderText = placeholder && !inputValue ? <span className={spanCls}>{placeholder}</span> : null;
  903. const n = selectedItems.length > maxTagCount ? maxTagCount : undefined;
  904. const NotOneLine = !maxTagCount; // Multiple lines (that is, do not set maxTagCount), do not use TagGroup, directly traverse with Tag, otherwise Input cannot follow the correct position
  905. const tagContent = NotOneLine ? tags : <TagGroup<"custom"> tagList={tags} maxTagCount={n} restCount={maxTagCount ? selectedItems.length - maxTagCount : undefined} size="large" mode="custom"/>;
  906. return (
  907. <>
  908. <div className={contentWrapperCls}>
  909. {tags && tags.length ? tagContent : placeholderText}
  910. {!filterable ? null : this.renderInput()}
  911. </div>
  912. </>
  913. );
  914. }
  915. onMouseEnter(e: MouseEvent) {
  916. this.foundation.handleMouseEnter(e as any);
  917. }
  918. onMouseLeave(e: MouseEvent) {
  919. this.foundation.handleMouseLeave(e as any);
  920. }
  921. onKeyPress(e: React.KeyboardEvent) {
  922. this.foundation.handleKeyPress(e as any);
  923. }
  924. /* Processing logic when popover visible changes */
  925. handlePopoverVisibleChange(status) {
  926. const { virtualize } = this.props;
  927. const { selections } = this.state;
  928. if (!status) {
  929. return;
  930. }
  931. if (virtualize) {
  932. let minItemIndex = -1;
  933. selections.forEach(item => {
  934. const itemIndex = get(item, '_scrollIndex');
  935. /* When the itemIndex is legal */
  936. if (isNumber(itemIndex) && itemIndex >= 0) {
  937. minItemIndex = minItemIndex !== -1 && minItemIndex < itemIndex
  938. ? minItemIndex
  939. : itemIndex;
  940. }
  941. });
  942. if (minItemIndex !== -1) {
  943. try {
  944. this.virtualizeListRef.current.scrollToItem(minItemIndex, 'center');
  945. } catch (error) { }
  946. }
  947. } else {
  948. this.foundation.updateScrollTop();
  949. }
  950. }
  951. renderSuffix() {
  952. const { suffix } = this.props;
  953. const suffixWrapperCls = cls({
  954. [`${prefixcls}-suffix`]: true,
  955. [`${prefixcls}-suffix-text`]: suffix && isString(suffix),
  956. [`${prefixcls}-suffix-icon`]: isSemiIcon(suffix),
  957. });
  958. return <div className={suffixWrapperCls} x-semi-prop="suffix">{suffix}</div>;
  959. }
  960. renderPrefix() {
  961. const { prefix, insetLabel, insetLabelId } = this.props;
  962. const labelNode = (prefix || insetLabel) as React.ReactElement<any, any>;
  963. const prefixWrapperCls = cls({
  964. [`${prefixcls}-prefix`]: true,
  965. [`${prefixcls}-inset-label`]: insetLabel,
  966. [`${prefixcls}-prefix-text`]: labelNode && isString(labelNode),
  967. [`${prefixcls}-prefix-icon`]: isSemiIcon(labelNode),
  968. });
  969. return (
  970. <div className={prefixWrapperCls} id={insetLabelId} x-semi-prop="prefix,insetLabel">
  971. {labelNode}
  972. </div>
  973. );
  974. }
  975. renderSelection() {
  976. const {
  977. disabled,
  978. multiple,
  979. filter,
  980. style,
  981. id,
  982. size,
  983. className,
  984. validateStatus,
  985. showArrow,
  986. suffix,
  987. prefix,
  988. insetLabel,
  989. placeholder,
  990. triggerRender,
  991. arrowIcon,
  992. } = this.props;
  993. const { selections, isOpen, keyboardEventSet, inputValue, isHovering, isFocus, showInput, focusIndex } = this.state;
  994. const useCustomTrigger = typeof triggerRender === 'function';
  995. const filterable = Boolean(filter); // filter(boolean || function)
  996. const selectionCls = useCustomTrigger ?
  997. cls(className) :
  998. cls(prefixcls, className, {
  999. [`${prefixcls}-open`]: isOpen,
  1000. [`${prefixcls}-focus`]: isFocus,
  1001. [`${prefixcls}-disabled`]: disabled,
  1002. [`${prefixcls}-single`]: !multiple,
  1003. [`${prefixcls}-multiple`]: multiple,
  1004. [`${prefixcls}-filterable`]: filterable,
  1005. [`${prefixcls}-small`]: size === 'small',
  1006. [`${prefixcls}-large`]: size === 'large',
  1007. [`${prefixcls}-error`]: validateStatus === 'error',
  1008. [`${prefixcls}-warning`]: validateStatus === 'warning',
  1009. [`${prefixcls}-no-arrow`]: !showArrow,
  1010. [`${prefixcls}-with-prefix`]: prefix || insetLabel,
  1011. [`${prefixcls}-with-suffix`]: suffix,
  1012. });
  1013. const showClear = this.props.showClear &&
  1014. (selections.size || inputValue) && !disabled && (isHovering || isOpen);
  1015. const arrowContent = showArrow ? (
  1016. <div className={`${prefixcls}-arrow`} x-semi-prop="arrowIcon">
  1017. {arrowIcon}
  1018. </div>
  1019. ) : (
  1020. <div className={`${prefixcls}-arrow-empty`} />
  1021. );
  1022. const inner = useCustomTrigger ? (
  1023. <Trigger
  1024. value={Array.from(selections.values())}
  1025. inputValue={inputValue}
  1026. onChange={this.handleInputChange}
  1027. onClear={this.onClear}
  1028. disabled={disabled}
  1029. triggerRender={triggerRender}
  1030. placeholder={placeholder as any}
  1031. componentName="Select"
  1032. componentProps={{ ...this.props }}
  1033. />
  1034. ) : (
  1035. [
  1036. <Fragment key="prefix">{prefix || insetLabel ? this.renderPrefix() : null}</Fragment>,
  1037. <Fragment key="selection">
  1038. <div className={cls(`${prefixcls}-selection`)}>
  1039. {multiple ?
  1040. this.renderMultipleSelection(selections, filterable) :
  1041. this.renderSingleSelection(selections, filterable)}
  1042. </div>
  1043. </Fragment>,
  1044. <Fragment key="clearicon">
  1045. {/* eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions */}
  1046. {showClear ? ( <div className={cls(`${prefixcls}-clear`)} onClick={this.onClear}><IconClear /></div>) : arrowContent}
  1047. </Fragment>,
  1048. <Fragment key="suffix">{suffix ? this.renderSuffix() : null}</Fragment>,
  1049. ]
  1050. );
  1051. /**
  1052. *
  1053. * In disabled, searchable single-selection and display input, and searchable multi-selection
  1054. * make combobox not focusable by tab key
  1055. *
  1056. * 在disabled,可搜索单选且显示input框,以及可搜索多选情况下
  1057. * 让combobox无法通过tab聚焦
  1058. */
  1059. const tabIndex = (disabled || (filterable && showInput) || (filterable && multiple)) ? -1 : 0;
  1060. return (
  1061. /* eslint-disable-next-line jsx-a11y/aria-activedescendant-has-tabindex */
  1062. <div
  1063. role="combobox"
  1064. aria-disabled={disabled}
  1065. aria-expanded={isOpen}
  1066. aria-controls={`${prefixcls}-${this.selectOptionListID}`}
  1067. aria-haspopup="listbox"
  1068. aria-label={selections.size ? 'selected' : ''} // if there is a value, expect the narration to speak selected
  1069. aria-invalid={this.props['aria-invalid']}
  1070. aria-errormessage={this.props['aria-errormessage']}
  1071. aria-labelledby={this.props['aria-labelledby']}
  1072. aria-describedby={this.props['aria-describedby']}
  1073. aria-required={this.props['aria-required']}
  1074. className={selectionCls}
  1075. ref={ref => ((this.triggerRef as any).current = ref)}
  1076. onClick={e => this.foundation.handleClick(e)}
  1077. style={style}
  1078. id={this.selectID}
  1079. tabIndex={tabIndex}
  1080. aria-activedescendant={focusIndex !== -1 ? `${this.selectID}-option-${focusIndex}`: ''}
  1081. onMouseEnter={this.onMouseEnter}
  1082. onMouseLeave={this.onMouseLeave}
  1083. onFocus={e => this.foundation.handleTriggerFocus(e)}
  1084. onBlur={e => this.foundation.handleTriggerBlur(e as any)}
  1085. onKeyPress={this.onKeyPress}
  1086. {...keyboardEventSet}
  1087. >
  1088. {inner}
  1089. </div>
  1090. );
  1091. }
  1092. render() {
  1093. const { direction } = this.context;
  1094. const defaultPosition = direction === 'rtl' ? 'bottomRight' : 'bottomLeft';
  1095. const {
  1096. children,
  1097. position = defaultPosition,
  1098. zIndex,
  1099. getPopupContainer,
  1100. motion,
  1101. autoAdjustOverflow,
  1102. mouseLeaveDelay,
  1103. mouseEnterDelay,
  1104. spacing,
  1105. stopPropagation,
  1106. } = this.props;
  1107. const { isOpen, optionKey } = this.state;
  1108. const optionList = this.renderOptions(children);
  1109. const selection = this.renderSelection();
  1110. return (
  1111. <Popover
  1112. getPopupContainer={getPopupContainer}
  1113. motion={motion}
  1114. autoAdjustOverflow={autoAdjustOverflow}
  1115. mouseLeaveDelay={mouseLeaveDelay}
  1116. mouseEnterDelay={mouseEnterDelay}
  1117. // transformFromCenter TODO: check no such property
  1118. zIndex={zIndex}
  1119. ref={this.optionsRef}
  1120. content={optionList}
  1121. visible={isOpen}
  1122. trigger="custom"
  1123. rePosKey={optionKey}
  1124. position={position}
  1125. spacing={spacing}
  1126. stopPropagation={stopPropagation}
  1127. disableArrowKeyDown={true}
  1128. onVisibleChange={status => this.handlePopoverVisibleChange(status)}
  1129. >
  1130. {selection}
  1131. </Popover>
  1132. );
  1133. }
  1134. }
  1135. export default Select;