List.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  3. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  4. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  5. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  6. /* eslint-disable default-case */
  7. import classNames from 'classnames';
  8. import * as React from 'react';
  9. import CascaderContext from "../context";
  10. import { getFullPathKeys, isLeaf, scrollIntoParentView, toPathKey, toPathKeys, toPathValueStr } from "../utils/commonUtil";
  11. import { toPathOptions } from "../utils/treeUtil";
  12. import CacheContent from "./CacheContent";
  13. import Column, { FIX_LABEL } from "./Column";
  14. import useActive from "./useActive";
  15. import useKeyboard from "./useKeyboard";
  16. var RawOptionList = /*#__PURE__*/React.forwardRef(function (props, ref) {
  17. var _optionColumns$, _ref3, _classNames;
  18. var prefixCls = props.prefixCls,
  19. multiple = props.multiple,
  20. searchValue = props.searchValue,
  21. toggleOpen = props.toggleOpen,
  22. notFoundContent = props.notFoundContent,
  23. direction = props.direction,
  24. open = props.open,
  25. disabled = props.disabled;
  26. var containerRef = React.useRef(null);
  27. var rtl = direction === 'rtl';
  28. var _React$useContext = React.useContext(CascaderContext),
  29. options = _React$useContext.options,
  30. values = _React$useContext.values,
  31. halfValues = _React$useContext.halfValues,
  32. fieldNames = _React$useContext.fieldNames,
  33. changeOnSelect = _React$useContext.changeOnSelect,
  34. onSelect = _React$useContext.onSelect,
  35. searchOptions = _React$useContext.searchOptions,
  36. dropdownPrefixCls = _React$useContext.dropdownPrefixCls,
  37. loadData = _React$useContext.loadData,
  38. expandTrigger = _React$useContext.expandTrigger;
  39. var mergedPrefixCls = dropdownPrefixCls || prefixCls;
  40. // ========================= loadData =========================
  41. var _React$useState = React.useState([]),
  42. _React$useState2 = _slicedToArray(_React$useState, 2),
  43. loadingKeys = _React$useState2[0],
  44. setLoadingKeys = _React$useState2[1];
  45. var internalLoadData = function internalLoadData(valueCells) {
  46. // Do not load when search
  47. if (!loadData || searchValue) {
  48. return;
  49. }
  50. var optionList = toPathOptions(valueCells, options, fieldNames);
  51. var rawOptions = optionList.map(function (_ref) {
  52. var option = _ref.option;
  53. return option;
  54. });
  55. var lastOption = rawOptions[rawOptions.length - 1];
  56. if (lastOption && !isLeaf(lastOption, fieldNames)) {
  57. var pathKey = toPathKey(valueCells);
  58. setLoadingKeys(function (keys) {
  59. return [].concat(_toConsumableArray(keys), [pathKey]);
  60. });
  61. loadData(rawOptions);
  62. }
  63. };
  64. // zombieJ: This is bad. We should make this same as `rc-tree` to use Promise instead.
  65. React.useEffect(function () {
  66. if (loadingKeys.length) {
  67. loadingKeys.forEach(function (loadingKey) {
  68. var valueStrCells = toPathValueStr(loadingKey);
  69. var optionList = toPathOptions(valueStrCells, options, fieldNames, true).map(function (_ref2) {
  70. var option = _ref2.option;
  71. return option;
  72. });
  73. var lastOption = optionList[optionList.length - 1];
  74. if (!lastOption || lastOption[fieldNames.children] || isLeaf(lastOption, fieldNames)) {
  75. setLoadingKeys(function (keys) {
  76. return keys.filter(function (key) {
  77. return key !== loadingKey;
  78. });
  79. });
  80. }
  81. });
  82. }
  83. }, [options, loadingKeys, fieldNames]);
  84. // ========================== Values ==========================
  85. var checkedSet = React.useMemo(function () {
  86. return new Set(toPathKeys(values));
  87. }, [values]);
  88. var halfCheckedSet = React.useMemo(function () {
  89. return new Set(toPathKeys(halfValues));
  90. }, [halfValues]);
  91. // ====================== Accessibility =======================
  92. var _useActive = useActive(multiple, open),
  93. _useActive2 = _slicedToArray(_useActive, 2),
  94. activeValueCells = _useActive2[0],
  95. setActiveValueCells = _useActive2[1];
  96. // =========================== Path ===========================
  97. var onPathOpen = function onPathOpen(nextValueCells) {
  98. setActiveValueCells(nextValueCells);
  99. // Trigger loadData
  100. internalLoadData(nextValueCells);
  101. };
  102. var isSelectable = function isSelectable(option) {
  103. if (disabled) {
  104. return false;
  105. }
  106. var optionDisabled = option.disabled;
  107. var isMergedLeaf = isLeaf(option, fieldNames);
  108. return !optionDisabled && (isMergedLeaf || changeOnSelect || multiple);
  109. };
  110. var onPathSelect = function onPathSelect(valuePath, leaf) {
  111. var fromKeyboard = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  112. onSelect(valuePath);
  113. if (!multiple && (leaf || changeOnSelect && (expandTrigger === 'hover' || fromKeyboard))) {
  114. toggleOpen(false);
  115. }
  116. };
  117. // ========================== Option ==========================
  118. var mergedOptions = React.useMemo(function () {
  119. if (searchValue) {
  120. return searchOptions;
  121. }
  122. return options;
  123. }, [searchValue, searchOptions, options]);
  124. // ========================== Column ==========================
  125. var optionColumns = React.useMemo(function () {
  126. var optionList = [{
  127. options: mergedOptions
  128. }];
  129. var currentList = mergedOptions;
  130. var fullPathKeys = getFullPathKeys(currentList, fieldNames);
  131. var _loop = function _loop() {
  132. var activeValueCell = activeValueCells[i];
  133. var currentOption = currentList.find(function (option, index) {
  134. return (fullPathKeys[index] ? toPathKey(fullPathKeys[index]) : option[fieldNames.value]) === activeValueCell;
  135. });
  136. var subOptions = currentOption === null || currentOption === void 0 ? void 0 : currentOption[fieldNames.children];
  137. if (!(subOptions !== null && subOptions !== void 0 && subOptions.length)) {
  138. return 1; // break
  139. }
  140. currentList = subOptions;
  141. optionList.push({
  142. options: subOptions
  143. });
  144. };
  145. for (var i = 0; i < activeValueCells.length; i += 1) {
  146. if (_loop()) break;
  147. }
  148. return optionList;
  149. }, [mergedOptions, activeValueCells, fieldNames]);
  150. // ========================= Keyboard =========================
  151. var onKeyboardSelect = function onKeyboardSelect(selectValueCells, option) {
  152. if (isSelectable(option)) {
  153. onPathSelect(selectValueCells, isLeaf(option, fieldNames), true);
  154. }
  155. };
  156. useKeyboard(ref, mergedOptions, fieldNames, activeValueCells, onPathOpen, onKeyboardSelect, {
  157. direction: direction,
  158. searchValue: searchValue,
  159. toggleOpen: toggleOpen,
  160. open: open
  161. });
  162. // >>>>> Active Scroll
  163. React.useEffect(function () {
  164. if (searchValue) {
  165. return;
  166. }
  167. for (var i = 0; i < activeValueCells.length; i += 1) {
  168. var _containerRef$current;
  169. var cellPath = activeValueCells.slice(0, i + 1);
  170. var cellKeyPath = toPathKey(cellPath);
  171. var ele = (_containerRef$current = containerRef.current) === null || _containerRef$current === void 0 ? void 0 : _containerRef$current.querySelector("li[data-path-key=\"".concat(cellKeyPath.replace(/\\{0,2}"/g, '\\"'), "\"]") // matches unescaped double quotes
  172. );
  173. if (ele) {
  174. scrollIntoParentView(ele);
  175. }
  176. }
  177. }, [activeValueCells, searchValue]);
  178. // ========================== Render ==========================
  179. // >>>>> Empty
  180. var isEmpty = !((_optionColumns$ = optionColumns[0]) !== null && _optionColumns$ !== void 0 && (_optionColumns$ = _optionColumns$.options) !== null && _optionColumns$ !== void 0 && _optionColumns$.length);
  181. var emptyList = [(_ref3 = {}, _defineProperty(_ref3, fieldNames.value, '__EMPTY__'), _defineProperty(_ref3, FIX_LABEL, notFoundContent), _defineProperty(_ref3, "disabled", true), _ref3)];
  182. var columnProps = _objectSpread(_objectSpread({}, props), {}, {
  183. multiple: !isEmpty && multiple,
  184. onSelect: onPathSelect,
  185. onActive: onPathOpen,
  186. onToggleOpen: toggleOpen,
  187. checkedSet: checkedSet,
  188. halfCheckedSet: halfCheckedSet,
  189. loadingKeys: loadingKeys,
  190. isSelectable: isSelectable
  191. });
  192. // >>>>> Columns
  193. var mergedOptionColumns = isEmpty ? [{
  194. options: emptyList
  195. }] : optionColumns;
  196. var columnNodes = mergedOptionColumns.map(function (col, index) {
  197. var prevValuePath = activeValueCells.slice(0, index);
  198. var activeValue = activeValueCells[index];
  199. return /*#__PURE__*/React.createElement(Column, _extends({
  200. key: index
  201. }, columnProps, {
  202. prefixCls: mergedPrefixCls,
  203. options: col.options,
  204. prevValuePath: prevValuePath,
  205. activeValue: activeValue
  206. }));
  207. });
  208. // >>>>> Render
  209. return /*#__PURE__*/React.createElement(CacheContent, {
  210. open: open
  211. }, /*#__PURE__*/React.createElement("div", {
  212. className: classNames("".concat(mergedPrefixCls, "-menus"), (_classNames = {}, _defineProperty(_classNames, "".concat(mergedPrefixCls, "-menu-empty"), isEmpty), _defineProperty(_classNames, "".concat(mergedPrefixCls, "-rtl"), rtl), _classNames)),
  213. ref: containerRef
  214. }, columnNodes));
  215. });
  216. if (process.env.NODE_ENV !== 'production') {
  217. RawOptionList.displayName = 'RawOptionList';
  218. }
  219. export default RawOptionList;