@tker-react/layout 0.2.3 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -85,6 +85,9 @@ interface LayoutContextValue {
85
85
  setActivePath: (path: string, fullPath?: string) => void;
86
86
  toggleCollapse: () => void;
87
87
  setCollapsed: (collapsed: boolean) => void;
88
+ openKeys: Set<string>;
89
+ toggleMenuOpen: (path: string, forceOpen?: boolean) => void;
90
+ menuItemClick: (path: string) => void;
88
91
  setExpandedWidth: (width: string) => void;
89
92
  setCollapsedWidth: (width: string) => void;
90
93
  setLayoutMode: (mode: "side-menu" | "top-menu") => void;
package/dist/index.d.ts CHANGED
@@ -85,6 +85,9 @@ interface LayoutContextValue {
85
85
  setActivePath: (path: string, fullPath?: string) => void;
86
86
  toggleCollapse: () => void;
87
87
  setCollapsed: (collapsed: boolean) => void;
88
+ openKeys: Set<string>;
89
+ toggleMenuOpen: (path: string, forceOpen?: boolean) => void;
90
+ menuItemClick: (path: string) => void;
88
91
  setExpandedWidth: (width: string) => void;
89
92
  setCollapsedWidth: (width: string) => void;
90
93
  setLayoutMode: (mode: "side-menu" | "top-menu") => void;
package/dist/index.mjs CHANGED
@@ -99,10 +99,58 @@ function LayoutProvider({ children }) {
99
99
  "side-menu"
100
100
  );
101
101
  const setMenus = useCallback((data) => setMenuData(data), []);
102
- const toggleCollapse = useCallback(
103
- () => setCollapsedState((p) => !p),
104
- []
105
- );
102
+ const [openKeys, setOpenKeys] = useState(/* @__PURE__ */ new Set());
103
+ const savedOpenKeysRef = useRef(/* @__PURE__ */ new Set());
104
+ const openKeysRef = useRef(openKeys);
105
+ openKeysRef.current = openKeys;
106
+ const collapsedRef = useRef(collapsed);
107
+ collapsedRef.current = collapsed;
108
+ const menuDataRef = useRef(menuData);
109
+ menuDataRef.current = menuData;
110
+ const toggleMenuOpen = useCallback((path, forceOpen) => {
111
+ setOpenKeys((prev) => {
112
+ const next = new Set(prev);
113
+ if (forceOpen === true) {
114
+ next.add(path);
115
+ } else if (forceOpen === false) {
116
+ next.delete(path);
117
+ } else if (next.has(path)) {
118
+ next.delete(path);
119
+ } else {
120
+ next.add(path);
121
+ }
122
+ return next;
123
+ });
124
+ }, []);
125
+ const menuItemClick = useCallback((path) => {
126
+ const item = findMenuItemByPath(menuDataRef.current, path);
127
+ const hasChildren = !!(item?.children && item.children.length > 0);
128
+ if (hasChildren) {
129
+ if (collapsedRef.current) {
130
+ setCollapsedState(false);
131
+ const restored = new Set(savedOpenKeysRef.current);
132
+ restored.add(path);
133
+ setOpenKeys(restored);
134
+ } else {
135
+ toggleMenuOpen(path);
136
+ }
137
+ } else {
138
+ const fullPath = pathParamsMap.current.get(path) || path;
139
+ if (window.location.pathname + window.location.search !== fullPath) {
140
+ navigateRef.current?.(fullPath);
141
+ }
142
+ }
143
+ }, []);
144
+ const toggleCollapse = useCallback(() => {
145
+ const nextCollapsed = !collapsedRef.current;
146
+ if (nextCollapsed) {
147
+ savedOpenKeysRef.current = new Set(openKeysRef.current);
148
+ setOpenKeys(/* @__PURE__ */ new Set());
149
+ } else {
150
+ setOpenKeys(new Set(savedOpenKeysRef.current));
151
+ }
152
+ setCollapsedState(nextCollapsed);
153
+ }, []);
106
154
  const pathParamsMap = useRef(/* @__PURE__ */ new Map());
107
155
  const storePathParams = useCallback((path, fullPath) => {
108
156
  if (fullPath && fullPath !== path) {
@@ -153,7 +201,19 @@ function LayoutProvider({ children }) {
153
201
  setMenus,
154
202
  setActivePath,
155
203
  toggleCollapse,
156
- setCollapsed: setCollapsedState,
204
+ // setCollapsed 同样处理 openKeys 保存/恢复,与 toggleCollapse 逻辑一致
205
+ setCollapsed: (val) => {
206
+ if (val) {
207
+ savedOpenKeysRef.current = new Set(openKeysRef.current);
208
+ setOpenKeys(/* @__PURE__ */ new Set());
209
+ } else {
210
+ setOpenKeys(new Set(savedOpenKeysRef.current));
211
+ }
212
+ setCollapsedState(val);
213
+ },
214
+ openKeys,
215
+ toggleMenuOpen,
216
+ menuItemClick,
157
217
  setExpandedWidth,
158
218
  setCollapsedWidth,
159
219
  setLayoutMode,
@@ -183,6 +243,9 @@ function LayoutProvider({ children }) {
183
243
  setMenus,
184
244
  setActivePath,
185
245
  toggleCollapse,
246
+ openKeys,
247
+ toggleMenuOpen,
248
+ menuItemClick,
186
249
  isConcretePage,
187
250
  getFullPath,
188
251
  setNavigateAdapter,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tker-react/layout",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "type": "module",