@tker-react/layout 0.2.4 → 0.2.6

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
@@ -87,6 +87,7 @@ interface LayoutContextValue {
87
87
  setCollapsed: (collapsed: boolean) => void;
88
88
  openKeys: Set<string>;
89
89
  toggleMenuOpen: (path: string, forceOpen?: boolean) => void;
90
+ menuItemClick: (path: string) => void;
90
91
  setExpandedWidth: (width: string) => void;
91
92
  setCollapsedWidth: (width: string) => void;
92
93
  setLayoutMode: (mode: "side-menu" | "top-menu") => void;
package/dist/index.d.ts CHANGED
@@ -87,6 +87,7 @@ interface LayoutContextValue {
87
87
  setCollapsed: (collapsed: boolean) => void;
88
88
  openKeys: Set<string>;
89
89
  toggleMenuOpen: (path: string, forceOpen?: boolean) => void;
90
+ menuItemClick: (path: string) => void;
90
91
  setExpandedWidth: (width: string) => void;
91
92
  setCollapsedWidth: (width: string) => void;
92
93
  setLayoutMode: (mode: "side-menu" | "top-menu") => void;
package/dist/index.mjs CHANGED
@@ -105,6 +105,10 @@ function LayoutProvider({ children }) {
105
105
  openKeysRef.current = openKeys;
106
106
  const collapsedRef = useRef(collapsed);
107
107
  collapsedRef.current = collapsed;
108
+ const menuDataRef = useRef(menuData);
109
+ menuDataRef.current = menuData;
110
+ const layoutModeRef = useRef(layoutMode);
111
+ layoutModeRef.current = layoutMode;
108
112
  const toggleMenuOpen = useCallback((path, forceOpen) => {
109
113
  setOpenKeys((prev) => {
110
114
  const next = new Set(prev);
@@ -120,6 +124,27 @@ function LayoutProvider({ children }) {
120
124
  return next;
121
125
  });
122
126
  }, []);
127
+ const menuItemClick = useCallback((path) => {
128
+ const item = findMenuItemByPath(menuDataRef.current, path);
129
+ const hasChildren = !!(item?.children && item.children.length > 0);
130
+ const isSide = layoutModeRef.current === "side-menu";
131
+ if (hasChildren) {
132
+ if (!isSide) return;
133
+ if (collapsedRef.current) {
134
+ setCollapsedState(false);
135
+ const restored = new Set(savedOpenKeysRef.current);
136
+ restored.add(path);
137
+ setOpenKeys(restored);
138
+ } else {
139
+ toggleMenuOpen(path);
140
+ }
141
+ } else {
142
+ const fullPath = pathParamsMap.current.get(path) || path;
143
+ if (window.location.pathname + window.location.search !== fullPath) {
144
+ navigateRef.current?.(fullPath);
145
+ }
146
+ }
147
+ }, []);
123
148
  const toggleCollapse = useCallback(() => {
124
149
  const nextCollapsed = !collapsedRef.current;
125
150
  if (nextCollapsed) {
@@ -180,6 +205,7 @@ function LayoutProvider({ children }) {
180
205
  setMenus,
181
206
  setActivePath,
182
207
  toggleCollapse,
208
+ // setCollapsed 同样处理 openKeys 保存/恢复,与 toggleCollapse 逻辑一致
183
209
  setCollapsed: (val) => {
184
210
  if (val) {
185
211
  savedOpenKeysRef.current = new Set(openKeysRef.current);
@@ -191,6 +217,7 @@ function LayoutProvider({ children }) {
191
217
  },
192
218
  openKeys,
193
219
  toggleMenuOpen,
220
+ menuItemClick,
194
221
  setExpandedWidth,
195
222
  setCollapsedWidth,
196
223
  setLayoutMode,
@@ -222,6 +249,7 @@ function LayoutProvider({ children }) {
222
249
  toggleCollapse,
223
250
  openKeys,
224
251
  toggleMenuOpen,
252
+ menuItemClick,
225
253
  isConcretePage,
226
254
  getFullPath,
227
255
  setNavigateAdapter,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tker-react/layout",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "type": "module",