foundation.ts 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  2. import { isNumber, isString, isEqual, omit } from 'lodash';
  3. import KeyCode, { ENTER_KEY } from '../utils/keyCode';
  4. import warning from '../utils/warning';
  5. import isNullOrUndefined from '../utils/isNullOrUndefined';
  6. import { BasicOptionProps } from './optionFoundation';
  7. import isEnterPress from '../utils/isEnterPress';
  8. import { handlePrevent } from '../utils/a11y';
  9. export interface SelectAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  10. getTriggerWidth(): number;
  11. updateFocusState(focus: boolean): void;
  12. focusTrigger(): void;
  13. unregisterClickOutsideHandler(): void;
  14. setOptionWrapperWidth(width: string | number): void;
  15. getOptionsFromChildren(): BasicOptionProps[];
  16. updateOptions(options: BasicOptionProps[]): void;
  17. rePositionDropdown(): void;
  18. updateFocusIndex(index: number): void;
  19. updateSelection(selection: Map<any, any>): void;
  20. openMenu(): void;
  21. notifyDropdownVisibleChange(visible: boolean): void;
  22. registerClickOutsideHandler(event: any): void;
  23. toggleInputShow(show: boolean, cb: () => void): void;
  24. closeMenu(): void;
  25. notifyCreate(option: BasicOptionProps): void;
  26. getMaxLimit(): number;
  27. getSelections(): Map<any, any>;
  28. notifyMaxLimit(arg: BasicOptionProps): void;
  29. notifyClear(): void;
  30. updateInputValue(inputValue: string): void;
  31. focusInput(): void;
  32. notifySearch(inputValue: string, event?: any): void;
  33. registerKeyDown(handler: () => void): void;
  34. unregisterKeyDown(): void;
  35. notifyChange(value: string | BasicOptionProps | (string | BasicOptionProps)[]): void;
  36. notifySelect(value: BasicOptionProps['value'], option: BasicOptionProps): void;
  37. notifyDeselect(value: BasicOptionProps['value'], option: BasicOptionProps): void;
  38. notifyBlur(event: any): void;
  39. notifyFocus(event: any): void;
  40. notifyListScroll(event: any): void;
  41. notifyMouseLeave(event: any): void;
  42. notifyMouseEnter(event: any): void;
  43. updateHovering(isHover: boolean): void;
  44. updateScrollTop(index?: number): void;
  45. updateOverflowItemCount(count: number): void;
  46. getContainer(): any;
  47. getFocusableElements(node: any): any[];
  48. getActiveElement(): any;
  49. setIsFocusInContainer(isFocusInContainer: boolean): void;
  50. getIsFocusInContainer(): boolean;
  51. on(eventName: string, eventCallback: () => void): void;
  52. off(eventName: string): void;
  53. emit(eventName: string): void;
  54. once(eventName: string, eventCallback: () => void): void
  55. }
  56. type LabelValue = string | number;
  57. type PropValue = LabelValue | Record<string, any>;
  58. export default class SelectFoundation extends BaseFoundation<SelectAdapter> {
  59. constructor(adapter: SelectAdapter) {
  60. super({ ...adapter });
  61. }
  62. // keyboard event listner
  63. _keydownHandler: (...arg: any[]) => void | null = null;
  64. init() {
  65. this._setDropdownWidth();
  66. const isDefaultOpen = this.getProp('defaultOpen');
  67. const isOpen = this.getProp('open');
  68. const originalOptions = this._collectOptions();
  69. this._setDefaultSelection(originalOptions);
  70. if (isDefaultOpen || isOpen) {
  71. this.open(undefined, originalOptions);
  72. }
  73. const autoFocus = this.getProp('autoFocus');
  74. if (autoFocus) {
  75. this.focus();
  76. }
  77. }
  78. focus() {
  79. const isFilterable = this._isFilterable();
  80. const isMultiple = this._isMultiple();
  81. this._adapter.updateFocusState(true);
  82. this._adapter.setIsFocusInContainer(false);
  83. if (isFilterable && isMultiple) {
  84. // when filter and multiple, only focus input
  85. this.focusInput();
  86. } else if (isFilterable && !isMultiple) {
  87. // when filter and not multiple, only show input and focus input
  88. this.toggle2SearchInput(true);
  89. } else {
  90. this._focusTrigger();
  91. }
  92. }
  93. _focusTrigger() {
  94. this._adapter.focusTrigger();
  95. // this.bindKeyBoardEvent();
  96. }
  97. destroy() {
  98. this._adapter.unregisterClickOutsideHandler();
  99. // this.unBindKeyBoardEvent();
  100. }
  101. _setDropdownWidth() {
  102. const { style, dropdownMatchSelectWidth } = this.getProps();
  103. let width;
  104. if (dropdownMatchSelectWidth) {
  105. if (style && isNumber(style.width)) {
  106. width = style.width;
  107. } else if (style && isString(style.width) && !style.width.includes('%')) {
  108. width = style.width;
  109. } else {
  110. width = this._adapter.getTriggerWidth();
  111. }
  112. this._adapter.setOptionWrapperWidth(width);
  113. }
  114. }
  115. _collectOptions() {
  116. const originalOptions = this._adapter.getOptionsFromChildren();
  117. this._adapter.updateOptions(originalOptions);
  118. // Reposition the drop-down layer
  119. this._adapter.rePositionDropdown();
  120. return originalOptions;
  121. }
  122. _setDefaultSelection(originalOptions: BasicOptionProps[]) {
  123. let { value } = this.getProps();
  124. const { defaultValue } = this.getProps();
  125. if (this._isControlledComponent()) {
  126. // do nothing
  127. } else {
  128. value = defaultValue;
  129. }
  130. this._update(value, originalOptions);
  131. }
  132. // call when props.optionList change
  133. handleOptionListChange() {
  134. const newOptionList = this._collectOptions();
  135. const { selections } = this.getStates();
  136. this.updateOptionsActiveStatus(selections, newOptionList);
  137. // reset focusIndex
  138. const { defaultActiveFirstOption } = this.getProps();
  139. if (defaultActiveFirstOption) {
  140. this._adapter.updateFocusIndex(0);
  141. }
  142. }
  143. // In uncontrolled mode, when props.optionList change,
  144. // but already had defaultValue or choose some option
  145. handleOptionListChangeHadDefaultValue() {
  146. const selections = this.getState('selections');
  147. let value;
  148. const { onChangeWithObject } = this.getProps();
  149. const isMultiple = this._isMultiple();
  150. switch (true) {
  151. case isMultiple && Boolean(selections.size):
  152. try {
  153. value = [...selections].map(item =>
  154. // At this point item1 is directly the object
  155. (onChangeWithObject ? item[1] : item[1].value)
  156. );
  157. } catch (error) {
  158. value = [];
  159. }
  160. break;
  161. case isMultiple && !selections.size:
  162. value = [];
  163. break;
  164. case !isMultiple && Boolean(selections.size):
  165. try {
  166. value = onChangeWithObject ? [...selections][0][1] : [...selections][0][1].value;
  167. } catch (error) {}
  168. break;
  169. case !isMultiple && !selections.size:
  170. break;
  171. default:
  172. break;
  173. }
  174. const originalOptions = this._adapter.getOptionsFromChildren();
  175. this._update(value, originalOptions);
  176. }
  177. // call when props.value change
  178. handleValueChange(value: PropValue) {
  179. const { allowCreate, autoClearSearchValue, remote } = this.getProps();
  180. const { inputValue } = this.getStates();
  181. let originalOptions;
  182. // AllowCreate and controlled mode, no need to re-collect optionList
  183. if (allowCreate && this._isControlledComponent()) {
  184. originalOptions = this.getState('options') as BasicOptionProps[];
  185. originalOptions.forEach(item => (item._show = true));
  186. } else {
  187. // originalOptions = this.getState('options');
  188. // The options in state cannot be used directly here,
  189. // because it is possible to update the optionList and props.value at the same time, and the options in state are still old at this time
  190. originalOptions = this._adapter.getOptionsFromChildren();
  191. }
  192. // Multi-selection, controlled mode, you need to reposition the drop-down menu after updating
  193. this._adapter.rePositionDropdown();
  194. if (this._isFilterable() && !autoClearSearchValue && inputValue && !remote) {
  195. originalOptions = this._filterOption(originalOptions, inputValue);
  196. }
  197. this._update(value, originalOptions);
  198. }
  199. // Update the selected item in the selection box
  200. _update(propValue: PropValue, originalOptions: BasicOptionProps[]) {
  201. let selections;
  202. if (!this._isMultiple()) {
  203. // Radio
  204. selections = this._updateSingle(propValue, originalOptions);
  205. } else {
  206. selections = this._updateMultiple(propValue as (PropValue)[], originalOptions);
  207. this.updateOverflowItemCount(selections.size);
  208. }
  209. // Update the text in the selection box
  210. this._adapter.updateSelection(selections);
  211. // Update the selected item in the drop-down box
  212. this.updateOptionsActiveStatus(selections, originalOptions);
  213. }
  214. // Optionally selected updates (when components are mounted, or after value changes)
  215. _updateSingle(propValue: PropValue, originalOptions: BasicOptionProps[]) {
  216. const selections = new Map();
  217. const { onChangeWithObject } = this.getProps();
  218. // When onChangeWithObject is true, the defaultValue or Value passed by the props should be the object, which corresponds to the result returned by onChange, so the value of the object needs to be taken as a judgment comparison
  219. const selectedValue = onChangeWithObject && typeof propValue !== 'undefined' ? (propValue as BasicOptionProps).value : propValue;
  220. const selectedOptions = originalOptions.filter(option => option.value === selectedValue);
  221. const noMatchOptionInList = !selectedOptions.length && typeof selectedValue !== 'undefined' && selectedValue !== null;
  222. // If the current value, there is a matching option in the optionList
  223. if (selectedOptions.length) {
  224. const selectedOption = selectedOptions[0];
  225. const optionExist = { ...selectedOption };
  226. // if (onChangeWithObject) {
  227. // OptionExist = {... propValue }; // value is the object with the'value 'Key
  228. // }
  229. selections.set(optionExist.label, optionExist);
  230. } else if (noMatchOptionInList) {
  231. // If the current value does not have a corresponding item in the optionList, construct an option and update it to the selection. However, it does not need to be inserted into the list
  232. let optionNotExist = { value: propValue, label: propValue, _notExist: true, _scrollIndex: -1 } as BasicOptionProps;
  233. if (onChangeWithObject) {
  234. optionNotExist = { ...propValue as BasicOptionProps, _notExist: true, _scrollIndex: -1 };
  235. }
  236. selections.set(optionNotExist.label, optionNotExist);
  237. }
  238. return selections;
  239. }
  240. // Multi-selected option update (when the component is mounted, or after the value changes)
  241. _updateMultiple(propValue: PropValue[], originalOptions: BasicOptionProps[]) {
  242. const nowSelections = this.getState('selections');
  243. let selectedOptionList: any[] = [];
  244. // Multiple selection is to determine whether it is an array to avoid the problem of defaultValue/value incoming string error
  245. const propValueIsArray = Array.isArray(propValue);
  246. this.checkMultipleProps();
  247. // If N values are currently selected, the corresponding option data is retrieved from the current selections for retrieval. Because these selected options may not exist in the new optionList
  248. if (nowSelections.size) {
  249. selectedOptionList = [...nowSelections].map(item => item[1]);
  250. }
  251. const selections = new Map();
  252. let selectedValues = propValue;
  253. const { onChangeWithObject } = this.getProps();
  254. // When onChangeWithObject is true
  255. if (onChangeWithObject && propValueIsArray) {
  256. selectedValues = (propValue as BasicOptionProps[]).map(item => item.value);
  257. }
  258. if (propValueIsArray && selectedValues.length) {
  259. (selectedValues as LabelValue[]).forEach((selectedValue, i: number) => {
  260. // The current value exists in the current optionList
  261. const index = originalOptions.findIndex(option => option.value === selectedValue);
  262. if (index !== -1) {
  263. selections.set(originalOptions[index].label, originalOptions[index]);
  264. } else {
  265. // The current value exists in the optionList that has been selected before the change, and does not exist in the current optionList, then directly take the corresponding value from the selections, no need to construct a new option
  266. const indexInSelectedList = selectedOptionList.findIndex(option => option.value === selectedValue);
  267. if (indexInSelectedList !== -1) {
  268. const option = selectedOptionList[indexInSelectedList];
  269. if (onChangeWithObject) {
  270. // Although the value is the same and can be found in selections, it cannot ensure that other items remain unchanged. A comparison is made.
  271. // https://github.com/DouyinFE/semi-design/pull/2139
  272. const optionCompare = { ...(propValue[i] as any) };
  273. if (isEqual(optionCompare, option)) {
  274. selections.set(option.label, option);
  275. } else {
  276. selections.set(optionCompare.label, optionCompare);
  277. }
  278. } else {
  279. selections.set(option.label, option);
  280. }
  281. } else {
  282. // The current value does not exist in the current optionList or the list before the change. Construct an option and update it to the selection
  283. let optionNotExist = { value: selectedValue, label: selectedValue, _notExist: true };
  284. onChangeWithObject ? (optionNotExist = { ...propValue[i] as any, _notExist: true }) : null;
  285. selections.set(optionNotExist.label, { ...optionNotExist, _scrollIndex: -1 });
  286. }
  287. }
  288. });
  289. }
  290. return selections;
  291. }
  292. _isMultiple() {
  293. return this.getProp('multiple');
  294. }
  295. _isDisabled() {
  296. return this.getProp('disabled');
  297. }
  298. _isFilterable() {
  299. return Boolean(this.getProp('filter')); // filter can be boolean or function
  300. }
  301. handleClick(e: any) {
  302. const { clickToHide } = this.getProps();
  303. const { isOpen } = this.getStates();
  304. const isDisabled = this._isDisabled();
  305. if (isDisabled) {
  306. return;
  307. } else if (!isOpen) {
  308. this.open();
  309. this._notifyFocus(e);
  310. } else if (isOpen && clickToHide) {
  311. this.close({ event: e });
  312. } else if (isOpen && !clickToHide) {
  313. this.focusInput();
  314. }
  315. }
  316. open(acInput?: string, originalOptions?: BasicOptionProps[]) {
  317. const isFilterable = this._isFilterable();
  318. const options = originalOptions || this.getState('options');
  319. // When searchable, when the drop-down box expands
  320. if (isFilterable) {
  321. // Also clears the options filter to show all candidates
  322. // Options created dynamically but not selected are also filtered out
  323. const sugInput = '';
  324. const newOptions = this._filterOption(options, sugInput).filter(item => !item._inputCreateOnly);
  325. this._adapter.updateOptions(newOptions);
  326. this.toggle2SearchInput(true);
  327. } else {
  328. // whether it is a filter or not, isFocus is guaranteed to be true when open
  329. this._adapter.updateFocusState(true);
  330. }
  331. this._adapter.openMenu();
  332. this._setDropdownWidth();
  333. this._adapter.notifyDropdownVisibleChange(true);
  334. this.bindKeyBoardEvent();
  335. this._adapter.registerClickOutsideHandler((e: MouseEvent) => {
  336. this.close({ event: e });
  337. this._notifyBlur(e);
  338. this._adapter.updateFocusState(false);
  339. });
  340. }
  341. toggle2SearchInput(isShow: boolean) {
  342. if (isShow) {
  343. this._adapter.toggleInputShow(isShow, () => this.focusInput());
  344. } else {
  345. // only when choose the option and close the panel, the input can be hide
  346. this._adapter.toggleInputShow(isShow, () => undefined);
  347. }
  348. }
  349. close(closeConfig?: { event?: any; closeCb?: () => void; notToggleInput?: boolean }) {
  350. // to support A11y, closing the panel trigger does not necessarily lose focus
  351. const { event, closeCb, notToggleInput } = closeConfig || {};
  352. this._adapter.closeMenu();
  353. this._adapter.notifyDropdownVisibleChange(false);
  354. this._adapter.setIsFocusInContainer(false);
  355. // this.unBindKeyBoardEvent();
  356. // this._notifyBlur(e);
  357. // this._adapter.updateFocusState(false);
  358. this._adapter.unregisterClickOutsideHandler();
  359. const isFilterable = this._isFilterable();
  360. // notToggleInput will only be true when in controlled mode - handleSingeleSelect process
  361. if (isFilterable && !notToggleInput) {
  362. this.toggle2SearchInput(false);
  363. }
  364. this._adapter.once('popoverClose', () => {
  365. if (isFilterable) {
  366. this.clearInput(event);
  367. }
  368. if (closeCb) {
  369. closeCb();
  370. }
  371. });
  372. }
  373. onSelect(option: BasicOptionProps, optionIndex: number, event: MouseEvent | KeyboardEvent) {
  374. const isDisabled = this._isDisabled();
  375. if (isDisabled) {
  376. return;
  377. }
  378. // If the allowCreate dynamically created option is selected, onCreate needs to be triggered
  379. if (option._inputCreateOnly) {
  380. this._adapter.notifyCreate(option);
  381. }
  382. const isMultiple = this._isMultiple();
  383. if (!isMultiple) {
  384. this._handleSingleSelect(option, event);
  385. this._focusTrigger();
  386. } else {
  387. this._handleMultipleSelect(option, event);
  388. }
  389. this._adapter.updateFocusIndex(optionIndex);
  390. }
  391. _handleSingleSelect({ value, label, ...rest }: BasicOptionProps, event: any) {
  392. const selections = new Map().set(label, { value, label, ...rest });
  393. // First trigger onSelect, then trigger onChange
  394. this._notifySelect(value, { value, label, ...rest });
  395. // If it is a controlled component, directly notify
  396. // Make sure that the operations of updating updateOptions are done after the animation ends
  397. // otherwise the content will be updated when the popup layer is not collapsed, and it looks like it will flash once when it is closed
  398. const isFilterable = this._isFilterable();
  399. if (this._isControlledComponent()) {
  400. this.close({
  401. event: event,
  402. notToggleInput: true,
  403. closeCb: () => {
  404. // trigger props.onChange -> update props.value -> updateSelection
  405. this._notifyChange(selections);
  406. // make sure toggleSearchInput update after updateSelection in controlled mode, otherwise text in inactive DOM will update quicker than selection, looks like flash text
  407. if (isFilterable) {
  408. this.toggle2SearchInput(false);
  409. }
  410. }
  411. });
  412. } else {
  413. this._adapter.updateSelection(selections);
  414. // notify user
  415. this._notifyChange(selections);
  416. this.close({
  417. event: event,
  418. closeCb: () => {
  419. // Update the selected item in the drop-down box
  420. this.updateOptionsActiveStatus(selections);
  421. },
  422. });
  423. }
  424. }
  425. _handleMultipleSelect({ value, label, ...rest }: BasicOptionProps, event: MouseEvent | KeyboardEvent) {
  426. const maxLimit = this._adapter.getMaxLimit();
  427. const selections = this._adapter.getSelections();
  428. const { autoClearSearchValue } = this.getProps();
  429. if (selections.has(label)) {
  430. this._notifyDeselect(value, { value, label, ...rest });
  431. selections.delete(label);
  432. } else if (maxLimit && selections.size === maxLimit) {
  433. this._adapter.notifyMaxLimit({ value, label, ...omit(rest, '_scrollIndex') });
  434. return;
  435. } else {
  436. this._notifySelect(value, { value, label, ...rest });
  437. selections.set(label, { value, label, ...rest });
  438. }
  439. if (this._isControlledComponent()) {
  440. // Controlled components, directly notified
  441. this._notifyChange(selections);
  442. if (this._isFilterable()) {
  443. if (autoClearSearchValue) {
  444. this.clearInput(event);
  445. }
  446. this.focusInput();
  447. }
  448. } else {
  449. // Uncontrolled components, update ui
  450. this._adapter.updateSelection(selections);
  451. this.updateOverflowItemCount(selections.size);
  452. // In multi-select mode, the drop-down pop-up layer is repositioned every time the value is changed, because the height selection of the selection box may have changed
  453. this._adapter.rePositionDropdown();
  454. let { options } = this.getStates();
  455. // Searchable filtering, when selected, resets Input
  456. if (this._isFilterable()) {
  457. // When filter active,if autoClearSearchValue is true,reset input after select
  458. if (autoClearSearchValue) {
  459. this.clearInput(event);
  460. // At the same time, the filtering of options is also cleared, in order to show all candidates
  461. const sugInput = '';
  462. options = this._filterOption(options, sugInput);
  463. }
  464. this.focusInput();
  465. }
  466. this.updateOptionsActiveStatus(selections, options);
  467. this._notifyChange(selections);
  468. }
  469. }
  470. clearSelected() {
  471. const selections = new Map();
  472. if (this._isControlledComponent()) {
  473. this._notifyChange(selections);
  474. this._adapter.notifyClear();
  475. } else {
  476. this._adapter.updateSelection(selections);
  477. this.updateOptionsActiveStatus(selections);
  478. this._notifyChange(selections);
  479. this._adapter.notifyClear();
  480. }
  481. // when call manually by ref method
  482. const { isOpen } = this.getStates();
  483. if (isOpen) {
  484. this._adapter.rePositionDropdown();
  485. }
  486. }
  487. // Update the selected item in the drop-down box
  488. updateOptionsActiveStatus(selections: Map<any, any>, options: BasicOptionProps[] = this.getState('options')) {
  489. const { allowCreate } = this.getProps();
  490. const newOptions = options.map(option => {
  491. if (selections.has(option.label)) {
  492. option._selected = true;
  493. if (allowCreate) {
  494. delete option._inputCreateOnly;
  495. }
  496. } else {
  497. if (option._inputCreateOnly) {
  498. option._show = false;
  499. }
  500. option._selected = false;
  501. }
  502. return option;
  503. });
  504. this._adapter.updateOptions(newOptions);
  505. }
  506. removeTag(item: BasicOptionProps) {
  507. const selections = this._adapter.getSelections();
  508. selections.delete(item.label);
  509. if (this._isControlledComponent()) {
  510. this._notifyDeselect(item.value, item);
  511. this._notifyChange(selections);
  512. } else {
  513. this._notifyDeselect(item.value, item);
  514. this._adapter.updateSelection(selections);
  515. this.updateOverflowItemCount(selections.size);
  516. this.updateOptionsActiveStatus(selections);
  517. // Repostion drop-down layer, because the selection may have changed the number of rows, resulting in a height change
  518. this._adapter.rePositionDropdown();
  519. this._notifyChange(selections);
  520. }
  521. }
  522. // The reason why event input is optional is that clearInput may be manually called by the user through ref
  523. clearInput(event?: any) {
  524. const { inputValue } = this.getStates();
  525. // only when input is not null, select should notifySearch and updateOptions
  526. if (inputValue !== '') {
  527. this._adapter.updateInputValue('');
  528. this._adapter.notifySearch('', event);
  529. // reset options filter
  530. const { options } = this.getStates();
  531. const { remote } = this.getProps();
  532. let optionsAfterFilter = options;
  533. if (!remote) {
  534. optionsAfterFilter = this._filterOption(options, '');
  535. }
  536. this._adapter.updateOptions(optionsAfterFilter);
  537. }
  538. }
  539. focusInput() {
  540. this._adapter.focusInput();
  541. this._adapter.updateFocusState(true);
  542. this._adapter.setIsFocusInContainer(false);
  543. }
  544. handleInputChange(sugInput: string, event: any) {
  545. // Input is a controlled component, so the value needs to be updated
  546. this._adapter.updateInputValue(sugInput);
  547. const { options, isOpen } = this.getStates();
  548. const { allowCreate, remote } = this.getProps();
  549. let optionsAfterFilter = options;
  550. if (!remote) {
  551. // Filter options based on input
  552. optionsAfterFilter = this._filterOption(options, sugInput);
  553. }
  554. // When allowClear is true, an entry can be created. You need to include the current input as a new Option input
  555. optionsAfterFilter = this._createOptionByInput(allowCreate, optionsAfterFilter, sugInput);
  556. this._adapter.updateOptions(optionsAfterFilter);
  557. this._adapter.notifySearch(sugInput, event);
  558. // In multi-select mode, the drop-down box is repositioned each time you enter, because it may cause a line break as the input changes
  559. if (this._isMultiple()) {
  560. this._adapter.rePositionDropdown();
  561. }
  562. }
  563. _filterOption(originalOptions: BasicOptionProps[], sugInput: string) {
  564. const filter = this.getProp('filter');
  565. if (!filter) {
  566. // 1. No filtering
  567. return originalOptions;
  568. } else if (typeof filter === 'boolean' && filter) {
  569. // 2. When true, the default filter is used
  570. const input = sugInput.toLowerCase();
  571. return originalOptions.map(option => {
  572. const label = option.label.toString().toLowerCase();
  573. const groupLabel = option._parentGroup && option._parentGroup.label;
  574. const matchOption = label.includes(input);
  575. const matchGroup = isString(groupLabel) && groupLabel.toLowerCase().includes(input);
  576. if (matchOption || matchGroup) {
  577. option._show = true;
  578. } else {
  579. option._show = false;
  580. }
  581. return option;
  582. });
  583. } else if (typeof filter === 'function') {
  584. // 3. When passing in a custom function, use a custom function for filtering
  585. return originalOptions.map(option => {
  586. filter(sugInput, option) ? (option._show = true) : (option._show = false);
  587. return option;
  588. });
  589. }
  590. return undefined;
  591. }
  592. _createOptionByInput(allowCreate: boolean, optionsAfterFilter: BasicOptionProps[], sugInput: string) {
  593. if (allowCreate) {
  594. if (sugInput) {
  595. // optionsAfterFilter clone ??? needClone ?
  596. const newOptionByInput = {
  597. _show: true,
  598. _selected: false,
  599. value: sugInput,
  600. label: sugInput,
  601. // True indicates that the option was dynamically created during user filtering
  602. _inputCreateOnly: true,
  603. };
  604. let createOptionIndex = -1;
  605. let matchOptionIndex = -1;
  606. optionsAfterFilter.forEach((option, index) => {
  607. if (!option._show && !option._inputCreateOnly) {
  608. return;
  609. }
  610. // The matching algorithm is not necessarily through labels?
  611. if (option.label === sugInput) {
  612. matchOptionIndex = index;
  613. }
  614. if (option._inputCreateOnly) {
  615. createOptionIndex = index;
  616. option.value = sugInput;
  617. option.label = sugInput;
  618. option._show = true;
  619. }
  620. });
  621. if (createOptionIndex === -1 && matchOptionIndex === -1) {
  622. optionsAfterFilter.push(newOptionByInput);
  623. }
  624. if (matchOptionIndex !== -1) {
  625. optionsAfterFilter = optionsAfterFilter.filter(item => !item._inputCreateOnly);
  626. }
  627. } else {
  628. // Delete input unselected items
  629. optionsAfterFilter = optionsAfterFilter.filter(item => !item._inputCreateOnly);
  630. }
  631. }
  632. // TODO Promise supports asynchronous creation
  633. return optionsAfterFilter;
  634. }
  635. bindKeyBoardEvent() {
  636. this._keydownHandler = event => {
  637. this._handleKeyDown(event);
  638. };
  639. this._adapter.registerKeyDown(this._keydownHandler);
  640. }
  641. unBindKeyBoardEvent() {
  642. if (this._keydownHandler) {
  643. this._adapter.unregisterKeyDown();
  644. }
  645. }
  646. _handleKeyDown(event: KeyboardEvent) {
  647. const key = event.keyCode;
  648. const { loading, filter, multiple, disabled } = this.getProps();
  649. const { isOpen } = this.getStates();
  650. if (loading || disabled) {
  651. return;
  652. }
  653. switch (key) {
  654. case KeyCode.UP:
  655. // Prevent Input's cursor from following
  656. // Prevent Input cursor from following
  657. event.preventDefault();
  658. this._handleArrowKeyDown(-1);
  659. break;
  660. case KeyCode.DOWN:
  661. // Prevent Input's cursor from following
  662. // Prevent Input cursor from following
  663. event.preventDefault();
  664. this._handleArrowKeyDown(1);
  665. break;
  666. case KeyCode.BACKSPACE:
  667. this._handleBackspaceKeyDown();
  668. break;
  669. case KeyCode.ENTER:
  670. // internal-issues:302
  671. // prevent trigger form’s submit when use in form
  672. handlePrevent(event);
  673. this._handleEnterKeyDown(event);
  674. break;
  675. case KeyCode.ESC:
  676. isOpen && this.close({ event: event });
  677. filter && !multiple && this._focusTrigger();
  678. break;
  679. case KeyCode.TAB:
  680. // check if slot have focusable element
  681. this._handleTabKeyDown(event);
  682. break;
  683. default:
  684. break;
  685. }
  686. }
  687. handleContainerKeyDown(event: any) {
  688. // when focus in contanier, handle the key down
  689. const key = event.keyCode;
  690. const { isOpen } = this.getStates();
  691. switch (key) {
  692. case KeyCode.TAB:
  693. isOpen && this._handleTabKeyDown(event);
  694. break;
  695. default:
  696. break;
  697. }
  698. }
  699. _getEnableFocusIndex(offset: number) {
  700. const { focusIndex, options } = this.getStates();
  701. const visibleOptions = options.filter((item: BasicOptionProps) => item._show);
  702. // let visibleOptions = options;
  703. const optionsLength = visibleOptions.length;
  704. let index = focusIndex + offset;
  705. if (index < 0) {
  706. index = optionsLength - 1;
  707. }
  708. if (index >= optionsLength) {
  709. index = 0;
  710. }
  711. // avoid newIndex option is disabled
  712. if (offset > 0) {
  713. let nearestActiveOption = -1;
  714. for (let i = 0; i < visibleOptions.length; i++) {
  715. const optionIsActive = !visibleOptions[i].disabled;
  716. if (optionIsActive) {
  717. nearestActiveOption = i;
  718. }
  719. if (nearestActiveOption >= index) {
  720. break;
  721. }
  722. }
  723. index = nearestActiveOption;
  724. } else {
  725. let nearestActiveOption = visibleOptions.length;
  726. for (let i = optionsLength - 1; i >= 0; i--) {
  727. const optionIsActive = !visibleOptions[i].disabled;
  728. if (optionIsActive) {
  729. nearestActiveOption = i;
  730. }
  731. if (nearestActiveOption <= index) {
  732. break;
  733. }
  734. }
  735. index = nearestActiveOption;
  736. }
  737. // console.log('new:' + index);
  738. this._adapter.updateFocusIndex(index);
  739. this._adapter.updateScrollTop(index);
  740. }
  741. _handleArrowKeyDown(offset: number) {
  742. const { isOpen } = this.getStates();
  743. isOpen ? this._getEnableFocusIndex(offset) : this.open();
  744. }
  745. _handleTabKeyDown(event: any) {
  746. const { isOpen } = this.getStates();
  747. this._adapter.updateFocusState(false);
  748. if (isOpen) {
  749. const container = this._adapter.getContainer();
  750. const focusableElements = this._adapter.getFocusableElements(container);
  751. const focusableNum = focusableElements.length;
  752. if (focusableNum > 0) {
  753. // Shift + Tab will move focus backward
  754. if (event.shiftKey) {
  755. this._handlePanelOpenShiftTabKeyDown(focusableElements, event);
  756. } else {
  757. this._handlePanelOpenTabKeyDown(focusableElements, event);
  758. }
  759. } else {
  760. // there are no focusable elements inside the container, tab to next element and trigger blur
  761. this.close({ event: event });
  762. this._notifyBlur(event);
  763. }
  764. } else {
  765. // tab or shift tab to next element and trigger blur
  766. this._notifyBlur(event);
  767. }
  768. }
  769. _handlePanelOpenTabKeyDown(focusableElements: any[], event: any) {
  770. const activeElement = this._adapter.getActiveElement();
  771. const isFocusInContainer = this._adapter.getIsFocusInContainer();
  772. if (!isFocusInContainer) {
  773. // focus in trigger, set next focus to the first element in container
  774. focusableElements[0].focus();
  775. this._adapter.setIsFocusInContainer(true);
  776. handlePrevent(event);
  777. } else if (activeElement === focusableElements[focusableElements.length - 1]) {
  778. // focus in the last element in container, focus back to trigger and close panel
  779. this._focusTrigger();
  780. this.close({ event });
  781. handlePrevent(event);
  782. }
  783. }
  784. _handlePanelOpenShiftTabKeyDown(focusableElements: any[], event: any) {
  785. const activeElement = this._adapter.getActiveElement();
  786. const isFocusInContainer = this._adapter.getIsFocusInContainer();
  787. if (!isFocusInContainer) {
  788. // focus in trigger, close the panel, shift tab to previe element and trigger blur
  789. this.close({ event });
  790. this._notifyBlur(event);
  791. } else if (activeElement === focusableElements[0]) {
  792. // focus in the first element in container, focus back to trigger
  793. this._focusTrigger();
  794. this._adapter.setIsFocusInContainer(false);
  795. handlePrevent(event);
  796. }
  797. }
  798. _handleEnterKeyDown(event: KeyboardEvent) {
  799. const { isOpen, options, focusIndex } = this.getStates();
  800. if (!isOpen) {
  801. this.open();
  802. } else {
  803. if (focusIndex !== -1) {
  804. const visibleOptions = options.filter((item: BasicOptionProps) => item._show);
  805. const { length } = visibleOptions;
  806. // fix issue 1201
  807. if (length <= focusIndex) {
  808. return;
  809. }
  810. if (visibleOptions && length) {
  811. const selectedOption = visibleOptions[focusIndex];
  812. if (selectedOption.disabled) {
  813. return;
  814. }
  815. this.onSelect(selectedOption, focusIndex, event);
  816. }
  817. } else {
  818. this.close({ event });
  819. }
  820. }
  821. }
  822. _handleBackspaceKeyDown() {
  823. if (this._isMultiple()) {
  824. const selections = this._adapter.getSelections();
  825. const { inputValue } = this.getStates();
  826. const length = selections.size;
  827. if (length && !inputValue) {
  828. const keys = [...selections.keys()];
  829. let index = length - 1;
  830. let targetLabel = keys[index];
  831. let targetItem = selections.get(targetLabel);
  832. let isAllDisabled = false;
  833. // can skip disabled item when remove trigger by backspace
  834. if (targetItem.disabled && index === 0) {
  835. return;
  836. }
  837. while (targetItem.disabled && index !== 0) {
  838. index = index - 1;
  839. targetLabel = keys[index];
  840. targetItem = selections.get(targetLabel);
  841. if (index == 0 && targetItem.disabled) {
  842. isAllDisabled = true;
  843. }
  844. }
  845. if (!isAllDisabled) {
  846. this.removeTag(targetItem);
  847. }
  848. }
  849. }
  850. }
  851. _notifyChange(selections: Map<any, any>) {
  852. const { onChangeWithObject } = this.getProps();
  853. const stateSelections = this.getState('selections');
  854. let notifyVal;
  855. const selectionsProps = [...selections.values()];
  856. const isMultiple = this._isMultiple();
  857. const hasChange = this._diffSelections(selections, stateSelections, isMultiple);
  858. if (!hasChange) {
  859. return;
  860. }
  861. switch (true) {
  862. case onChangeWithObject:
  863. this._notifyChangeWithObject(selections);
  864. break;
  865. case !onChangeWithObject && !isMultiple:
  866. notifyVal = selectionsProps.length ? selectionsProps[0].value : undefined;
  867. this._adapter.notifyChange(notifyVal);
  868. break;
  869. case !onChangeWithObject && isMultiple:
  870. notifyVal = selectionsProps.length ? selectionsProps.map(props => props.value) : [];
  871. this._adapter.notifyChange(notifyVal);
  872. break;
  873. default:
  874. break;
  875. }
  876. }
  877. _removeInternalKey(option: BasicOptionProps) {
  878. let newOption = { ...option };
  879. delete newOption._parentGroup;
  880. delete newOption._show;
  881. delete newOption._selected;
  882. delete newOption._scrollIndex;
  883. delete newOption._keyInJsx;
  884. if ('_keyInOptionList' in newOption) {
  885. newOption.key = newOption._keyInOptionList;
  886. delete newOption._keyInOptionList;
  887. }
  888. return newOption;
  889. }
  890. _notifySelect(value: BasicOptionProps['value'], option: BasicOptionProps) {
  891. const newOption = this._removeInternalKey(option);
  892. this._adapter.notifySelect(value, newOption);
  893. }
  894. _notifyDeselect(value: BasicOptionProps['value'], option: BasicOptionProps) {
  895. const newOption = this._removeInternalKey(option);
  896. this._adapter.notifyDeselect(value, newOption);
  897. }
  898. _diffSelections(selections: Map<any, any>, oldSelections: Map<any, any>, isMultiple: boolean) {
  899. let diff = true;
  900. if (!isMultiple) {
  901. const selectionProps = [...selections.values()];
  902. const oldSelectionProps = [...oldSelections.values()];
  903. const optionLabel = selectionProps[0] ? selectionProps[0].label : selectionProps[0];
  904. const oldOptionLabel = oldSelectionProps[0] ? oldSelectionProps[0].label : oldSelectionProps[0];
  905. diff = !isEqual(optionLabel, oldOptionLabel);
  906. } else {
  907. // When multiple selection, there is no scene where the value is different between the two operations
  908. }
  909. return diff;
  910. }
  911. // When onChangeWithObject is true, the onChange input parameter is not only value, but also label and other parameters
  912. _notifyChangeWithObject(selections: Map<any, any>) {
  913. const stateSelections = this.getState('selections');
  914. const values = [];
  915. for (const item of selections.entries()) {
  916. let val = { label: item[0], ...item[1] };
  917. val = this._removeInternalKey(val);
  918. values.push(val);
  919. }
  920. if (!this._isMultiple()) {
  921. this._adapter.notifyChange(values[0]);
  922. } else {
  923. this._adapter.notifyChange(values);
  924. }
  925. }
  926. // Scenes that may trigger blur:
  927. // 1、clickOutSide
  928. // 2、 tab to next element/ shift tab to previous element
  929. // 3、[remove when add a11y] click option / press enter, and then select complete(when multiple is false
  930. // 4、[remove when add a11y] press esc when dropdown list open
  931. _notifyBlur(e: FocusEvent) {
  932. this._adapter.notifyBlur(e);
  933. }
  934. // Scenes that may trigger focus:
  935. // 1、click selection
  936. _notifyFocus(e: FocusEvent) {
  937. this._adapter.notifyFocus(e);
  938. }
  939. handleMouseEnter(e: MouseEvent) {
  940. this._adapter.updateHovering(true);
  941. this._adapter.notifyMouseEnter(e);
  942. }
  943. handleMouseLeave(e: MouseEvent) {
  944. this._adapter.updateHovering(false);
  945. this._adapter.notifyMouseLeave(e);
  946. }
  947. handleClearClick(e: MouseEvent) {
  948. const { filter } = this.getProps();
  949. if (filter) {
  950. this.clearInput(e);
  951. }
  952. // after click showClear button, the select need to be focused
  953. this.focus();
  954. this.clearSelected();
  955. // prevent this click open dropdown
  956. e.stopPropagation();
  957. }
  958. handleKeyPress(e: KeyboardEvent) {
  959. if (e && e.key === ENTER_KEY) {
  960. this.handleClick(e);
  961. }
  962. }
  963. /* istanbul ignore next */
  964. handleClearBtnEnterPress(e: KeyboardEvent) {
  965. if (isEnterPress(e)) {
  966. this.handleClearClick(e as any);
  967. }
  968. }
  969. handleOptionMouseEnter(optionIndex: number) {
  970. this._adapter.updateFocusIndex(optionIndex);
  971. }
  972. handleListScroll(e: any) {
  973. this._adapter.notifyListScroll(e);
  974. }
  975. handleTriggerFocus(e) {
  976. this.bindKeyBoardEvent();
  977. // close the tag in multiple select did not trigger select focus, but trigger TriggerFocus, so not need to updateFocusState in this function
  978. // this._adapter.updateFocusState(true);
  979. this._adapter.setIsFocusInContainer(false);
  980. }
  981. handleTriggerBlur(e: FocusEvent) {
  982. const { filter, autoFocus } = this.getProps();
  983. const { isOpen, isFocus } = this.getStates();
  984. // Under normal circumstances, blur will be accompanied by clickOutsideHandler, so the notify of blur can be called uniformly in clickOutsideHandler
  985. // But when autoFocus or the panel is close, because clickOutsideHandler is not register or unregister, you need to listen for the trigger's blur and trigger the notify callback
  986. if (isFocus && !isOpen) {
  987. this._notifyBlur(e);
  988. this._adapter.updateFocusState(false);
  989. }
  990. }
  991. handleInputBlur(e: any) {
  992. const { filter, autoFocus } = this.getProps();
  993. const isMultiple = this._isMultiple();
  994. if (autoFocus && filter && !isMultiple ) {
  995. // under this condition, when input blur, hide the input
  996. this.toggle2SearchInput(false);
  997. }
  998. }
  999. selectAll() {
  1000. const { options } = this.getStates();
  1001. const { onChangeWithObject } = this.getProps();
  1002. let selectedValues = [];
  1003. const isMultiple = this._isMultiple();
  1004. if (!isMultiple) {
  1005. console.warn(`[Semi Select]: It seems that you have called the selectAll method in the single-selection Select.
  1006. Please note that this is not a legal way to use it`
  1007. );
  1008. return;
  1009. }
  1010. if (onChangeWithObject) {
  1011. selectedValues = options;
  1012. } else {
  1013. selectedValues = options.map((option: BasicOptionProps) => option.value);
  1014. }
  1015. this.handleValueChange(selectedValues);
  1016. this._adapter.notifyChange(selectedValues);
  1017. }
  1018. /**
  1019. * Check whether the props
  1020. * -defaultValue/value in multiple selection mode is array
  1021. * @param {Object} props
  1022. */
  1023. checkMultipleProps(props?: Record<string, any>) {
  1024. if (this._isMultiple()) {
  1025. const currentProps = props ? props : this.getProps();
  1026. const { defaultValue, value } = currentProps;
  1027. const selectedValues = value || defaultValue;
  1028. if (!isNullOrUndefined(selectedValues) && !Array.isArray(selectedValues)) {
  1029. /* istanbul ignore next */
  1030. warning(true, '[Semi Select] defaultValue/value should be array type in multiple mode');
  1031. }
  1032. }
  1033. }
  1034. updateScrollTop() {
  1035. this._adapter.updateScrollTop();
  1036. }
  1037. updateOverflowItemCount(selectionLength: number, overFlowCount?: number) {
  1038. const { maxTagCount, ellipsisTrigger } = this.getProps();
  1039. if (!ellipsisTrigger) {
  1040. return ;
  1041. }
  1042. if (overFlowCount) {
  1043. this._adapter.updateOverflowItemCount(overFlowCount);
  1044. } else if (typeof maxTagCount === 'number') {
  1045. if (selectionLength - maxTagCount > 0) {
  1046. this._adapter.updateOverflowItemCount(selectionLength - maxTagCount);
  1047. } else {
  1048. this._adapter.updateOverflowItemCount(0);
  1049. }
  1050. }
  1051. }
  1052. updateIsFullTags() {
  1053. const { isFullTags } = this.getStates();
  1054. if (!isFullTags) {
  1055. this._adapter.setState({
  1056. isFullTags: true,
  1057. });
  1058. }
  1059. }
  1060. handlePopoverClose() {
  1061. this._adapter.emit('popoverClose');
  1062. }
  1063. // need to remove focus style of option when user hover slot
  1064. handleSlotMouseEnter() {
  1065. this._adapter.updateFocusIndex(-1);
  1066. }
  1067. }