OptionList.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
  3. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  4. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  5. import { useBaseProps } from 'rc-select';
  6. import Tree from 'rc-tree';
  7. import { UnstableContext } from 'rc-tree';
  8. import KeyCode from "rc-util/es/KeyCode";
  9. import useMemo from "rc-util/es/hooks/useMemo";
  10. import * as React from 'react';
  11. import LegacyContext from "./LegacyContext";
  12. import TreeSelectContext from "./TreeSelectContext";
  13. import { getAllKeys, isCheckDisabled } from "./utils/valueUtil";
  14. import { useEvent } from 'rc-util';
  15. var HIDDEN_STYLE = {
  16. width: 0,
  17. height: 0,
  18. display: 'flex',
  19. overflow: 'hidden',
  20. opacity: 0,
  21. border: 0,
  22. padding: 0,
  23. margin: 0
  24. };
  25. var OptionList = function OptionList(_, ref) {
  26. var _useBaseProps = useBaseProps(),
  27. prefixCls = _useBaseProps.prefixCls,
  28. multiple = _useBaseProps.multiple,
  29. searchValue = _useBaseProps.searchValue,
  30. toggleOpen = _useBaseProps.toggleOpen,
  31. open = _useBaseProps.open,
  32. notFoundContent = _useBaseProps.notFoundContent;
  33. var _React$useContext = React.useContext(TreeSelectContext),
  34. virtual = _React$useContext.virtual,
  35. listHeight = _React$useContext.listHeight,
  36. listItemHeight = _React$useContext.listItemHeight,
  37. listItemScrollOffset = _React$useContext.listItemScrollOffset,
  38. treeData = _React$useContext.treeData,
  39. fieldNames = _React$useContext.fieldNames,
  40. onSelect = _React$useContext.onSelect,
  41. dropdownMatchSelectWidth = _React$useContext.dropdownMatchSelectWidth,
  42. treeExpandAction = _React$useContext.treeExpandAction,
  43. treeTitleRender = _React$useContext.treeTitleRender,
  44. onPopupScroll = _React$useContext.onPopupScroll,
  45. leftMaxCount = _React$useContext.leftMaxCount,
  46. leafCountOnly = _React$useContext.leafCountOnly,
  47. valueEntities = _React$useContext.valueEntities;
  48. var _React$useContext2 = React.useContext(LegacyContext),
  49. checkable = _React$useContext2.checkable,
  50. checkedKeys = _React$useContext2.checkedKeys,
  51. halfCheckedKeys = _React$useContext2.halfCheckedKeys,
  52. treeExpandedKeys = _React$useContext2.treeExpandedKeys,
  53. treeDefaultExpandAll = _React$useContext2.treeDefaultExpandAll,
  54. treeDefaultExpandedKeys = _React$useContext2.treeDefaultExpandedKeys,
  55. onTreeExpand = _React$useContext2.onTreeExpand,
  56. treeIcon = _React$useContext2.treeIcon,
  57. showTreeIcon = _React$useContext2.showTreeIcon,
  58. switcherIcon = _React$useContext2.switcherIcon,
  59. treeLine = _React$useContext2.treeLine,
  60. treeNodeFilterProp = _React$useContext2.treeNodeFilterProp,
  61. loadData = _React$useContext2.loadData,
  62. treeLoadedKeys = _React$useContext2.treeLoadedKeys,
  63. treeMotion = _React$useContext2.treeMotion,
  64. onTreeLoad = _React$useContext2.onTreeLoad,
  65. keyEntities = _React$useContext2.keyEntities;
  66. var treeRef = React.useRef();
  67. var memoTreeData = useMemo(function () {
  68. return treeData;
  69. },
  70. // eslint-disable-next-line react-hooks/exhaustive-deps
  71. [open, treeData], function (prev, next) {
  72. return next[0] && prev[1] !== next[1];
  73. });
  74. // ========================== Values ==========================
  75. var mergedCheckedKeys = React.useMemo(function () {
  76. if (!checkable) {
  77. return null;
  78. }
  79. return {
  80. checked: checkedKeys,
  81. halfChecked: halfCheckedKeys
  82. };
  83. }, [checkable, checkedKeys, halfCheckedKeys]);
  84. // ========================== Scroll ==========================
  85. React.useEffect(function () {
  86. // Single mode should scroll to current key
  87. if (open && !multiple && checkedKeys.length) {
  88. var _treeRef$current;
  89. (_treeRef$current = treeRef.current) === null || _treeRef$current === void 0 || _treeRef$current.scrollTo({
  90. key: checkedKeys[0]
  91. });
  92. }
  93. // eslint-disable-next-line react-hooks/exhaustive-deps
  94. }, [open]);
  95. // ========================== Events ==========================
  96. var onListMouseDown = function onListMouseDown(event) {
  97. event.preventDefault();
  98. };
  99. var onInternalSelect = function onInternalSelect(__, info) {
  100. var node = info.node;
  101. if (checkable && isCheckDisabled(node)) {
  102. return;
  103. }
  104. onSelect(node.key, {
  105. selected: !checkedKeys.includes(node.key)
  106. });
  107. if (!multiple) {
  108. toggleOpen(false);
  109. }
  110. };
  111. // =========================== Keys ===========================
  112. var _React$useState = React.useState(treeDefaultExpandedKeys),
  113. _React$useState2 = _slicedToArray(_React$useState, 2),
  114. expandedKeys = _React$useState2[0],
  115. setExpandedKeys = _React$useState2[1];
  116. var _React$useState3 = React.useState(null),
  117. _React$useState4 = _slicedToArray(_React$useState3, 2),
  118. searchExpandedKeys = _React$useState4[0],
  119. setSearchExpandedKeys = _React$useState4[1];
  120. var mergedExpandedKeys = React.useMemo(function () {
  121. if (treeExpandedKeys) {
  122. return _toConsumableArray(treeExpandedKeys);
  123. }
  124. return searchValue ? searchExpandedKeys : expandedKeys;
  125. }, [expandedKeys, searchExpandedKeys, treeExpandedKeys, searchValue]);
  126. var onInternalExpand = function onInternalExpand(keys) {
  127. setExpandedKeys(keys);
  128. setSearchExpandedKeys(keys);
  129. if (onTreeExpand) {
  130. onTreeExpand(keys);
  131. }
  132. };
  133. // ========================== Search ==========================
  134. var lowerSearchValue = String(searchValue).toLowerCase();
  135. var filterTreeNode = function filterTreeNode(treeNode) {
  136. if (!lowerSearchValue) {
  137. return false;
  138. }
  139. return String(treeNode[treeNodeFilterProp]).toLowerCase().includes(lowerSearchValue);
  140. };
  141. React.useEffect(function () {
  142. if (searchValue) {
  143. setSearchExpandedKeys(getAllKeys(treeData, fieldNames));
  144. }
  145. // eslint-disable-next-line react-hooks/exhaustive-deps
  146. }, [searchValue]);
  147. // ========================= Disabled =========================
  148. // Cache disabled states in React state to ensure re-render when cache updates
  149. var _React$useState5 = React.useState(function () {
  150. return new Map();
  151. }),
  152. _React$useState6 = _slicedToArray(_React$useState5, 2),
  153. disabledCache = _React$useState6[0],
  154. setDisabledCache = _React$useState6[1];
  155. React.useEffect(function () {
  156. if (leftMaxCount) {
  157. setDisabledCache(new Map());
  158. }
  159. }, [leftMaxCount]);
  160. function getDisabledWithCache(node) {
  161. var value = node[fieldNames.value];
  162. if (!disabledCache.has(value)) {
  163. var entity = valueEntities.get(value);
  164. var isLeaf = (entity.children || []).length === 0;
  165. if (!isLeaf) {
  166. var checkableChildren = entity.children.filter(function (childTreeNode) {
  167. return !childTreeNode.node.disabled && !childTreeNode.node.disableCheckbox && !checkedKeys.includes(childTreeNode.node[fieldNames.value]);
  168. });
  169. var checkableChildrenCount = checkableChildren.length;
  170. disabledCache.set(value, checkableChildrenCount > leftMaxCount);
  171. } else {
  172. disabledCache.set(value, false);
  173. }
  174. }
  175. return disabledCache.get(value);
  176. }
  177. var nodeDisabled = useEvent(function (node) {
  178. var nodeValue = node[fieldNames.value];
  179. if (checkedKeys.includes(nodeValue)) {
  180. return false;
  181. }
  182. if (leftMaxCount === null) {
  183. return false;
  184. }
  185. if (leftMaxCount <= 0) {
  186. return true;
  187. }
  188. // This is a low performance calculation
  189. if (leafCountOnly && leftMaxCount) {
  190. return getDisabledWithCache(node);
  191. }
  192. return false;
  193. });
  194. // ========================== Get First Selectable Node ==========================
  195. var getFirstMatchingNode = function getFirstMatchingNode(nodes) {
  196. var _iterator = _createForOfIteratorHelper(nodes),
  197. _step;
  198. try {
  199. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  200. var node = _step.value;
  201. if (node.disabled || node.selectable === false) {
  202. continue;
  203. }
  204. if (searchValue) {
  205. if (filterTreeNode(node)) {
  206. return node;
  207. }
  208. } else {
  209. return node;
  210. }
  211. if (node[fieldNames.children]) {
  212. var matchInChildren = getFirstMatchingNode(node[fieldNames.children]);
  213. if (matchInChildren) {
  214. return matchInChildren;
  215. }
  216. }
  217. }
  218. } catch (err) {
  219. _iterator.e(err);
  220. } finally {
  221. _iterator.f();
  222. }
  223. return null;
  224. };
  225. // ========================== Active ==========================
  226. var _React$useState7 = React.useState(null),
  227. _React$useState8 = _slicedToArray(_React$useState7, 2),
  228. activeKey = _React$useState8[0],
  229. setActiveKey = _React$useState8[1];
  230. var activeEntity = keyEntities[activeKey];
  231. React.useEffect(function () {
  232. if (!open) {
  233. return;
  234. }
  235. var nextActiveKey = null;
  236. var getFirstNode = function getFirstNode() {
  237. var firstNode = getFirstMatchingNode(memoTreeData);
  238. return firstNode ? firstNode[fieldNames.value] : null;
  239. };
  240. // single mode active first checked node
  241. if (!multiple && checkedKeys.length && !searchValue) {
  242. nextActiveKey = checkedKeys[0];
  243. } else {
  244. nextActiveKey = getFirstNode();
  245. }
  246. setActiveKey(nextActiveKey);
  247. }, [open, searchValue]);
  248. // ========================= Keyboard =========================
  249. React.useImperativeHandle(ref, function () {
  250. var _treeRef$current2;
  251. return {
  252. scrollTo: (_treeRef$current2 = treeRef.current) === null || _treeRef$current2 === void 0 ? void 0 : _treeRef$current2.scrollTo,
  253. onKeyDown: function onKeyDown(event) {
  254. var _treeRef$current3;
  255. var which = event.which;
  256. switch (which) {
  257. // >>> Arrow keys
  258. case KeyCode.UP:
  259. case KeyCode.DOWN:
  260. case KeyCode.LEFT:
  261. case KeyCode.RIGHT:
  262. (_treeRef$current3 = treeRef.current) === null || _treeRef$current3 === void 0 || _treeRef$current3.onKeyDown(event);
  263. break;
  264. // >>> Select item
  265. case KeyCode.ENTER:
  266. {
  267. if (activeEntity) {
  268. var isNodeDisabled = nodeDisabled(activeEntity.node);
  269. var _ref = (activeEntity === null || activeEntity === void 0 ? void 0 : activeEntity.node) || {},
  270. selectable = _ref.selectable,
  271. value = _ref.value,
  272. disabled = _ref.disabled;
  273. if (selectable !== false && !disabled && !isNodeDisabled) {
  274. onInternalSelect(null, {
  275. node: {
  276. key: activeKey
  277. },
  278. selected: !checkedKeys.includes(value)
  279. });
  280. }
  281. }
  282. break;
  283. }
  284. // >>> Close
  285. case KeyCode.ESC:
  286. {
  287. toggleOpen(false);
  288. }
  289. }
  290. },
  291. onKeyUp: function onKeyUp() {}
  292. };
  293. });
  294. var hasLoadDataFn = useMemo(function () {
  295. return searchValue ? false : true;
  296. }, [searchValue, treeExpandedKeys || expandedKeys], function (_ref2, _ref3) {
  297. var _ref4 = _slicedToArray(_ref2, 1),
  298. preSearchValue = _ref4[0];
  299. var _ref5 = _slicedToArray(_ref3, 2),
  300. nextSearchValue = _ref5[0],
  301. nextExcludeSearchExpandedKeys = _ref5[1];
  302. return preSearchValue !== nextSearchValue && !!(nextSearchValue || nextExcludeSearchExpandedKeys);
  303. });
  304. var syncLoadData = hasLoadDataFn ? loadData : null;
  305. // ========================== Render ==========================
  306. if (memoTreeData.length === 0) {
  307. return /*#__PURE__*/React.createElement("div", {
  308. role: "listbox",
  309. className: "".concat(prefixCls, "-empty"),
  310. onMouseDown: onListMouseDown
  311. }, notFoundContent);
  312. }
  313. var treeProps = {
  314. fieldNames: fieldNames
  315. };
  316. if (treeLoadedKeys) {
  317. treeProps.loadedKeys = treeLoadedKeys;
  318. }
  319. if (mergedExpandedKeys) {
  320. treeProps.expandedKeys = mergedExpandedKeys;
  321. }
  322. return /*#__PURE__*/React.createElement("div", {
  323. onMouseDown: onListMouseDown
  324. }, activeEntity && open && /*#__PURE__*/React.createElement("span", {
  325. style: HIDDEN_STYLE,
  326. "aria-live": "assertive"
  327. }, activeEntity.node.value), /*#__PURE__*/React.createElement(UnstableContext.Provider, {
  328. value: {
  329. nodeDisabled: nodeDisabled
  330. }
  331. }, /*#__PURE__*/React.createElement(Tree, _extends({
  332. ref: treeRef,
  333. focusable: false,
  334. prefixCls: "".concat(prefixCls, "-tree"),
  335. treeData: memoTreeData,
  336. height: listHeight,
  337. itemHeight: listItemHeight,
  338. itemScrollOffset: listItemScrollOffset,
  339. virtual: virtual !== false && dropdownMatchSelectWidth !== false,
  340. multiple: multiple,
  341. icon: treeIcon,
  342. showIcon: showTreeIcon,
  343. switcherIcon: switcherIcon,
  344. showLine: treeLine,
  345. loadData: syncLoadData,
  346. motion: treeMotion,
  347. activeKey: activeKey
  348. // We handle keys by out instead tree self
  349. ,
  350. checkable: checkable,
  351. checkStrictly: true,
  352. checkedKeys: mergedCheckedKeys,
  353. selectedKeys: !checkable ? checkedKeys : [],
  354. defaultExpandAll: treeDefaultExpandAll,
  355. titleRender: treeTitleRender
  356. }, treeProps, {
  357. // Proxy event out
  358. onActiveChange: setActiveKey,
  359. onSelect: onInternalSelect,
  360. onCheck: onInternalSelect,
  361. onExpand: onInternalExpand,
  362. onLoad: onTreeLoad,
  363. filterTreeNode: filterTreeNode,
  364. expandAction: treeExpandAction,
  365. onScroll: onPopupScroll
  366. }))));
  367. };
  368. var RefOptionList = /*#__PURE__*/React.forwardRef(OptionList);
  369. if (process.env.NODE_ENV !== 'production') {
  370. RefOptionList.displayName = 'OptionList';
  371. }
  372. export default RefOptionList;