foundation.ts 37 KB

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