foundation.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. import { isEqual, get, difference, isUndefined, assign, cloneDeep, isEmpty, isNumber, includes } from 'lodash';
  2. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  3. import {
  4. filter,
  5. findAncestorKeys,
  6. calcCheckedKeysForUnchecked,
  7. calcCheckedKeysForChecked,
  8. calcCheckedKeys,
  9. findDescendantKeys,
  10. normalizeKeyList
  11. } from '../tree/treeUtil';
  12. import { Motion } from '../utils/type';
  13. import {
  14. convertDataToEntities,
  15. findKeysForValues,
  16. normalizedArr,
  17. isValid,
  18. calcMergeType
  19. } from './util';
  20. import { strings } from './constants';
  21. import isEnterPress from '../utils/isEnterPress';
  22. export interface BasicData {
  23. data: BasicCascaderData;
  24. disabled: boolean;
  25. key: string;
  26. searchText: any[];
  27. }
  28. export interface BasicEntities {
  29. [idx: string]: BasicEntity;
  30. }
  31. export interface BasicEntity {
  32. _notExist?: boolean;
  33. /* children list */
  34. children?: Array<BasicEntity>;
  35. /* treedata */
  36. data: BasicCascaderData;
  37. /* index */
  38. ind: number;
  39. /* key */
  40. key: string;
  41. /* node level */
  42. level: number;
  43. /* parent data */
  44. parent?: BasicEntity;
  45. /* parent key */
  46. parentKey?: string;
  47. /* key path */
  48. path: Array<string>;
  49. /* value path */
  50. valuePath: Array<string>;
  51. }
  52. export interface BasicCascaderData {
  53. [x: string]: any;
  54. value: string | number;
  55. label: any;
  56. disabled?: boolean;
  57. isLeaf?: boolean;
  58. loading?: boolean;
  59. children?: BasicCascaderData[];
  60. }
  61. export type CascaderType = 'large' | 'small' | 'default';
  62. /* The basic type of the value of Cascader */
  63. export type BasicSimpleValueType = string | number | BasicCascaderData;
  64. /* The value of Cascader */
  65. export type BasicValue = BasicSimpleValueType | Array<BasicSimpleValueType> | Array<Array<BasicSimpleValueType>>;
  66. export type ShowNextType = 'click' | 'hover';
  67. export interface BasicTriggerRenderProps {
  68. /* Props passed to Cascader by all users */
  69. componentProps: BasicCascaderProps;
  70. /* Whether to disable Cascader */
  71. disabled: boolean;
  72. /** The hierarchical position of the selected node in treeData,
  73. * as in the following example, when Zhejiang-Hangzhou-Xiaoshan
  74. * District is selected, the value here is 0-0-1 */
  75. value?: string | Set<string>;
  76. /* The input value of the current input box */
  77. inputValue: string;
  78. /* Cascader's placeholder */
  79. placeholder?: string;
  80. /** The function used to update the value of the input box. You
  81. * should call this function when the value of the Input component
  82. * customized by triggerRender is updated to synchronize the state
  83. * with Cascader. */
  84. onChange: (inputValue: string) => void;
  85. /* Function to clear the value */
  86. onClear: (e: any) => void;
  87. }
  88. export interface BasicScrollPanelProps {
  89. panelIndex: number;
  90. activeNode: BasicCascaderData;
  91. }
  92. export interface BasicCascaderProps {
  93. mouseEnterDelay?: number;
  94. mouseLeaveDelay?: number;
  95. separator?: string;
  96. arrowIcon?: any;
  97. changeOnSelect?: boolean;
  98. multiple?: boolean;
  99. autoMergeValue?: boolean;
  100. defaultValue?: BasicValue;
  101. disabled?: boolean;
  102. dropdownClassName?: string;
  103. dropdownStyle?: any;
  104. emptyContent?: any;
  105. filterLeafOnly?: boolean;
  106. motion?: Motion;
  107. filterTreeNode?: ((inputValue: string, treeNodeString: string) => boolean) | boolean;
  108. placeholder?: string;
  109. searchPlaceholder?: string;
  110. size?: CascaderType;
  111. className?: string;
  112. treeData?: Array<BasicCascaderData>;
  113. treeNodeFilterProp?: string;
  114. displayProp?: string;
  115. maxTagCount?: number;
  116. max?: number;
  117. showRestTagsPopover?: boolean;
  118. restTagsPopoverProps?: any;
  119. children?: any;
  120. zIndex?: number;
  121. value?: BasicValue;
  122. prefix?: any;
  123. suffix?: any;
  124. insetLabel?: any;
  125. style?: any;
  126. stopPropagation?: boolean | string;
  127. showClear?: boolean;
  128. autoAdjustOverflow?: boolean;
  129. defaultOpen?: boolean;
  130. onChangeWithObject?: boolean;
  131. bottomSlot?: any;
  132. topSlot?: any;
  133. showNext?: ShowNextType;
  134. disableStrictly?: boolean;
  135. leafOnly?: boolean;
  136. enableLeafClick?: boolean;
  137. preventScroll?: boolean;
  138. onClear?: () => void;
  139. triggerRender?: (props: BasicTriggerRenderProps) => any;
  140. onListScroll?: (e: any, panel: BasicScrollPanelProps) => void;
  141. loadData?: (selectOptions: BasicCascaderData[]) => Promise<void>;
  142. onLoad?: (newLoadedKeys: Set<string>, data: BasicCascaderData) => void;
  143. onDropdownVisibleChange?: (visible: boolean) => void;
  144. getPopupContainer?: () => HTMLElement;
  145. onChange?: (value: BasicValue) => void;
  146. onSearch?: (value: string) => void;
  147. onSelect?: (value: string | number | Array<string | number>) => void;
  148. onExceed?: (checkedItem: BasicEntity[]) => void;
  149. displayRender?: (selected: Array<string> | BasicEntity, idx?: number) => any;
  150. onBlur?: (e: any) => void;
  151. onFocus?: (e: any) => void;
  152. }
  153. export interface BasicCascaderInnerData {
  154. isOpen: boolean;
  155. rePosKey: number;
  156. keyEntities: BasicEntities;
  157. selectedKeys: Set<string>;
  158. activeKeys: Set<string>;
  159. filteredKeys: Set<string>;
  160. inputValue: string;
  161. isSearching: boolean;
  162. inputPlaceHolder: string;
  163. prevProps: BasicCascaderProps;
  164. isHovering: boolean;
  165. checkedKeys: Set<string>;
  166. halfCheckedKeys: Set<string>;
  167. resolvedCheckedKeys: Set<string>;
  168. loadedKeys: Set<string>;
  169. loadingKeys: Set<string>;
  170. loading: boolean;
  171. treeData?: Array<BasicCascaderData>;
  172. isFocus?: boolean;
  173. isInput?: boolean;
  174. disabledKeys?: Set<string>;
  175. showInput?: boolean;
  176. }
  177. export interface CascaderAdapter extends DefaultAdapter<BasicCascaderProps, BasicCascaderInnerData> {
  178. notifyClear?: () => void;
  179. updateInputValue: (value: string) => void;
  180. updateInputPlaceHolder: (value: string) => void;
  181. focusInput: () => void;
  182. registerClickOutsideHandler: (cb: (e: any) => void) => void;
  183. unregisterClickOutsideHandler: () => void;
  184. rePositionDropdown: () => void;
  185. updateStates: (states: Partial<BasicCascaderInnerData>) => void;
  186. openMenu: () => void;
  187. closeMenu: (cb?: () => void) => void;
  188. updateSelection: (selectedKeys: Set<string>) => void;
  189. notifyChange: (value: BasicValue) => void;
  190. notifySelect: (selected: string | number | Array<string | number>) => void;
  191. notifyOnSearch: (input: string) => void;
  192. notifyFocus: (e: any) => void;
  193. notifyBlur: (e: any) => void;
  194. notifyDropdownVisibleChange: (visible: boolean) => void;
  195. toggleHovering: (bool: boolean) => void;
  196. notifyLoadData: (selectedOpt: BasicCascaderData[], callback: (data?: BasicEntities) => void) => void;
  197. notifyOnLoad: (newLoadedKeys: Set<string>, data: BasicCascaderData) => void;
  198. notifyListScroll: (e: any, panel: BasicScrollPanelProps) => void;
  199. notifyOnExceed: (data: BasicEntity[]) => void;
  200. toggleInputShow: (show: boolean, cb: () => void) => void;
  201. updateFocusState: (focus: boolean) => void,
  202. }
  203. // eslint-disable-next-line max-len
  204. export default class CascaderFoundation extends BaseFoundation<CascaderAdapter, BasicCascaderProps, BasicCascaderInnerData> {
  205. constructor(adapter: CascaderAdapter) {
  206. super({ ...adapter });
  207. }
  208. init() {
  209. const isOpen = this.getProp('open') || this.getProp('defaultOpen');
  210. this.collectOptions(true);
  211. if (isOpen && !this._isDisabled()) {
  212. this.open();
  213. }
  214. }
  215. destroy() {
  216. this._adapter.unregisterClickOutsideHandler();
  217. }
  218. _isDisabled() {
  219. return this.getProp('disabled');
  220. }
  221. _isFilterable() {
  222. return Boolean(this.getProp('filterTreeNode')); // filter can be boolean or function
  223. }
  224. _notifyChange(item: BasicEntity | BasicData | Set<string>) {
  225. const { onChangeWithObject, multiple } = this.getProps();
  226. const valueProp: string | any[] = onChangeWithObject ? [] : 'value';
  227. if (multiple) {
  228. const valuePath: BasicValue = [];
  229. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  230. // @ts-ignore
  231. item.forEach((checkedKey: string) => {
  232. const valuePathItem = this.getItemPropPath(checkedKey, valueProp);
  233. valuePath.push(valuePathItem as any);
  234. });
  235. this._adapter.notifyChange(valuePath);
  236. } else {
  237. const valuePath = isUndefined(item) || !('key' in item) ?
  238. [] :
  239. this.getItemPropPath(item.key, valueProp);
  240. this._adapter.notifyChange(valuePath);
  241. }
  242. }
  243. _isLeaf(item: BasicCascaderData) {
  244. if (this.getProp('loadData')) {
  245. return Boolean(item.isLeaf);
  246. }
  247. return !item.children || !item.children.length;
  248. }
  249. _clearInput() {
  250. this._adapter.updateInputValue('');
  251. }
  252. // Scenes that may trigger blur:
  253. // 1、clickOutSide
  254. _notifyBlur(e: any) {
  255. this._adapter.notifyBlur(e);
  256. }
  257. // Scenes that may trigger focus:
  258. // 1、click selection
  259. _notifyFocus(e: any) {
  260. this._adapter.notifyFocus(e);
  261. }
  262. _isOptionDisabled(key: string, keyEntities: BasicEntities) {
  263. const isDisabled = findAncestorKeys([key], keyEntities, true)
  264. .some(item => keyEntities[item].data.disabled);
  265. return isDisabled;
  266. }
  267. getCopyFromState(items: string | string[]) {
  268. const res: Partial<BasicCascaderInnerData> = {};
  269. normalizedArr(items).forEach(key => {
  270. res[key] = cloneDeep(this.getState(key));
  271. });
  272. return res;
  273. }
  274. // prop: is array, return all data
  275. getItemPropPath(selectedKey: string, prop: string | any[], keyEntities?: BasicEntities) {
  276. const searchMap = keyEntities || this.getState('keyEntities');
  277. const selectedItem = searchMap[selectedKey];
  278. let path = [];
  279. if (!selectedItem) {
  280. // do nothing
  281. } else if (selectedItem._notExist) {
  282. path = selectedItem.path;
  283. } else {
  284. const keyPath = selectedItem.path;
  285. path = Array.isArray(prop) ?
  286. keyPath.map((key: string) => searchMap[key].data) :
  287. keyPath.map((key: string) => searchMap[key].data[prop]);
  288. }
  289. return path;
  290. }
  291. _getCacheValue(keyEntities: BasicEntities) {
  292. const { selectedKeys } = this.getStates();
  293. const selectedKey = Array.from(selectedKeys as Set<string>)[0];
  294. let cacheValue;
  295. /* selectedKeys does not match keyEntities */
  296. if (isEmpty(keyEntities[selectedKey])) {
  297. if (includes(selectedKey, 'not-exist-')) {
  298. /* Get the value behind not-exist- */
  299. // eslint-disable-next-line prefer-destructuring
  300. const targetValue = selectedKey.match(/not-exist-(\S*)/)[1];
  301. // eslint-disable-next-line max-depth
  302. if (isEmpty(keyEntities[targetValue])) {
  303. cacheValue = targetValue;
  304. } else {
  305. /**
  306. * 典型的场景是: 假设我们选中了 0-0 这个节点,此时 selectedKeys=Set('0-0'),
  307. * 输入框会显示 0-0 的 label。当 treeData 发生更新,假设此时 0-0 在 treeData
  308. * 中不存在,则 selectedKeys=Set('not-exist-0-0'),此时输入框显示的是 0-0,
  309. * 也就是显示 not-exist- 后的内容。当treeData再次更新,假设此时 0-0 在 treeData
  310. * 中存在,则 selectedKeys=Set('0-0'),此时输入框显示 0-0 的 label。 这个地
  311. * 方做的操作就是,为了例子中第二次更新后 0-0 label 能够正常显示。
  312. */
  313. /**
  314. * The typical scenario is: suppose we select the 0-0 node, at this time
  315. * selectedKeys=Set('0-0'), the input box will display a 0-0 label. When
  316. * treeData is updated, assuming 0-0 does not exist in treeData at this
  317. * time, then selectedKeys=Set('not-exist-0-0'), at this time the input
  318. * box displays 0-0, which means not-exist -After the content. When treeData
  319. * is updated again, assuming that 0-0 exists in treeData at this time,
  320. * then selectedKeys=Set('0-0'), and the input box displays a label of
  321. * 0-0 at this time. The operation done here is for the 0-0 label to be
  322. * displayed normally after the second update in the example.
  323. */
  324. cacheValue = keyEntities[targetValue].valuePath;
  325. }
  326. } else {
  327. cacheValue = selectedKey;
  328. }
  329. /* selectedKeys match keyEntities */
  330. } else {
  331. /* selectedKeys match keyEntities */
  332. cacheValue = keyEntities[selectedKey].valuePath;
  333. }
  334. return cacheValue;
  335. }
  336. collectOptions(init = false) {
  337. const { treeData, value, defaultValue } = this.getProps();
  338. const keyEntities = convertDataToEntities(treeData);
  339. this._adapter.rePositionDropdown();
  340. let cacheValue;
  341. /* when mount */
  342. if (init) {
  343. cacheValue = defaultValue;
  344. } else if (!isEmpty(keyEntities)) {
  345. cacheValue = this._getCacheValue(keyEntities);
  346. }
  347. const selectedValue = !this._isControlledComponent() ? cacheValue : value;
  348. if (isValid(selectedValue)) {
  349. this.updateSelectedKey(selectedValue, keyEntities);
  350. } else {
  351. this._adapter.updateStates({ keyEntities });
  352. }
  353. }
  354. // call when props.value change
  355. handleValueChange(value: BasicValue) {
  356. const { keyEntities } = this.getStates();
  357. const { multiple } = this.getProps();
  358. !multiple && this.updateSelectedKey(value, keyEntities);
  359. }
  360. /**
  361. * When single selection, the clear objects of
  362. * selectedKeys, activeKeys, filteredKeys, input, etc.
  363. */
  364. _getClearSelectedKey(filterable: boolean) {
  365. const updateStates: Partial<BasicCascaderInnerData> = {};
  366. const { searchPlaceholder, placeholder, multiple } = this.getProps();
  367. updateStates.selectedKeys = new Set([]);
  368. updateStates.activeKeys = new Set([]);
  369. updateStates.filteredKeys = new Set([]);
  370. if (filterable && !multiple) {
  371. updateStates.inputPlaceHolder = searchPlaceholder || placeholder || '';
  372. updateStates.inputValue = '';
  373. }
  374. return updateStates;
  375. }
  376. updateSelectedKey(value: BasicValue, keyEntities: BasicEntities) {
  377. const { changeOnSelect, onChangeWithObject, multiple } = this.getProps();
  378. const {
  379. activeKeys,
  380. loadingKeys,
  381. loading,
  382. keyEntities: keyEntityState,
  383. selectedKeys: selectedKeysState
  384. } = this.getStates();
  385. const filterable = this._isFilterable();
  386. const loadingActive = [...activeKeys].filter(i => loadingKeys.has(i));
  387. const valuePath = onChangeWithObject ? normalizedArr(value).map(i => i.value) : normalizedArr(value);
  388. const selectedKeys = findKeysForValues(valuePath, keyEntities);
  389. let updateStates: Partial<BasicCascaderInnerData> = {};
  390. if (selectedKeys.length) {
  391. const selectedKey = selectedKeys[0];
  392. const selectedItem = keyEntities[selectedKey];
  393. /**
  394. * When changeOnSelect is turned on, or the target option is a leaf option,
  395. * the option is considered to be selected, even if the option is disabled
  396. */
  397. if (changeOnSelect || this._isLeaf(selectedItem.data)) {
  398. updateStates.selectedKeys = new Set([selectedKey]);
  399. if (!loadingActive.length) {
  400. updateStates.activeKeys = new Set(selectedItem.path);
  401. }
  402. if (filterable && !multiple) {
  403. const displayText = this.renderDisplayText(selectedKey, keyEntities);
  404. updateStates.inputPlaceHolder = displayText;
  405. updateStates.inputValue = displayText;
  406. }
  407. /**
  408. * If selectedKeys does not meet the update conditions,
  409. * and state.selectedKeys is the same as selectedKeys
  410. * at this time, state.selectedKeys should be cleared.
  411. * A typical scenario is:
  412. * The originally selected node is the leaf node, but
  413. * after props.treeData is dynamically updated, the node
  414. * is a non-leaf node. At this point, selectedKeys should
  415. * be cleared.
  416. */
  417. } else if (isEqual(selectedKeys, Array.from(selectedKeysState))) {
  418. updateStates = this._getClearSelectedKey(filterable);
  419. }
  420. } else if (value && (value as any).length) {
  421. const val = valuePath[valuePath.length - 1];
  422. const key = `not-exist-${val}`;
  423. const optionNotExist = {
  424. data: {
  425. label: val,
  426. value: val,
  427. },
  428. key,
  429. path: valuePath,
  430. _notExist: true,
  431. };
  432. updateStates.selectedKeys = new Set([key]);
  433. if (filterable && !multiple) {
  434. const displayText = this._defaultRenderText(valuePath);
  435. updateStates.inputPlaceHolder = displayText;
  436. updateStates.inputValue = displayText;
  437. }
  438. keyEntities[key] = optionNotExist as BasicEntity;
  439. // Fix: 1155, if the data is loaded asynchronously to update treeData, the emptying operation should not be done when entering the updateSelectedKey method
  440. } else if (loading) {
  441. // Use assign to avoid overwriting the'not-exist- * 'property of keyEntities after asynchronous loading
  442. // Overwriting'not-exist- * 'will cause selectionContent to be emptied unexpectedly when clicking on a dropDown item
  443. updateStates.keyEntities = assign(keyEntityState, keyEntities);
  444. this._adapter.updateStates(updateStates);
  445. return;
  446. } else {
  447. updateStates = this._getClearSelectedKey(filterable);
  448. }
  449. updateStates.keyEntities = keyEntities;
  450. this._adapter.updateStates(updateStates);
  451. }
  452. open() {
  453. const filterable = this._isFilterable();
  454. const { multiple } = this.getProps();
  455. this._adapter.openMenu();
  456. if (filterable) {
  457. this._clearInput();
  458. !multiple && this.toggle2SearchInput(true);
  459. }
  460. if (this._isControlledComponent()) {
  461. this.reCalcActiveKeys();
  462. }
  463. this._adapter.notifyDropdownVisibleChange(true);
  464. this._adapter.registerClickOutsideHandler(e => this.close(e));
  465. }
  466. reCalcActiveKeys() {
  467. const { selectedKeys, activeKeys, keyEntities } = this.getStates();
  468. const selectedKey = [...selectedKeys][0];
  469. const selectedItem = keyEntities[selectedKey];
  470. if (!selectedItem) {
  471. return;
  472. }
  473. const newActiveKeys: Set<string> = new Set(selectedItem.path);
  474. if (!isEqual(newActiveKeys, activeKeys)) {
  475. this._adapter.updateStates({
  476. activeKeys: newActiveKeys,
  477. });
  478. }
  479. }
  480. close(e: any, key?: string) {
  481. const { multiple } = this.getProps();
  482. this._adapter.closeMenu();
  483. this._adapter.notifyDropdownVisibleChange(false);
  484. this._adapter.unregisterClickOutsideHandler();
  485. if (this._isFilterable()) {
  486. const { selectedKeys } = this.getStates();
  487. let inputValue = '';
  488. if (key && !multiple) {
  489. inputValue = this.renderDisplayText(key);
  490. } else if (selectedKeys.size && !multiple) {
  491. inputValue = this.renderDisplayText([...selectedKeys][0]);
  492. }
  493. this._adapter.updateStates({ inputValue });
  494. !multiple && this.toggle2SearchInput(false);
  495. !multiple && this._adapter.updateFocusState(false);
  496. }
  497. this._notifyBlur(e);
  498. }
  499. toggle2SearchInput(isShow: boolean) {
  500. if (isShow) {
  501. this._adapter.toggleInputShow(isShow, () => this.focusInput());
  502. } else {
  503. this._adapter.toggleInputShow(isShow, () => undefined);
  504. }
  505. }
  506. focusInput() {
  507. this._adapter.focusInput();
  508. this._adapter.updateFocusState(true);
  509. }
  510. getMergedMotion = () => {
  511. const { motion } = this.getProps();
  512. const { isSearching } = this.getStates();
  513. if (isSearching) {
  514. const mergedMotion =
  515. typeof motion === 'undefined' || motion ?
  516. {
  517. ...motion,
  518. didLeave: (...args: any) => {
  519. const didLeave = get(motion, 'didLeave');
  520. if (typeof didLeave === 'function') {
  521. didLeave(...args);
  522. }
  523. this._adapter.updateStates({ isSearching: false });
  524. },
  525. } :
  526. false;
  527. return mergedMotion;
  528. }
  529. return motion;
  530. };
  531. handleItemClick(e: any, item: BasicEntity | BasicData) {
  532. const isDisabled = this._isDisabled();
  533. if (isDisabled) {
  534. return;
  535. }
  536. this.handleSingleSelect(e, item);
  537. this._adapter.rePositionDropdown();
  538. }
  539. handleItemHover(e: any, item: BasicEntity) {
  540. const isDisabled = this._isDisabled();
  541. if (isDisabled) {
  542. return;
  543. }
  544. this.handleShowNextByHover(item);
  545. }
  546. handleShowNextByHover(item: BasicEntity) {
  547. const { keyEntities } = this.getStates();
  548. const { data, key } = item;
  549. const isLeaf = this._isLeaf(data);
  550. const activeKeys = keyEntities[key].path;
  551. this._adapter.updateStates({
  552. activeKeys: new Set(activeKeys)
  553. });
  554. if (!isLeaf) {
  555. this.notifyIfLoadData(item);
  556. }
  557. }
  558. onItemCheckboxClick(item: BasicEntity | BasicData) {
  559. const isDisabled = this._isDisabled();
  560. if (isDisabled) {
  561. return;
  562. }
  563. this._handleMultipleSelect(item);
  564. this._adapter.rePositionDropdown();
  565. }
  566. handleClick(e: any) {
  567. const isDisabled = this._isDisabled();
  568. const isFilterable = this._isFilterable();
  569. const { isOpen } = this.getStates();
  570. if (isDisabled) {
  571. return;
  572. } else if (!isOpen) {
  573. this.open();
  574. this._notifyFocus(e);
  575. } else if (isOpen && !isFilterable) {
  576. this.close(e);
  577. }
  578. }
  579. /**
  580. * A11y: simulate selection click
  581. */
  582. /* istanbul ignore next */
  583. handleSelectionEnterPress(keyboardEvent: any) {
  584. if (isEnterPress(keyboardEvent)) {
  585. this.handleClick(keyboardEvent);
  586. }
  587. }
  588. toggleHoverState(bool: boolean) {
  589. this._adapter.toggleHovering(bool);
  590. }
  591. _defaultRenderText(path: any[], displayRender?: BasicCascaderProps['displayRender']) {
  592. const separator = this.getProp('separator');
  593. if (displayRender && typeof displayRender === 'function') {
  594. return displayRender(path);
  595. } else {
  596. return path.join(separator);
  597. }
  598. }
  599. renderDisplayText(targetKey: string, keyEntities?: BasicEntities) {
  600. const renderFunc = this.getProp('displayRender');
  601. const displayProp = this.getProp('displayProp');
  602. const displayPath = this.getItemPropPath(targetKey, displayProp, keyEntities);
  603. return this._defaultRenderText(displayPath, renderFunc);
  604. }
  605. handleNodeLoad(item: BasicEntity | BasicData) {
  606. const { data, key } = item;
  607. const {
  608. loadedKeys: prevLoadedKeys,
  609. loadingKeys: prevLoadingKeys
  610. } = this.getCopyFromState(['loadedKeys', 'loadingKeys']);
  611. const newLoadedKeys = prevLoadedKeys.add(key);
  612. const newLoadingKeys = new Set([...prevLoadingKeys]);
  613. newLoadingKeys.delete(key);
  614. // onLoad should trigger before internal setState to avoid `loadData` trigger twice.
  615. this._adapter.notifyOnLoad(newLoadedKeys, data);
  616. this._adapter.updateStates({
  617. loadingKeys: newLoadingKeys,
  618. });
  619. }
  620. notifyIfLoadData(item: BasicEntity | BasicData) {
  621. const { data, key } = item;
  622. this._adapter.updateStates({ loading: false });
  623. if (!data.isLeaf && !data.children && this.getProp('loadData')) {
  624. const { loadedKeys, loadingKeys } = this.getCopyFromState(['loadedKeys', 'loadingKeys']);
  625. if (loadedKeys.has(key) || loadingKeys.has(key)) {
  626. return;
  627. }
  628. this._adapter.updateStates({ loading: true });
  629. const { keyEntities } = this.getStates();
  630. const optionPath = this.getItemPropPath(key, [], keyEntities);
  631. this._adapter.updateStates({ loadingKeys: loadingKeys.add(key) });
  632. this._adapter.notifyLoadData(optionPath, this.handleNodeLoad.bind(this, item));
  633. }
  634. }
  635. handleSingleSelect(e: any, item: BasicEntity | BasicData) {
  636. const { changeOnSelect: allowChange, filterLeafOnly, multiple, enableLeafClick } = this.getProps();
  637. const { keyEntities, selectedKeys, isSearching } = this.getStates();
  638. const filterable = this._isFilterable();
  639. const { data, key } = item;
  640. const isLeaf = this._isLeaf(data);
  641. const activeKeys = keyEntities[key].path;
  642. const selectedKey = [key];
  643. const hasChanged = key !== [...selectedKeys][0];
  644. if (!isLeaf && !allowChange && !isSearching) {
  645. this._adapter.updateStates({ activeKeys: new Set(activeKeys) });
  646. this.notifyIfLoadData(item);
  647. return;
  648. }
  649. if (multiple) {
  650. this._adapter.updateStates({ activeKeys: new Set(activeKeys) });
  651. if (isLeaf && enableLeafClick) {
  652. this.onItemCheckboxClick(item);
  653. }
  654. } else {
  655. this._adapter.notifySelect(data.value);
  656. if (hasChanged) {
  657. this._notifyChange(item);
  658. this.notifyIfLoadData(item);
  659. if (this._isControlledComponent()) {
  660. this._adapter.updateStates({ activeKeys: new Set(activeKeys) });
  661. if (isLeaf) {
  662. this.close(e);
  663. }
  664. return;
  665. }
  666. this._adapter.updateStates({
  667. activeKeys: new Set(activeKeys),
  668. selectedKeys: new Set(selectedKey),
  669. });
  670. const displayText = this.renderDisplayText(key);
  671. if (filterable) {
  672. this._adapter.updateInputPlaceHolder(displayText);
  673. }
  674. if (isLeaf) {
  675. this.close(e, key);
  676. } else if (!filterLeafOnly && isSearching) {
  677. this.close(e, key);
  678. }
  679. } else {
  680. this.close(e);
  681. }
  682. }
  683. }
  684. _handleMultipleSelect(item: BasicEntity | BasicData) {
  685. const { key } = item;
  686. const { checkedKeys, keyEntities, resolvedCheckedKeys } = this.getStates();
  687. const { autoMergeValue, max, disableStrictly, leafOnly } = this.getProps();
  688. // prev checked status
  689. const prevCheckedStatus = checkedKeys.has(key);
  690. // next checked status
  691. const curCheckedStatus = disableStrictly ?
  692. this.calcCheckedStatus(!prevCheckedStatus, key) :
  693. !prevCheckedStatus;
  694. // calculate all key of nodes that are checked or half checked
  695. const {
  696. checkedKeys: curCheckedKeys,
  697. halfCheckedKeys: curHalfCheckedKeys
  698. } = disableStrictly ?
  699. this.calcNonDisabledCheckedKeys(key, curCheckedStatus) :
  700. this.calcCheckedKeys(key, curCheckedStatus);
  701. const mergeType = calcMergeType(autoMergeValue, leafOnly);
  702. const isLeafOnlyMerge = mergeType === strings.LEAF_ONLY_MERGE_TYPE;
  703. const isNoneMerge = mergeType === strings.NONE_MERGE_TYPE;
  704. const curResolvedCheckedKeys = new Set(normalizeKeyList(curCheckedKeys, keyEntities, isLeafOnlyMerge));
  705. const curRealCheckedKeys = isNoneMerge
  706. ? curCheckedKeys
  707. : curResolvedCheckedKeys;
  708. if (isNumber(max)) {
  709. if (!isNoneMerge) {
  710. // When it exceeds max, the quantity is allowed to be reduced, and no further increase is allowed
  711. if (resolvedCheckedKeys.size < curResolvedCheckedKeys.size && curResolvedCheckedKeys.size > max) {
  712. const checkedEntities: BasicEntity[] = [];
  713. curResolvedCheckedKeys.forEach(itemKey => {
  714. checkedEntities.push(keyEntities[itemKey]);
  715. });
  716. this._adapter.notifyOnExceed(checkedEntities);
  717. return;
  718. }
  719. } else {
  720. // When it exceeds max, the quantity is allowed to be reduced, and no further increase is allowed
  721. if (checkedKeys.size < curCheckedKeys.size && curCheckedKeys.size > max) {
  722. const checkedEntities: BasicEntity[] = [];
  723. curCheckedKeys.forEach((itemKey: string) => {
  724. checkedEntities.push(keyEntities[itemKey]);
  725. });
  726. this._adapter.notifyOnExceed(checkedEntities);
  727. return;
  728. }
  729. }
  730. }
  731. if (!this._isControlledComponent()) {
  732. this._adapter.updateStates({
  733. checkedKeys: curCheckedKeys,
  734. halfCheckedKeys: curHalfCheckedKeys,
  735. resolvedCheckedKeys: curResolvedCheckedKeys
  736. });
  737. }
  738. // The click event during multiple selection will definitely cause the checked state of node to change,
  739. // so there is no need to judge the value to change.
  740. this._notifyChange(curRealCheckedKeys);
  741. if (curCheckedStatus) {
  742. this._notifySelect(curRealCheckedKeys);
  743. }
  744. this._adapter.updateStates({ inputValue: '' });
  745. }
  746. calcNonDisabledCheckedKeys(eventKey: string, targetStatus: boolean) {
  747. const { keyEntities, disabledKeys } = this.getStates();
  748. const { checkedKeys } = this.getCopyFromState(['checkedKeys']);
  749. const descendantKeys = normalizeKeyList(findDescendantKeys([eventKey], keyEntities, false), keyEntities, true);
  750. const hasDisabled = descendantKeys.some(key => disabledKeys.has(key));
  751. if (!hasDisabled) {
  752. return this.calcCheckedKeys(eventKey, targetStatus);
  753. }
  754. const nonDisabled = descendantKeys.filter(key => !disabledKeys.has(key));
  755. const newCheckedKeys = targetStatus
  756. ? [...nonDisabled, ...checkedKeys]
  757. : difference(normalizeKeyList([...checkedKeys], keyEntities, true), nonDisabled);
  758. return calcCheckedKeys(newCheckedKeys, keyEntities);
  759. }
  760. calcCheckedStatus(targetStatus: boolean, eventKey: string) {
  761. if (!targetStatus) {
  762. return targetStatus;
  763. }
  764. const { checkedKeys, keyEntities, disabledKeys } = this.getStates();
  765. const descendantKeys = normalizeKeyList(findDescendantKeys([eventKey], keyEntities, false), keyEntities, true);
  766. const hasDisabled = descendantKeys.some(key => disabledKeys.has(key));
  767. if (!hasDisabled) {
  768. return targetStatus;
  769. }
  770. const nonDisabledKeys = descendantKeys.filter(key => !disabledKeys.has(key));
  771. const allChecked = nonDisabledKeys.every(key => checkedKeys.has(key));
  772. return !allChecked;
  773. }
  774. _notifySelect(keys: Set<string>) {
  775. const { keyEntities } = this.getStates();
  776. const values: (string | number)[] = [];
  777. keys.forEach(key => {
  778. if (!isEmpty(keyEntities) && !isEmpty(keyEntities[key])) {
  779. const valueItem = keyEntities[key].data.value;
  780. values.push(valueItem);
  781. }
  782. });
  783. const formatValue: number | string | Array<string | number> = values.length === 1 ?
  784. values[0] :
  785. values;
  786. this._adapter.notifySelect(formatValue);
  787. }
  788. /**
  789. * calculate all key of nodes that are checked or half checked
  790. * @param {string} key key of node
  791. * @param {boolean} curCheckedStatus checked status of node
  792. */
  793. calcCheckedKeys(key: string, curCheckedStatus: boolean) {
  794. const { keyEntities } = this.getStates();
  795. const { checkedKeys, halfCheckedKeys } = this.getCopyFromState(['checkedKeys', 'halfCheckedKeys']);
  796. return curCheckedStatus ?
  797. calcCheckedKeysForChecked(key, keyEntities, checkedKeys, halfCheckedKeys) :
  798. calcCheckedKeysForUnchecked(key, keyEntities, checkedKeys, halfCheckedKeys);
  799. }
  800. handleInputChange(sugInput: string) {
  801. this._adapter.updateInputValue(sugInput);
  802. const { keyEntities } = this.getStates();
  803. const { treeNodeFilterProp, filterTreeNode, filterLeafOnly } = this.getProps();
  804. let filteredKeys: string[] = [];
  805. if (sugInput) {
  806. filteredKeys = (Object.values(keyEntities) as BasicEntity[])
  807. .filter(item => {
  808. const { key, _notExist } = item;
  809. if (_notExist) {
  810. return false;
  811. }
  812. const filteredPath = this.getItemPropPath(key, treeNodeFilterProp).join();
  813. return filter(sugInput, filteredPath, filterTreeNode, false);
  814. })
  815. .filter(
  816. item => (filterTreeNode && !filterLeafOnly) ||
  817. this._isLeaf(item as unknown as BasicCascaderData)
  818. )
  819. .map(item => item.key);
  820. }
  821. this._adapter.updateStates({
  822. isSearching: Boolean(sugInput),
  823. filteredKeys: new Set(filteredKeys),
  824. });
  825. this._adapter.notifyOnSearch(sugInput);
  826. }
  827. handleClear() {
  828. const { isSearching } = this.getStates();
  829. const { searchPlaceholder, placeholder, multiple } = this.getProps();
  830. const isFilterable = this._isFilterable();
  831. const isControlled = this._isControlledComponent();
  832. const newState: Partial<BasicCascaderInnerData> = {};
  833. if (multiple) {
  834. this._adapter.updateInputValue('');
  835. this._adapter.notifyOnSearch('');
  836. newState.checkedKeys = new Set([]);
  837. newState.halfCheckedKeys = new Set([]);
  838. newState.selectedKeys = new Set([]);
  839. newState.activeKeys = new Set([]);
  840. newState.resolvedCheckedKeys = new Set([]);
  841. this._adapter.notifyChange([]);
  842. } else {
  843. // if click clearBtn when not searching, clear selected and active values as well
  844. if (isFilterable && isSearching) {
  845. newState.isSearching = false;
  846. this._adapter.updateInputValue('');
  847. this._adapter.notifyOnSearch('');
  848. } else {
  849. if (isFilterable) {
  850. newState.inputValue = '';
  851. newState.inputPlaceHolder = searchPlaceholder || placeholder || '';
  852. this._adapter.updateInputValue('');
  853. this._adapter.notifyOnSearch('');
  854. }
  855. if (!isControlled) {
  856. newState.selectedKeys = new Set([]);
  857. }
  858. newState.activeKeys = new Set([]);
  859. newState.filteredKeys = new Set([]);
  860. this._adapter.notifyChange([]);
  861. }
  862. }
  863. this._adapter.updateStates(newState);
  864. this._adapter.notifyClear();
  865. this._adapter.rePositionDropdown();
  866. }
  867. /**
  868. * A11y: simulate clear button click
  869. */
  870. /* istanbul ignore next */
  871. handleClearEnterPress(keyboardEvent: any) {
  872. if (isEnterPress(keyboardEvent)) {
  873. this.handleClear();
  874. }
  875. }
  876. getRenderData() {
  877. const { keyEntities, isSearching } = this.getStates();
  878. const isFilterable = this._isFilterable();
  879. if (isSearching && isFilterable) {
  880. return this.getFilteredData();
  881. }
  882. return (Object.values(keyEntities) as BasicEntity[])
  883. .filter(item => item.parentKey === null && !item._notExist)
  884. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  885. // @ts-ignore
  886. .sort((a, b) => parseInt(a.ind, 10) - parseInt(b.ind, 10));
  887. }
  888. getFilteredData() {
  889. const { treeNodeFilterProp } = this.getProps();
  890. const { filteredKeys, keyEntities } = this.getStates();
  891. const filteredList: BasicData[] = [];
  892. const filteredKeyArr = [...filteredKeys];
  893. filteredKeyArr.forEach(key => {
  894. const item = keyEntities[key];
  895. if (!item) {
  896. return;
  897. }
  898. const itemSearchPath = this.getItemPropPath(key, treeNodeFilterProp);
  899. const isDisabled = this._isOptionDisabled(key, keyEntities);
  900. filteredList.push({
  901. data: item.data,
  902. key,
  903. disabled: isDisabled,
  904. searchText: itemSearchPath
  905. });
  906. });
  907. return filteredList;
  908. }
  909. handleListScroll(e: any, ind: number) {
  910. const { activeKeys, keyEntities } = this.getStates();
  911. const lastActiveKey = [...activeKeys][activeKeys.size - 1];
  912. const data = lastActiveKey ? get(keyEntities, [lastActiveKey, 'data'], null) : null;
  913. this._adapter.notifyListScroll(e, { panelIndex: ind, activeNode: data });
  914. }
  915. handleTagRemove(e: any, tagValuePath: string[]) {
  916. const { keyEntities } = this.getStates();
  917. const { disabled } = this.getProps();
  918. if (disabled) {
  919. /* istanbul ignore next */
  920. return;
  921. }
  922. const removedItem = (Object.values(keyEntities) as BasicEntity[])
  923. .filter(item => isEqual(item.valuePath, tagValuePath))[0];
  924. !isEmpty(removedItem) &&
  925. !removedItem.data.disabled &&
  926. this._handleMultipleSelect(removedItem);
  927. }
  928. }