UNPKG

3.75 kBJavaScriptView Raw
1import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3import * as React from 'react';
4import { useRef, useCallback } from 'react';
5import warning from "rc-util/es/warning";
6import { nextSlice } from '../utils/timeUtil';
7var PATH_SPLIT = '__RC_UTIL_PATH_SPLIT__';
8
9var getPathStr = function getPathStr(keyPath) {
10 return keyPath.join(PATH_SPLIT);
11};
12
13var getPathKeys = function getPathKeys(keyPathStr) {
14 return keyPathStr.split(PATH_SPLIT);
15};
16
17export var OVERFLOW_KEY = 'rc-menu-more';
18export default function useKeyRecords() {
19 var _React$useState = React.useState({}),
20 _React$useState2 = _slicedToArray(_React$useState, 2),
21 internalForceUpdate = _React$useState2[1];
22
23 var key2pathRef = useRef(new Map());
24 var path2keyRef = useRef(new Map());
25
26 var _React$useState3 = React.useState([]),
27 _React$useState4 = _slicedToArray(_React$useState3, 2),
28 overflowKeys = _React$useState4[0],
29 setOverflowKeys = _React$useState4[1];
30
31 var updateRef = useRef(0);
32 var destroyRef = useRef(false);
33
34 var forceUpdate = function forceUpdate() {
35 if (!destroyRef.current) {
36 internalForceUpdate({});
37 }
38 };
39
40 var registerPath = useCallback(function (key, keyPath) {
41 // Warning for invalidate or duplicated `key`
42 if (process.env.NODE_ENV !== 'production') {
43 warning(!key2pathRef.current.has(key), "Duplicated key '".concat(key, "' used in Menu by path [").concat(keyPath.join(' > '), "]"));
44 } // Fill map
45
46
47 var connectedPath = getPathStr(keyPath);
48 path2keyRef.current.set(connectedPath, key);
49 key2pathRef.current.set(key, connectedPath);
50 updateRef.current += 1;
51 var id = updateRef.current;
52 nextSlice(function () {
53 if (id === updateRef.current) {
54 forceUpdate();
55 }
56 });
57 }, []);
58 var unregisterPath = useCallback(function (key, keyPath) {
59 var connectedPath = getPathStr(keyPath);
60 path2keyRef.current.delete(connectedPath);
61 key2pathRef.current.delete(key);
62 }, []);
63 var refreshOverflowKeys = useCallback(function (keys) {
64 setOverflowKeys(keys);
65 }, []);
66 var getKeyPath = useCallback(function (eventKey, includeOverflow) {
67 var fullPath = key2pathRef.current.get(eventKey) || '';
68 var keys = getPathKeys(fullPath);
69
70 if (includeOverflow && overflowKeys.includes(keys[0])) {
71 keys.unshift(OVERFLOW_KEY);
72 }
73
74 return keys;
75 }, [overflowKeys]);
76 var isSubPathKey = useCallback(function (pathKeys, eventKey) {
77 return pathKeys.some(function (pathKey) {
78 var pathKeyList = getKeyPath(pathKey, true);
79 return pathKeyList.includes(eventKey);
80 });
81 }, [getKeyPath]);
82
83 var getKeys = function getKeys() {
84 var keys = _toConsumableArray(key2pathRef.current.keys());
85
86 if (overflowKeys.length) {
87 keys.push(OVERFLOW_KEY);
88 }
89
90 return keys;
91 };
92 /**
93 * Find current key related child path keys
94 */
95
96
97 var getSubPathKeys = useCallback(function (key) {
98 var connectedPath = "".concat(key2pathRef.current.get(key)).concat(PATH_SPLIT);
99 var pathKeys = new Set();
100
101 _toConsumableArray(path2keyRef.current.keys()).forEach(function (pathKey) {
102 if (pathKey.startsWith(connectedPath)) {
103 pathKeys.add(path2keyRef.current.get(pathKey));
104 }
105 });
106
107 return pathKeys;
108 }, []);
109 React.useEffect(function () {
110 return function () {
111 destroyRef.current = true;
112 };
113 }, []);
114 return {
115 // Register
116 registerPath: registerPath,
117 unregisterPath: unregisterPath,
118 refreshOverflowKeys: refreshOverflowKeys,
119 // Util
120 isSubPathKey: isSubPathKey,
121 getKeyPath: getKeyPath,
122 getKeys: getKeys,
123 getSubPathKeys: getSubPathKeys
124 };
125}
\No newline at end of file