foundation.ts 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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, isSearching } = 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. isSearching && this._adapter.updateStates({ isSearching: false });
  497. }
  498. this._notifyBlur(e);
  499. }
  500. toggle2SearchInput(isShow: boolean) {
  501. if (isShow) {
  502. this._adapter.toggleInputShow(isShow, () => this.focusInput());
  503. } else {
  504. this._adapter.toggleInputShow(isShow, () => undefined);
  505. }
  506. }
  507. focusInput() {
  508. this._adapter.focusInput();
  509. this._adapter.updateFocusState(true);
  510. }
  511. getMergedMotion = () => {
  512. const { motion } = this.getProps();
  513. const { isSearching } = this.getStates();
  514. if (isSearching) {
  515. const mergedMotion =
  516. typeof motion === 'undefined' || motion ?
  517. {
  518. ...motion,
  519. didLeave: (...args: any) => {
  520. const didLeave = get(motion, 'didLeave');
  521. if (typeof didLeave === 'function') {
  522. didLeave(...args);
  523. }
  524. this._adapter.updateStates({ isSearching: false });
  525. },
  526. } :
  527. false;
  528. return mergedMotion;
  529. }
  530. return motion;
  531. };
  532. handleItemClick(e: any, item: BasicEntity | BasicData) {
  533. const isDisabled = this._isDisabled();
  534. if (isDisabled) {
  535. return;
  536. }
  537. this.handleSingleSelect(e, item);
  538. this._adapter.rePositionDropdown();
  539. }
  540. handleItemHover(e: any, item: BasicEntity) {
  541. const isDisabled = this._isDisabled();
  542. if (isDisabled) {
  543. return;
  544. }
  545. this.handleShowNextByHover(item);
  546. }
  547. handleShowNextByHover(item: BasicEntity) {
  548. const { keyEntities } = this.getStates();
  549. const { data, key } = item;
  550. const isLeaf = this._isLeaf(data);
  551. const activeKeys = keyEntities[key].path;
  552. this._adapter.updateStates({
  553. activeKeys: new Set(activeKeys)
  554. });
  555. if (!isLeaf) {
  556. this.notifyIfLoadData(item);
  557. }
  558. }
  559. onItemCheckboxClick(item: BasicEntity | BasicData) {
  560. const isDisabled = this._isDisabled();
  561. if (isDisabled) {
  562. return;
  563. }
  564. this._handleMultipleSelect(item);
  565. this._adapter.rePositionDropdown();
  566. }
  567. handleClick(e: any) {
  568. const isDisabled = this._isDisabled();
  569. const isFilterable = this._isFilterable();
  570. const { isOpen } = this.getStates();
  571. if (isDisabled) {
  572. return;
  573. } else if (!isOpen) {
  574. this.open();
  575. this._notifyFocus(e);
  576. } else if (isOpen && !isFilterable) {
  577. this.close(e);
  578. }
  579. }
  580. /**
  581. * A11y: simulate selection click
  582. */
  583. /* istanbul ignore next */
  584. handleSelectionEnterPress(keyboardEvent: any) {
  585. if (isEnterPress(keyboardEvent)) {
  586. this.handleClick(keyboardEvent);
  587. }
  588. }
  589. toggleHoverState(bool: boolean) {
  590. this._adapter.toggleHovering(bool);
  591. }
  592. _defaultRenderText(path: any[], displayRender?: BasicCascaderProps['displayRender']) {
  593. const separator = this.getProp('separator');
  594. if (displayRender && typeof displayRender === 'function') {
  595. return displayRender(path);
  596. } else {
  597. return path.join(separator);
  598. }
  599. }
  600. renderDisplayText(targetKey: string, keyEntities?: BasicEntities) {
  601. const renderFunc = this.getProp('displayRender');
  602. const displayProp = this.getProp('displayProp');
  603. const displayPath = this.getItemPropPath(targetKey, displayProp, keyEntities);
  604. return this._defaultRenderText(displayPath, renderFunc);
  605. }
  606. handleNodeLoad(item: BasicEntity | BasicData) {
  607. const { data, key } = item;
  608. const {
  609. loadedKeys: prevLoadedKeys,
  610. loadingKeys: prevLoadingKeys
  611. } = this.getCopyFromState(['loadedKeys', 'loadingKeys']);
  612. const newLoadedKeys = prevLoadedKeys.add(key);
  613. const newLoadingKeys = new Set([...prevLoadingKeys]);
  614. newLoadingKeys.delete(key);
  615. // onLoad should trigger before internal setState to avoid `loadData` trigger twice.
  616. this._adapter.notifyOnLoad(newLoadedKeys, data);
  617. this._adapter.updateStates({
  618. loadingKeys: newLoadingKeys,
  619. });
  620. }
  621. notifyIfLoadData(item: BasicEntity | BasicData) {
  622. const { data, key } = item;
  623. this._adapter.updateStates({ loading: false });
  624. if (!data.isLeaf && !data.children && this.getProp('loadData')) {
  625. const { loadedKeys, loadingKeys } = this.getCopyFromState(['loadedKeys', 'loadingKeys']);
  626. if (loadedKeys.has(key) || loadingKeys.has(key)) {
  627. return;
  628. }
  629. this._adapter.updateStates({ loading: true });
  630. const { keyEntities } = this.getStates();
  631. const optionPath = this.getItemPropPath(key, [], keyEntities);
  632. this._adapter.updateStates({ loadingKeys: loadingKeys.add(key) });
  633. this._adapter.notifyLoadData(optionPath, this.handleNodeLoad.bind(this, item));
  634. }
  635. }
  636. handleSingleSelect(e: any, item: BasicEntity | BasicData) {
  637. const { changeOnSelect: allowChange, filterLeafOnly, multiple, enableLeafClick } = this.getProps();
  638. const { keyEntities, selectedKeys, isSearching } = this.getStates();
  639. const filterable = this._isFilterable();
  640. const { data, key } = item;
  641. const isLeaf = this._isLeaf(data);
  642. const activeKeys = keyEntities[key].path;
  643. const selectedKey = [key];
  644. const hasChanged = key !== [...selectedKeys][0];
  645. if (!isLeaf && !allowChange && !isSearching) {
  646. this._adapter.updateStates({ activeKeys: new Set(activeKeys) });
  647. this.notifyIfLoadData(item);
  648. return;
  649. }
  650. if (multiple) {
  651. this._adapter.updateStates({ activeKeys: new Set(activeKeys) });
  652. if (isLeaf && enableLeafClick) {
  653. this.onItemCheckboxClick(item);
  654. }
  655. } else {
  656. this._adapter.notifySelect(data.value);
  657. if (hasChanged) {
  658. this._notifyChange(item);
  659. this.notifyIfLoadData(item);
  660. if (this._isControlledComponent()) {
  661. this._adapter.updateStates({ activeKeys: new Set(activeKeys) });
  662. if (isLeaf) {
  663. this.close(e);
  664. }
  665. return;
  666. }
  667. this._adapter.updateStates({
  668. activeKeys: new Set(activeKeys),
  669. selectedKeys: new Set(selectedKey),
  670. });
  671. const displayText = this.renderDisplayText(key);
  672. if (filterable) {
  673. this._adapter.updateInputPlaceHolder(displayText);
  674. }
  675. if (isLeaf) {
  676. this.close(e, key);
  677. } else if (!filterLeafOnly && isSearching) {
  678. this.close(e, key);
  679. }
  680. } else {
  681. this.close(e);
  682. }
  683. }
  684. }
  685. _handleMultipleSelect(item: BasicEntity | BasicData) {
  686. const { key } = item;
  687. const { checkedKeys, keyEntities, resolvedCheckedKeys } = this.getStates();
  688. const { autoMergeValue, max, disableStrictly, leafOnly } = this.getProps();
  689. // prev checked status
  690. const prevCheckedStatus = checkedKeys.has(key);
  691. // next checked status
  692. const curCheckedStatus = disableStrictly ?
  693. this.calcCheckedStatus(!prevCheckedStatus, key) :
  694. !prevCheckedStatus;
  695. // calculate all key of nodes that are checked or half checked
  696. const {
  697. checkedKeys: curCheckedKeys,
  698. halfCheckedKeys: curHalfCheckedKeys
  699. } = disableStrictly ?
  700. this.calcNonDisabledCheckedKeys(key, curCheckedStatus) :
  701. this.calcCheckedKeys(key, curCheckedStatus);
  702. const mergeType = calcMergeType(autoMergeValue, leafOnly);
  703. const isLeafOnlyMerge = mergeType === strings.LEAF_ONLY_MERGE_TYPE;
  704. const isNoneMerge = mergeType === strings.NONE_MERGE_TYPE;
  705. const curResolvedCheckedKeys = new Set(normalizeKeyList(curCheckedKeys, keyEntities, isLeafOnlyMerge));
  706. const curRealCheckedKeys = isNoneMerge
  707. ? curCheckedKeys
  708. : curResolvedCheckedKeys;
  709. if (isNumber(max)) {
  710. if (!isNoneMerge) {
  711. // When it exceeds max, the quantity is allowed to be reduced, and no further increase is allowed
  712. if (resolvedCheckedKeys.size < curResolvedCheckedKeys.size && curResolvedCheckedKeys.size > max) {
  713. const checkedEntities: BasicEntity[] = [];
  714. curResolvedCheckedKeys.forEach(itemKey => {
  715. checkedEntities.push(keyEntities[itemKey]);
  716. });
  717. this._adapter.notifyOnExceed(checkedEntities);
  718. return;
  719. }
  720. } else {
  721. // When it exceeds max, the quantity is allowed to be reduced, and no further increase is allowed
  722. if (checkedKeys.size < curCheckedKeys.size && curCheckedKeys.size > max) {
  723. const checkedEntities: BasicEntity[] = [];
  724. curCheckedKeys.forEach((itemKey: string) => {
  725. checkedEntities.push(keyEntities[itemKey]);
  726. });
  727. this._adapter.notifyOnExceed(checkedEntities);
  728. return;
  729. }
  730. }
  731. }
  732. if (!this._isControlledComponent()) {
  733. this._adapter.updateStates({
  734. checkedKeys: curCheckedKeys,
  735. halfCheckedKeys: curHalfCheckedKeys,
  736. resolvedCheckedKeys: curResolvedCheckedKeys
  737. });
  738. }
  739. // The click event during multiple selection will definitely cause the checked state of node to change,
  740. // so there is no need to judge the value to change.
  741. this._notifyChange(curRealCheckedKeys);
  742. if (curCheckedStatus) {
  743. this._notifySelect(curRealCheckedKeys);
  744. }
  745. this._adapter.updateStates({ inputValue: '' });
  746. }
  747. calcNonDisabledCheckedKeys(eventKey: string, targetStatus: boolean) {
  748. const { keyEntities, disabledKeys } = this.getStates();
  749. const { checkedKeys } = this.getCopyFromState(['checkedKeys']);
  750. const descendantKeys = normalizeKeyList(findDescendantKeys([eventKey], keyEntities, false), keyEntities, true);
  751. const hasDisabled = descendantKeys.some(key => disabledKeys.has(key));
  752. if (!hasDisabled) {
  753. return this.calcCheckedKeys(eventKey, targetStatus);
  754. }
  755. const nonDisabled = descendantKeys.filter(key => !disabledKeys.has(key));
  756. const newCheckedKeys = targetStatus
  757. ? [...nonDisabled, ...checkedKeys]
  758. : difference(normalizeKeyList([...checkedKeys], keyEntities, true), nonDisabled);
  759. return calcCheckedKeys(newCheckedKeys, keyEntities);
  760. }
  761. calcCheckedStatus(targetStatus: boolean, eventKey: string) {
  762. if (!targetStatus) {
  763. return targetStatus;
  764. }
  765. const { checkedKeys, keyEntities, disabledKeys } = this.getStates();
  766. const descendantKeys = normalizeKeyList(findDescendantKeys([eventKey], keyEntities, false), keyEntities, true);
  767. const hasDisabled = descendantKeys.some(key => disabledKeys.has(key));
  768. if (!hasDisabled) {
  769. return targetStatus;
  770. }
  771. const nonDisabledKeys = descendantKeys.filter(key => !disabledKeys.has(key));
  772. const allChecked = nonDisabledKeys.every(key => checkedKeys.has(key));
  773. return !allChecked;
  774. }
  775. _notifySelect(keys: Set<string>) {
  776. const { keyEntities } = this.getStates();
  777. const values: (string | number)[] = [];
  778. keys.forEach(key => {
  779. if (!isEmpty(keyEntities) && !isEmpty(keyEntities[key])) {
  780. const valueItem = keyEntities[key].data.value;
  781. values.push(valueItem);
  782. }
  783. });
  784. const formatValue: number | string | Array<string | number> = values.length === 1 ?
  785. values[0] :
  786. values;
  787. this._adapter.notifySelect(formatValue);
  788. }
  789. /**
  790. * calculate all key of nodes that are checked or half checked
  791. * @param {string} key key of node
  792. * @param {boolean} curCheckedStatus checked status of node
  793. */
  794. calcCheckedKeys(key: string, curCheckedStatus: boolean) {
  795. const { keyEntities } = this.getStates();
  796. const { checkedKeys, halfCheckedKeys } = this.getCopyFromState(['checkedKeys', 'halfCheckedKeys']);
  797. return curCheckedStatus ?
  798. calcCheckedKeysForChecked(key, keyEntities, checkedKeys, halfCheckedKeys) :
  799. calcCheckedKeysForUnchecked(key, keyEntities, checkedKeys, halfCheckedKeys);
  800. }
  801. handleInputChange(sugInput: string) {
  802. this._adapter.updateInputValue(sugInput);
  803. const { keyEntities } = this.getStates();
  804. const { treeNodeFilterProp, filterTreeNode, filterLeafOnly } = this.getProps();
  805. let filteredKeys: string[] = [];
  806. if (sugInput) {
  807. filteredKeys = (Object.values(keyEntities) as BasicEntity[])
  808. .filter(item => {
  809. const { key, _notExist } = item;
  810. if (_notExist) {
  811. return false;
  812. }
  813. const filteredPath = this.getItemPropPath(key, treeNodeFilterProp).join();
  814. return filter(sugInput, filteredPath, filterTreeNode, false);
  815. })
  816. .filter(
  817. item => (filterTreeNode && !filterLeafOnly) ||
  818. this._isLeaf(item as unknown as BasicCascaderData)
  819. )
  820. .map(item => item.key);
  821. }
  822. this._adapter.updateStates({
  823. isSearching: Boolean(sugInput),
  824. filteredKeys: new Set(filteredKeys),
  825. });
  826. this._adapter.notifyOnSearch(sugInput);
  827. }
  828. handleClear() {
  829. const { isSearching } = this.getStates();
  830. const { searchPlaceholder, placeholder, multiple } = this.getProps();
  831. const isFilterable = this._isFilterable();
  832. const isControlled = this._isControlledComponent();
  833. const newState: Partial<BasicCascaderInnerData> = {};
  834. if (multiple) {
  835. this._adapter.updateInputValue('');
  836. this._adapter.notifyOnSearch('');
  837. newState.checkedKeys = new Set([]);
  838. newState.halfCheckedKeys = new Set([]);
  839. newState.selectedKeys = new Set([]);
  840. newState.activeKeys = new Set([]);
  841. newState.resolvedCheckedKeys = new Set([]);
  842. this._adapter.notifyChange([]);
  843. } else {
  844. // if click clearBtn when not searching, clear selected and active values as well
  845. if (isFilterable && isSearching) {
  846. newState.isSearching = false;
  847. this._adapter.updateInputValue('');
  848. this._adapter.notifyOnSearch('');
  849. } else {
  850. if (isFilterable) {
  851. newState.inputValue = '';
  852. newState.inputPlaceHolder = searchPlaceholder || placeholder || '';
  853. this._adapter.updateInputValue('');
  854. this._adapter.notifyOnSearch('');
  855. }
  856. if (!isControlled) {
  857. newState.selectedKeys = new Set([]);
  858. }
  859. newState.activeKeys = new Set([]);
  860. newState.filteredKeys = new Set([]);
  861. this._adapter.notifyChange([]);
  862. }
  863. }
  864. this._adapter.updateStates(newState);
  865. this._adapter.notifyClear();
  866. this._adapter.rePositionDropdown();
  867. }
  868. /**
  869. * A11y: simulate clear button click
  870. */
  871. /* istanbul ignore next */
  872. handleClearEnterPress(keyboardEvent: any) {
  873. if (isEnterPress(keyboardEvent)) {
  874. this.handleClear();
  875. }
  876. }
  877. getRenderData() {
  878. const { keyEntities, isSearching } = this.getStates();
  879. const isFilterable = this._isFilterable();
  880. if (isSearching && isFilterable) {
  881. return this.getFilteredData();
  882. }
  883. return (Object.values(keyEntities) as BasicEntity[])
  884. .filter(item => item.parentKey === null && !item._notExist)
  885. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  886. // @ts-ignore
  887. .sort((a, b) => parseInt(a.ind, 10) - parseInt(b.ind, 10));
  888. }
  889. getFilteredData() {
  890. const { treeNodeFilterProp } = this.getProps();
  891. const { filteredKeys, keyEntities } = this.getStates();
  892. const filteredList: BasicData[] = [];
  893. const filteredKeyArr = [...filteredKeys];
  894. filteredKeyArr.forEach(key => {
  895. const item = keyEntities[key];
  896. if (!item) {
  897. return;
  898. }
  899. const itemSearchPath = this.getItemPropPath(key, treeNodeFilterProp);
  900. const isDisabled = this._isOptionDisabled(key, keyEntities);
  901. filteredList.push({
  902. data: item.data,
  903. key,
  904. disabled: isDisabled,
  905. searchText: itemSearchPath
  906. });
  907. });
  908. return filteredList;
  909. }
  910. handleListScroll(e: any, ind: number) {
  911. const { activeKeys, keyEntities } = this.getStates();
  912. const lastActiveKey = [...activeKeys][activeKeys.size - 1];
  913. const data = lastActiveKey ? get(keyEntities, [lastActiveKey, 'data'], null) : null;
  914. this._adapter.notifyListScroll(e, { panelIndex: ind, activeNode: data });
  915. }
  916. handleTagRemove(e: any, tagValuePath: string[]) {
  917. const { keyEntities } = this.getStates();
  918. const { disabled } = this.getProps();
  919. if (disabled) {
  920. /* istanbul ignore next */
  921. return;
  922. }
  923. const removedItem = (Object.values(keyEntities) as BasicEntity[])
  924. .filter(item => isEqual(item.valuePath, tagValuePath))[0];
  925. !isEmpty(removedItem) &&
  926. !removedItem.data.disabled &&
  927. this._handleMultipleSelect(removedItem);
  928. }
  929. }