foundation.ts 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /* argus-disable unPkgSensitiveInfo */
  2. /* eslint-disable max-len */
  3. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  4. import KeyCode, { ENTER_KEY } from '../utils/keyCode';
  5. import { isNumber, isString, isEqual } from 'lodash-es';
  6. import warning from '../utils/warning';
  7. import isNullOrUndefined from '../utils/isNullOrUndefined';
  8. import { BasicOptionProps } from './optionFoundation';
  9. import isEnterPress from '../utils/isEnterPress';
  10. export interface SelectAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  11. getTriggerWidth(): number;
  12. setOptionsWidth?(): any;
  13. updateFocusState(focus: boolean): void;
  14. focusTrigger(): void;
  15. unregisterClickOutsideHandler(): void;
  16. setOptionWrapperWidth(width: string | number): void;
  17. getOptionsFromChildren(): BasicOptionProps[];
  18. updateOptions(options: BasicOptionProps[]): void;
  19. rePositionDropdown(): void;
  20. updateFocusIndex(index: number): void;
  21. updateSelection(selection: Map<any, any>): void;
  22. openMenu(): void;
  23. notifyDropdownVisibleChange(visible: boolean): void;
  24. registerClickOutsideHandler(event: any): void;
  25. toggleInputShow(show: boolean, cb: () => void): void;
  26. closeMenu(): void;
  27. notifyCreate(option: BasicOptionProps): void;
  28. getMaxLimit(): number;
  29. getSelections(): Map<any, any>;
  30. notifyMaxLimit(arg: BasicOptionProps): void;
  31. notifyClear(): void;
  32. updateInputValue(inputValue: string): void;
  33. focusInput(): void;
  34. notifySearch(inputValue: string): void;
  35. registerKeyDown(handler: () => void): void;
  36. unregisterKeyDown(): void;
  37. notifyChange(value: string | BasicOptionProps | (string | BasicOptionProps)[]): void;
  38. notifySelect(value: BasicOptionProps['value'], option: BasicOptionProps): void;
  39. notifyDeselect(value: BasicOptionProps['value'], option: BasicOptionProps): void;
  40. notifyBlur(event: any): void;
  41. notifyFocus(event: any): void;
  42. notifyListScroll(event: any): void;
  43. notifyMouseLeave(event: any): void;
  44. notifyMouseEnter(event: any): void;
  45. updateHovering(isHover: boolean): void;
  46. updateScrollTop(): void;
  47. }
  48. type PropValue = string | number | Record<string, any>;
  49. export default class SelectFoundation extends BaseFoundation<SelectAdapter> {
  50. constructor(adapter: SelectAdapter) {
  51. super({ ...adapter });
  52. }
  53. // keyboard event listner
  54. // eslint-disable-next-line @typescript-eslint/member-ordering
  55. _keydownHandler: (...arg: any[]) => void | null = null;
  56. init() {
  57. this._setDropdownWidth();
  58. const isDefaultOpen = this.getProp('defaultOpen');
  59. const isOpen = this.getProp('open');
  60. const originalOptions = this._collectOptions();
  61. this._setDefaultSelection(originalOptions);
  62. if (isDefaultOpen || isOpen) {
  63. this.open(undefined, originalOptions);
  64. }
  65. const autoFocus = this.getProp('autoFocus');
  66. if (autoFocus) {
  67. this.focus();
  68. }
  69. }
  70. focus() {
  71. this._focusTrigger();
  72. const isFilterable = this._isFilterable();
  73. this._adapter.updateFocusState(true);
  74. if (isFilterable) {
  75. this.toggle2SearchInput(true);
  76. }
  77. }
  78. _focusTrigger() {
  79. this._adapter.focusTrigger();
  80. // this.bindKeyBoardEvent();
  81. }
  82. destroy() {
  83. this._adapter.unregisterClickOutsideHandler();
  84. this.unBindKeyBoardEvent();
  85. }
  86. _setDropdownWidth() {
  87. const { style, dropdownMatchSelectWidth } = this.getProps();
  88. let width;
  89. if (dropdownMatchSelectWidth) {
  90. if (style && isNumber(style.width)) {
  91. width = style.width;
  92. } else if (style && isString(style.width) && !style.width.includes('%')) {
  93. width = style.width;
  94. } else {
  95. width = this._adapter.getTriggerWidth();
  96. }
  97. this._adapter.setOptionWrapperWidth(width);
  98. }
  99. }
  100. _collectOptions() {
  101. const originalOptions = this._adapter.getOptionsFromChildren();
  102. this._adapter.updateOptions(originalOptions);
  103. // Reposition the drop-down layer
  104. this._adapter.rePositionDropdown();
  105. return originalOptions;
  106. }
  107. _setDefaultSelection(originalOptions: BasicOptionProps[]) {
  108. let { value } = this.getProps();
  109. const { defaultValue } = this.getProps();
  110. if (this._isControlledComponent()) {
  111. // do nothing
  112. } else {
  113. value = defaultValue;
  114. }
  115. this._update(value, originalOptions);
  116. }
  117. // call when props.optionList change
  118. handleOptionListChange() {
  119. const newOptionList = this._collectOptions();
  120. const { selections } = this.getStates();
  121. this.updateOptionsActiveStatus(selections, newOptionList);
  122. // reset focusIndex
  123. const { defaultActiveFirstOption } = this.getProps();
  124. if (defaultActiveFirstOption) {
  125. this._adapter.updateFocusIndex(0);
  126. }
  127. }
  128. // In uncontrolled mode, when props.optionList change,
  129. // but already had defaultValue or choose some option
  130. handleOptionListChangeHadDefaultValue() {
  131. const selections = this.getState('selections');
  132. let value;
  133. const { onChangeWithObject } = this.getProps();
  134. const isMultiple = this._isMultiple();
  135. switch (true) {
  136. case isMultiple && Boolean(selections.size):
  137. try {
  138. value = [...selections].map(item =>
  139. // At this point item1 is directly the object
  140. (onChangeWithObject ? item[1] : item[1].value)
  141. );
  142. } catch (error) {
  143. value = [];
  144. }
  145. break;
  146. case isMultiple && !selections.size:
  147. value = [];
  148. break;
  149. case !isMultiple && Boolean(selections.size):
  150. try {
  151. value = onChangeWithObject ? [...selections][0][1] : [...selections][0][1].value;
  152. } catch (error) {}
  153. break;
  154. case !isMultiple && !selections.size:
  155. break;
  156. default:
  157. break;
  158. }
  159. const originalOptions = this._adapter.getOptionsFromChildren();
  160. this._update(value, originalOptions);
  161. }
  162. // call when props.value change
  163. handleValueChange(value: PropValue) {
  164. const { allowCreate } = this.getProps();
  165. let originalOptions;
  166. // AllowCreate and controlled mode, no need to re-collect optionList
  167. if (allowCreate && this._isControlledComponent()) {
  168. originalOptions = this.getState('options') as BasicOptionProps[];
  169. originalOptions.forEach(item => (item._show = true));
  170. } else {
  171. // originalOptions = this.getState('options');
  172. // The options in state cannot be used directly here, 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
  173. originalOptions = this._adapter.getOptionsFromChildren();
  174. }
  175. // Multi-selection, controlled mode, you need to reposition the drop-down menu after updating
  176. this._adapter.rePositionDropdown();
  177. this._update(value, originalOptions);
  178. }
  179. // Update the selected item in the selection box
  180. _update(propValue: PropValue, originalOptions: BasicOptionProps[]) {
  181. let selections;
  182. if (!this._isMultiple()) {
  183. // Radio
  184. selections = this._updateSingle(propValue, originalOptions);
  185. } else {
  186. selections = this._updateMultiple(propValue as (PropValue)[], originalOptions);
  187. }
  188. // Update the text in the selection box
  189. this._adapter.updateSelection(selections);
  190. // Update the selected item in the drop-down box
  191. this.updateOptionsActiveStatus(selections, originalOptions);
  192. }
  193. // Optionally selected updates (when components are mounted, or after value changes)
  194. _updateSingle(propValue: PropValue, originalOptions: BasicOptionProps[]) {
  195. const selections = new Map();
  196. const { onChangeWithObject } = this.getProps();
  197. // 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
  198. const selectedValue = onChangeWithObject && typeof propValue !== 'undefined' ? (propValue as BasicOptionProps).value : propValue;
  199. const selectedOptions = originalOptions.filter(option => option.value === selectedValue);
  200. const noMatchOptionInList = !selectedOptions.length && typeof selectedValue !== 'undefined';
  201. // If the current value, there is a matching option in the optionList
  202. if (selectedOptions.length) {
  203. const selectedOption = selectedOptions[0];
  204. const optionExist = { ...selectedOption };
  205. // if (onChangeWithObject) {
  206. // OptionExist = {... propValue }; // value is the object with the'value 'Key
  207. // }
  208. selections.set(optionExist.label, optionExist);
  209. } else if (noMatchOptionInList) {
  210. // 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
  211. let optionNotExist = { value: propValue, label: propValue, _notExist: true };
  212. if (onChangeWithObject) {
  213. optionNotExist = { ...propValue as BasicOptionProps, _notExist: true } as any;
  214. }
  215. selections.set(optionNotExist.label, optionNotExist);
  216. }
  217. return selections;
  218. }
  219. // Multi-selected option update (when the component is mounted, or after the value changes)
  220. _updateMultiple(propValue: PropValue[], originalOptions: BasicOptionProps[]) {
  221. const nowSelections = this.getState('selections');
  222. let selectedOptionList: any[] = [];
  223. // Multiple selection is to determine whether it is an array to avoid the problem of defaultValue/value incoming string error
  224. const propValueIsArray = Array.isArray(propValue);
  225. this.checkMultipleProps();
  226. // 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
  227. if (nowSelections.size) {
  228. selectedOptionList = [...nowSelections].map(item => item[1]);
  229. }
  230. const selections = new Map();
  231. let selectedValues = propValue;
  232. const { onChangeWithObject } = this.getProps();
  233. // When onChangeWithObject is true
  234. if (onChangeWithObject && propValueIsArray) {
  235. selectedValues = propValue.map((item: BasicOptionProps) => item.value) as any;
  236. }
  237. if (propValueIsArray && selectedValues.length) {
  238. selectedValues.forEach((selectedValue: string, i: number) => {
  239. // The current value exists in the current optionList
  240. const index = originalOptions.findIndex(option => option.value === selectedValue);
  241. if (index !== -1) {
  242. selections.set(originalOptions[index].label, originalOptions[index]);
  243. } else {
  244. // 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
  245. const indexInSelectedList = selectedOptionList.findIndex(option => option.value === selectedValue);
  246. if (indexInSelectedList !== -1) {
  247. const option = selectedOptionList[indexInSelectedList];
  248. selections.set(option.label, option);
  249. } else {
  250. // 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
  251. let optionNotExist = { value: selectedValue, label: selectedValue, _notExist: true };
  252. onChangeWithObject ? (optionNotExist = { ...propValue[i] as any, _notExist: true }) : null;
  253. selections.set(optionNotExist.label, optionNotExist);
  254. }
  255. }
  256. });
  257. }
  258. return selections;
  259. }
  260. _isMultiple() {
  261. return this.getProp('multiple');
  262. }
  263. _isDisabled() {
  264. return this.getProp('disabled');
  265. }
  266. _isFilterable() {
  267. return Boolean(this.getProp('filter')); // filter can be boolean or function
  268. }
  269. handleClick(e: any) {
  270. const { clickToHide } = this.getProps();
  271. const { isOpen } = this.getStates();
  272. const isDisabled = this._isDisabled();
  273. if (isDisabled) {
  274. return;
  275. } else if (!isOpen) {
  276. this.open();
  277. this._notifyFocus(e);
  278. } else if (isOpen && clickToHide) {
  279. this.close(e);
  280. } else if (isOpen && !clickToHide) {
  281. this.focusInput();
  282. }
  283. }
  284. open(acInput?: string, originalOptions?: BasicOptionProps[]) {
  285. const isFilterable = this._isFilterable();
  286. const options = originalOptions || this.getState('options');
  287. // When searchable, when the drop-down box expands
  288. if (isFilterable) {
  289. // Also clears the options filter to show all candidates
  290. // Options created dynamically but not selected are also filtered out
  291. const sugInput = '';
  292. const newOptions = this._filterOption(options, sugInput).filter(item => !item._inputCreateOnly);
  293. this._adapter.updateOptions(newOptions);
  294. this.toggle2SearchInput(true);
  295. }
  296. this._adapter.openMenu();
  297. this._setDropdownWidth();
  298. this._adapter.notifyDropdownVisibleChange(true);
  299. this.bindKeyBoardEvent();
  300. this._adapter.registerClickOutsideHandler((e: MouseEvent) => {
  301. this.close(e);
  302. });
  303. }
  304. toggle2SearchInput(isShow: boolean) {
  305. if (isShow) {
  306. this._adapter.toggleInputShow(isShow, () => this.focusInput());
  307. } else {
  308. this._adapter.toggleInputShow(isShow, () => undefined);
  309. }
  310. }
  311. close(e?: any) {
  312. const isFilterable = this._isFilterable();
  313. if (isFilterable) {
  314. this.unBindKeyBoardEvent();
  315. this.clearInput();
  316. this.toggle2SearchInput(false);
  317. }
  318. this._adapter.closeMenu();
  319. this._adapter.notifyDropdownVisibleChange(false);
  320. this.unBindKeyBoardEvent();
  321. this._notifyBlur(e);
  322. this._adapter.unregisterClickOutsideHandler();
  323. this._adapter.updateFocusState(false);
  324. }
  325. onSelect(option: BasicOptionProps, optionIndex: number, event: MouseEvent | KeyboardEvent) {
  326. const isDisabled = this._isDisabled();
  327. if (isDisabled) {
  328. return;
  329. }
  330. // If the allowCreate dynamically created option is selected, onCreate needs to be triggered
  331. if (option._inputCreateOnly) {
  332. this._adapter.notifyCreate(option);
  333. }
  334. const isMultiple = this._isMultiple();
  335. if (!isMultiple) {
  336. this._handleSingleSelect(option, event);
  337. } else {
  338. this._handleMultipleSelect(option, event);
  339. }
  340. this._adapter.updateFocusIndex(optionIndex);
  341. }
  342. _handleSingleSelect({ value, label, ...rest }: BasicOptionProps, event: any) {
  343. const selections = new Map().set(label, { value, label, ...rest });
  344. // First trigger onSelect, then trigger onChange
  345. this._notifySelect(value, { value, label, ...rest });
  346. // If it is a controlled component, directly notify
  347. if (this._isControlledComponent()) {
  348. this._notifyChange(selections);
  349. this.close(event);
  350. } else {
  351. this._adapter.updateSelection(selections);
  352. // notify user
  353. this._notifyChange(selections);
  354. // Update the selected item in the drop-down box
  355. this.close(event);
  356. this.updateOptionsActiveStatus(selections);
  357. }
  358. }
  359. _handleMultipleSelect({ value, label, ...rest }: BasicOptionProps, event: MouseEvent | KeyboardEvent) {
  360. const maxLimit = this._adapter.getMaxLimit();
  361. const selections = this._adapter.getSelections();
  362. if (selections.has(label)) {
  363. this._notifyDeselect(value, { value, label, ...rest });
  364. selections.delete(label);
  365. } else if (maxLimit && selections.size === maxLimit) {
  366. this._adapter.notifyMaxLimit({ value, label, ...rest });
  367. return;
  368. } else {
  369. this._notifySelect(value, { value, label, ...rest });
  370. selections.set(label, { value, label, ...rest });
  371. }
  372. if (this._isControlledComponent()) {
  373. // Controlled components, directly notified
  374. this._notifyChange(selections);
  375. if (this._isFilterable()) {
  376. this.clearInput();
  377. this.focusInput();
  378. }
  379. } else {
  380. // Uncontrolled components, update ui
  381. this._adapter.updateSelection(selections);
  382. // 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
  383. this._adapter.rePositionDropdown();
  384. let { options } = this.getStates();
  385. // Searchable filtering, when selected, resets Input
  386. if (this._isFilterable()) {
  387. this.clearInput();
  388. this.focusInput();
  389. // At the same time, the filtering of options is also cleared, in order to show all candidates
  390. const sugInput = '';
  391. options = this._filterOption(options, sugInput);
  392. }
  393. this.updateOptionsActiveStatus(selections, options);
  394. this._notifyChange(selections);
  395. }
  396. }
  397. clearSelected() {
  398. const selections = new Map();
  399. if (this._isControlledComponent()) {
  400. this._notifyChange(selections);
  401. this._adapter.notifyClear();
  402. } else {
  403. this._adapter.updateSelection(selections);
  404. this.updateOptionsActiveStatus(selections);
  405. this._notifyChange(selections);
  406. this._adapter.notifyClear();
  407. }
  408. // when call manually by ref method
  409. const { isOpen } = this.getStates();
  410. if (isOpen) {
  411. this._adapter.rePositionDropdown();
  412. }
  413. }
  414. // Update the selected item in the drop-down box
  415. updateOptionsActiveStatus(selections: Map<any, any>, options: BasicOptionProps[] = this.getState('options')) {
  416. const { allowCreate } = this.getProps();
  417. const newOptions = options.map(option => {
  418. if (selections.has(option.label)) {
  419. option._selected = true;
  420. if (allowCreate) {
  421. delete option._inputCreateOnly;
  422. }
  423. } else {
  424. if (option._inputCreateOnly) {
  425. option._show = false;
  426. }
  427. option._selected = false;
  428. }
  429. return option;
  430. });
  431. this._adapter.updateOptions(newOptions);
  432. }
  433. removeTag(item: BasicOptionProps) {
  434. const selections = this._adapter.getSelections();
  435. selections.delete(item.label);
  436. if (this._isControlledComponent()) {
  437. this._notifyDeselect(item.value, item);
  438. this._notifyChange(selections);
  439. } else {
  440. this._notifyDeselect(item.value, item);
  441. this._adapter.updateSelection(selections);
  442. this.updateOptionsActiveStatus(selections);
  443. // Repostion drop-down layer, because the selection may have changed the number of rows, resulting in a height change
  444. this._adapter.rePositionDropdown();
  445. this._notifyChange(selections);
  446. }
  447. }
  448. clearInput() {
  449. this._adapter.updateInputValue('');
  450. }
  451. focusInput() {
  452. this._adapter.focusInput();
  453. this._adapter.updateFocusState(true);
  454. }
  455. handleInputChange(sugInput: string) {
  456. // Input is a controlled component, so the value needs to be updated
  457. this._adapter.updateInputValue(sugInput);
  458. const { options, isOpen } = this.getStates();
  459. const { allowCreate, remote } = this.getProps();
  460. let optionsAfterFilter = options;
  461. if (!remote) {
  462. // Filter options based on input
  463. optionsAfterFilter = this._filterOption(options, sugInput);
  464. }
  465. // When allowClear is true, an entry can be created. You need to include the current input as a new Option input
  466. optionsAfterFilter = this._createOptionByInput(allowCreate, optionsAfterFilter, sugInput);
  467. this._adapter.updateOptions(optionsAfterFilter);
  468. this._adapter.notifySearch(sugInput);
  469. // 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
  470. if (this._isMultiple()) {
  471. this._adapter.rePositionDropdown();
  472. }
  473. }
  474. _filterOption(originalOptions: BasicOptionProps[], sugInput: string) {
  475. const filter = this.getProp('filter');
  476. if (!filter) {
  477. // 1. No filtering
  478. return originalOptions;
  479. } else if (typeof filter === 'boolean' && filter) {
  480. // 2. When true, the default filter is used
  481. const input = sugInput.toLowerCase();
  482. return originalOptions.map(option => {
  483. const label = option.label.toString().toLowerCase();
  484. const groupLabel = option._parentGroup && option._parentGroup.label;
  485. const matchOption = label.includes(input);
  486. const matchGroup = isString(groupLabel) && groupLabel.toLowerCase().includes(input);
  487. if (matchOption || matchGroup) {
  488. option._show = true;
  489. } else {
  490. option._show = false;
  491. }
  492. return option;
  493. });
  494. } else if (typeof filter === 'function') {
  495. // 3. When passing in a custom function, use a custom function for filtering
  496. return originalOptions.map(option => {
  497. filter(sugInput, option) ? (option._show = true) : (option._show = false);
  498. return option;
  499. });
  500. }
  501. return undefined;
  502. }
  503. _createOptionByInput(allowCreate: boolean, optionsAfterFilter: BasicOptionProps[], sugInput: string) {
  504. if (allowCreate) {
  505. if (sugInput) {
  506. // optionsAfterFilter clone ??? needClone ?
  507. const newOptionByInput = {
  508. _show: true,
  509. _selected: false,
  510. value: sugInput,
  511. label: sugInput,
  512. // True indicates that the option was dynamically created during user filtering
  513. _inputCreateOnly: true,
  514. };
  515. let createOptionIndex = -1;
  516. let matchOptionIndex = -1;
  517. optionsAfterFilter.forEach((option, index) => {
  518. if (!option._show && !option._inputCreateOnly) {
  519. return;
  520. }
  521. // The matching algorithm is not necessarily through labels?
  522. if (option.label === sugInput) {
  523. matchOptionIndex = index;
  524. }
  525. if (option._inputCreateOnly) {
  526. createOptionIndex = index;
  527. option.value = sugInput;
  528. option.label = sugInput;
  529. option._show = true;
  530. }
  531. });
  532. if (createOptionIndex === -1 && matchOptionIndex === -1) {
  533. optionsAfterFilter.push(newOptionByInput);
  534. }
  535. if (matchOptionIndex !== -1) {
  536. optionsAfterFilter = optionsAfterFilter.filter(item => !item._inputCreateOnly);
  537. }
  538. } else {
  539. // Delete input unselected items
  540. optionsAfterFilter = optionsAfterFilter.filter(item => !item._inputCreateOnly);
  541. }
  542. }
  543. // TODO Promise supports asynchronous creation
  544. return optionsAfterFilter;
  545. }
  546. bindKeyBoardEvent() {
  547. this._keydownHandler = event => {
  548. this._handleKeyDown(event);
  549. };
  550. this._adapter.registerKeyDown(this._keydownHandler);
  551. }
  552. unBindKeyBoardEvent() {
  553. if (this._keydownHandler) {
  554. this._adapter.unregisterKeyDown();
  555. }
  556. }
  557. _handleKeyDown(event: KeyboardEvent) {
  558. const key = event.keyCode;
  559. const { isOpen } = this.getStates();
  560. const { loading } = this.getProps();
  561. if (!isOpen || loading) {
  562. return;
  563. }
  564. switch (key) {
  565. case KeyCode.UP:
  566. // Prevent Input's cursor from following
  567. // Prevent Input cursor from following
  568. event.preventDefault();
  569. this._handleArrowKeyDown(-1);
  570. break;
  571. case KeyCode.DOWN:
  572. // Prevent Input's cursor from following
  573. // Prevent Input cursor from following
  574. event.preventDefault();
  575. this._handleArrowKeyDown(1);
  576. break;
  577. case KeyCode.BACKSPACE:
  578. this._handleBackspaceKeyDown();
  579. break;
  580. case KeyCode.ENTER:
  581. // internal-issues:302
  582. // prevent trigger form’s submit when use in form
  583. event.preventDefault();
  584. event.stopPropagation();
  585. this._handleEnterKeyDown(event);
  586. break;
  587. case KeyCode.ESC:
  588. case KeyCode.TAB:
  589. this.close(event);
  590. break;
  591. default:
  592. break;
  593. }
  594. }
  595. _getEnableFocusIndex(offset: number) {
  596. const { focusIndex, options } = this.getStates();
  597. const visibleOptions = options.filter((item: BasicOptionProps) => item._show);
  598. // let visibleOptions = options;
  599. const optionsLength = visibleOptions.length;
  600. let index = focusIndex + offset;
  601. if (index < 0) {
  602. index = optionsLength - 1;
  603. }
  604. if (index >= optionsLength) {
  605. index = 0;
  606. }
  607. // avoid newIndex option is disabled
  608. if (offset > 0) {
  609. let nearestActiveOption = -1;
  610. for (let i = 0; i < visibleOptions.length; i++) {
  611. const optionIsActive = !visibleOptions[i].disabled;
  612. if (optionIsActive) {
  613. nearestActiveOption = i;
  614. }
  615. if (nearestActiveOption >= index) {
  616. break;
  617. }
  618. }
  619. index = nearestActiveOption;
  620. } else {
  621. let nearestActiveOption = visibleOptions.length;
  622. for (let i = optionsLength - 1; i >= 0; i--) {
  623. const optionIsActive = !visibleOptions[i].disabled;
  624. if (optionIsActive) {
  625. nearestActiveOption = i;
  626. }
  627. if (nearestActiveOption <= index) {
  628. break;
  629. }
  630. }
  631. index = nearestActiveOption;
  632. }
  633. // console.log('new:' + index);
  634. this._adapter.updateFocusIndex(index);
  635. // TODO requires scrollIntoView
  636. }
  637. _handleArrowKeyDown(offset: number) {
  638. this._getEnableFocusIndex(offset);
  639. }
  640. _handleEnterKeyDown(event: KeyboardEvent) {
  641. const { isOpen, options, focusIndex } = this.getStates();
  642. if (focusIndex !== -1) {
  643. const visibleOptions = options.filter((item: BasicOptionProps) => item._show);
  644. const { length } = visibleOptions;
  645. // fix issue 1201
  646. if (length <= focusIndex) {
  647. return;
  648. }
  649. if (visibleOptions && length) {
  650. const selectedOption = visibleOptions[focusIndex];
  651. if (selectedOption.disabled) {
  652. return;
  653. }
  654. this.onSelect(selectedOption, focusIndex, event);
  655. }
  656. } else if (isOpen) {
  657. }
  658. }
  659. _handleBackspaceKeyDown() {
  660. if (this._isMultiple()) {
  661. const selections = this._adapter.getSelections();
  662. const { inputValue } = this.getStates();
  663. const length = selections.size;
  664. if (length && !inputValue) {
  665. const keys = [...selections.keys()];
  666. let index = length - 1;
  667. let targetLabel = keys[index];
  668. let targetItem = selections.get(targetLabel);
  669. let isAllDisabled = false;
  670. // can skip disabled item when remove trigger by backspace
  671. if (targetItem.disabled && index === 0) {
  672. return;
  673. }
  674. while (targetItem.disabled && index !== 0) {
  675. index = index - 1;
  676. targetLabel = keys[index];
  677. targetItem = selections.get(targetLabel);
  678. // eslint-disable-next-line
  679. if (index == 0 && targetItem.disabled) {
  680. isAllDisabled = true;
  681. }
  682. }
  683. if (!isAllDisabled) {
  684. this.removeTag(targetItem);
  685. }
  686. }
  687. }
  688. }
  689. _notifyChange(selections: Map<any, any>) {
  690. const { onChangeWithObject } = this.getProps();
  691. const stateSelections = this.getState('selections');
  692. let notifyVal;
  693. const selectionsProps = [...selections.values()];
  694. const isMultiple = this._isMultiple();
  695. const hasChange = this._diffSelections(selections, stateSelections, isMultiple);
  696. if (!hasChange) {
  697. return;
  698. }
  699. switch (true) {
  700. case onChangeWithObject:
  701. this._notifyChangeWithObject(selections);
  702. break;
  703. case !onChangeWithObject && !isMultiple:
  704. notifyVal = selectionsProps.length ? selectionsProps[0].value : undefined;
  705. this._adapter.notifyChange(notifyVal);
  706. break;
  707. case !onChangeWithObject && isMultiple:
  708. notifyVal = selectionsProps.length ? selectionsProps.map(props => props.value) : [];
  709. this._adapter.notifyChange(notifyVal);
  710. break;
  711. default:
  712. break;
  713. }
  714. }
  715. _removeInternalKey(option: BasicOptionProps) {
  716. delete option._parentGroup;
  717. delete option._show;
  718. delete option._selected;
  719. if ('_keyInOptionList' in option) {
  720. option.key = option._keyInOptionList;
  721. delete option._keyInOptionList;
  722. }
  723. return option;
  724. }
  725. _notifySelect(value: BasicOptionProps['value'], option: BasicOptionProps) {
  726. const newOption = this._removeInternalKey(option);
  727. this._adapter.notifySelect(value, newOption);
  728. }
  729. _notifyDeselect(value: BasicOptionProps['value'], option: BasicOptionProps) {
  730. const newOption = this._removeInternalKey(option);
  731. this._adapter.notifyDeselect(value, newOption);
  732. }
  733. _diffSelections(selections: Map<any, any>, oldSelections: Map<any, any>, isMultiple: boolean) {
  734. let diff = true;
  735. if (!isMultiple) {
  736. const selectionProps = [...selections.values()];
  737. const oldSelectionProps = [...oldSelections.values()];
  738. const optionLabel = selectionProps[0] ? selectionProps[0].label : selectionProps[0];
  739. const oldOptionLabel = oldSelectionProps[0] ? oldSelectionProps[0].label : oldSelectionProps[0];
  740. diff = !isEqual(optionLabel, oldOptionLabel);
  741. } else {
  742. // When multiple selection, there is no scene where the value is different between the two operations
  743. }
  744. return diff;
  745. }
  746. // When onChangeWithObject is true, the onChange input parameter is not only value, but also label and other parameters
  747. _notifyChangeWithObject(selections: Map<any, any>) {
  748. const stateSelections = this.getState('selections');
  749. const values = [];
  750. for (const item of selections.entries()) {
  751. let val = { label: item[0], ...item[1] };
  752. val = this._removeInternalKey(val);
  753. values.push(val);
  754. }
  755. if (!this._isMultiple()) {
  756. this._adapter.notifyChange(values[0]);
  757. } else {
  758. this._adapter.notifyChange(values);
  759. }
  760. }
  761. // Scenes that may trigger blur:
  762. // 1、clickOutSide
  763. // 2、click option / press enter, and then select complete(when multiple is false
  764. // 3、press esc when dropdown list open
  765. _notifyBlur(e: FocusEvent) {
  766. this._adapter.notifyBlur(e);
  767. }
  768. // Scenes that may trigger focus:
  769. // 1、click selection
  770. _notifyFocus(e: FocusEvent) {
  771. this._adapter.notifyFocus(e);
  772. }
  773. handleMouseEnter(e: MouseEvent) {
  774. this._adapter.updateHovering(true);
  775. this._adapter.notifyMouseEnter(e);
  776. }
  777. handleMouseLeave(e: MouseEvent) {
  778. this._adapter.updateHovering(false);
  779. this._adapter.notifyMouseLeave(e);
  780. }
  781. handleClearClick(e: MouseEvent) {
  782. this.clearInput();
  783. // TODO
  784. this.clearSelected();
  785. // prevent this click open dropdown
  786. e.stopPropagation();
  787. }
  788. handleKeyPress(e: KeyboardEvent) {
  789. if (e && e.key === ENTER_KEY) {
  790. this.handleClick(e);
  791. }
  792. }
  793. handleClearBtnEnterPress(e: KeyboardEvent) {
  794. if (isEnterPress(e)) {
  795. this.handleClearClick(e as any);
  796. }
  797. }
  798. handleOptionMouseEnter(optionIndex: number) {
  799. this._adapter.updateFocusIndex(optionIndex);
  800. }
  801. handleListScroll(e: any) {
  802. this._adapter.notifyListScroll(e);
  803. }
  804. // handleTriggerFocus(e) {
  805. // console.log('handleTriggerFocus');
  806. // this._adapter.updateFocusState(true);
  807. // }
  808. handleTriggerBlur(e: FocusEvent) {
  809. this._adapter.updateFocusState(false);
  810. const { filter, autoFocus } = this.getProps();
  811. const { isOpen, isFocus } = this.getStates();
  812. // Under normal circumstances, blur will be accompanied by dropdown close, so the notify of blur can be called uniformly in close
  813. // But when autoFocus, because dropdown is not expanded, you need to listen for the trigger's blur and trigger the notify callback
  814. if (autoFocus && isFocus && !isOpen) {
  815. // blur when autoFocus & not open dropdown yet
  816. this._notifyBlur(e);
  817. }
  818. }
  819. selectAll() {
  820. const { options } = this.getStates();
  821. const { onChangeWithObject } = this.getProps();
  822. let selectedValues = [];
  823. const isMultiple = this._isMultiple();
  824. if (!isMultiple) {
  825. console.warn(`[Semi Select]: It seems that you have called the selectAll method in the single-selection Select.
  826. Please note that this is not a legal way to use it`
  827. );
  828. return;
  829. }
  830. if (onChangeWithObject) {
  831. selectedValues = options;
  832. } else {
  833. selectedValues = options.map((option: BasicOptionProps) => option.value);
  834. }
  835. this.handleValueChange(selectedValues);
  836. this._adapter.notifyChange(selectedValues);
  837. }
  838. /**
  839. * Check whether the props
  840. * -defaultValue/value in multiple selection mode is array
  841. * @param {Object} props
  842. */
  843. checkMultipleProps(props?: Record<string, any>) {
  844. if (this._isMultiple()) {
  845. const currentProps = props ? props : this.getProps();
  846. const { defaultValue, value } = currentProps;
  847. const selectedValues = value || defaultValue;
  848. if (!isNullOrUndefined(selectedValues) && !Array.isArray(selectedValues)) {
  849. warning(true, '[Semi Select] defaultValue/value should be array type in multiple mode');
  850. }
  851. }
  852. }
  853. updateScrollTop() {
  854. this._adapter.updateScrollTop();
  855. }
  856. }