@tker-react/layout 0.2.6 → 0.2.7

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
@@ -39,6 +39,10 @@ interface MenuAdapterProps {
39
39
  mode: "side" | "top";
40
40
  width: string;
41
41
  onSelect: (path: string) => void;
42
+ /** 当前展开的子菜单路径集合(side-menu 模式使用) */
43
+ openKeys: Set<string>;
44
+ /** 统一菜单点击入口,自动处理展开/折叠/导航,替代 onSelect 时 adapter 无需自行判断菜单类型 */
45
+ onMenuItemClick: (path: string) => void;
42
46
  }
43
47
  interface BreadcrumbAdapterProps {
44
48
  breadcrumbData: BreadcrumbItem[];
@@ -77,17 +81,11 @@ interface LayoutContextValue {
77
81
  setUserAvatarAdapter: (component: ComponentType<UserAvatarAdapterProps>) => void;
78
82
  menuData: MenuItem[];
79
83
  activePath: string;
80
- collapsed: boolean;
81
84
  expandedWidth: string;
82
85
  collapsedWidth: string;
83
86
  layoutMode: "side-menu" | "top-menu";
84
87
  setMenus: (data: MenuItem[]) => void;
85
88
  setActivePath: (path: string, fullPath?: string) => void;
86
- toggleCollapse: () => void;
87
- setCollapsed: (collapsed: boolean) => void;
88
- openKeys: Set<string>;
89
- toggleMenuOpen: (path: string, forceOpen?: boolean) => void;
90
- menuItemClick: (path: string) => void;
91
89
  setExpandedWidth: (width: string) => void;
92
90
  setCollapsedWidth: (width: string) => void;
93
91
  setLayoutMode: (mode: "side-menu" | "top-menu") => void;
@@ -105,5 +103,5 @@ declare function LayoutProvider({ children }: {
105
103
  children: ReactNode;
106
104
  }): react_jsx_runtime.JSX.Element;
107
105
 
108
- export { Layout, LayoutProvider, useLayoutContext as useLayout, useLayoutContext };
106
+ export { Layout, LayoutProvider, useLayoutContext as useLayout };
109
107
  export type { BreadcrumbAdapterProps, BreadcrumbItem, LayoutAdapters, LogoAdapterProps, MenuAdapterProps, MenuItem, TabAdapterProps, TabItem, UserAvatarAdapterProps };
package/dist/index.d.ts CHANGED
@@ -39,6 +39,10 @@ interface MenuAdapterProps {
39
39
  mode: "side" | "top";
40
40
  width: string;
41
41
  onSelect: (path: string) => void;
42
+ /** 当前展开的子菜单路径集合(side-menu 模式使用) */
43
+ openKeys: Set<string>;
44
+ /** 统一菜单点击入口,自动处理展开/折叠/导航,替代 onSelect 时 adapter 无需自行判断菜单类型 */
45
+ onMenuItemClick: (path: string) => void;
42
46
  }
43
47
  interface BreadcrumbAdapterProps {
44
48
  breadcrumbData: BreadcrumbItem[];
@@ -77,17 +81,11 @@ interface LayoutContextValue {
77
81
  setUserAvatarAdapter: (component: ComponentType<UserAvatarAdapterProps>) => void;
78
82
  menuData: MenuItem[];
79
83
  activePath: string;
80
- collapsed: boolean;
81
84
  expandedWidth: string;
82
85
  collapsedWidth: string;
83
86
  layoutMode: "side-menu" | "top-menu";
84
87
  setMenus: (data: MenuItem[]) => void;
85
88
  setActivePath: (path: string, fullPath?: string) => void;
86
- toggleCollapse: () => void;
87
- setCollapsed: (collapsed: boolean) => void;
88
- openKeys: Set<string>;
89
- toggleMenuOpen: (path: string, forceOpen?: boolean) => void;
90
- menuItemClick: (path: string) => void;
91
89
  setExpandedWidth: (width: string) => void;
92
90
  setCollapsedWidth: (width: string) => void;
93
91
  setLayoutMode: (mode: "side-menu" | "top-menu") => void;
@@ -105,5 +103,5 @@ declare function LayoutProvider({ children }: {
105
103
  children: ReactNode;
106
104
  }): react_jsx_runtime.JSX.Element;
107
105
 
108
- export { Layout, LayoutProvider, useLayoutContext as useLayout, useLayoutContext };
106
+ export { Layout, LayoutProvider, useLayoutContext as useLayout };
109
107
  export type { BreadcrumbAdapterProps, BreadcrumbItem, LayoutAdapters, LogoAdapterProps, MenuAdapterProps, MenuItem, TabAdapterProps, TabItem, UserAvatarAdapterProps };
package/dist/index.mjs CHANGED
@@ -44,6 +44,14 @@ function generateBreadcrumb(menuData, activePath) {
44
44
  }
45
45
 
46
46
  const LayoutContext = createContext(null);
47
+ const LayoutInteractionContext = createContext(null);
48
+ function useLayoutInteractionContext() {
49
+ const ctx = useContext(LayoutInteractionContext);
50
+ if (!ctx) {
51
+ throw new Error("useLayoutInteractionContext \u5FC5\u987B\u5728 <Layout> \u5185\u90E8\u4F7F\u7528");
52
+ }
53
+ return ctx;
54
+ }
47
55
  function useLayoutContext() {
48
56
  const ctx = useContext(LayoutContext);
49
57
  if (!ctx) {
@@ -187,6 +195,26 @@ function LayoutProvider({ children }) {
187
195
  const setMaxTabs = useCallback((max) => {
188
196
  setMaxTabsState(max);
189
197
  }, []);
198
+ const setCollapsed = useCallback((val) => {
199
+ if (val) {
200
+ savedOpenKeysRef.current = new Set(openKeysRef.current);
201
+ setOpenKeys(/* @__PURE__ */ new Set());
202
+ } else {
203
+ setOpenKeys(new Set(savedOpenKeysRef.current));
204
+ }
205
+ setCollapsedState(val);
206
+ }, []);
207
+ const interactionValue = useMemo(
208
+ () => ({
209
+ collapsed,
210
+ toggleCollapse,
211
+ setCollapsed,
212
+ openKeys,
213
+ toggleMenuOpen,
214
+ menuItemClick
215
+ }),
216
+ [collapsed, toggleCollapse, setCollapsed, openKeys, toggleMenuOpen, menuItemClick]
217
+ );
190
218
  const value = useMemo(
191
219
  () => ({
192
220
  adapters,
@@ -198,26 +226,11 @@ function LayoutProvider({ children }) {
198
226
  setUserAvatarAdapter,
199
227
  menuData,
200
228
  activePath,
201
- collapsed,
202
229
  expandedWidth,
203
230
  collapsedWidth,
204
231
  layoutMode,
205
232
  setMenus,
206
233
  setActivePath,
207
- toggleCollapse,
208
- // setCollapsed 同样处理 openKeys 保存/恢复,与 toggleCollapse 逻辑一致
209
- setCollapsed: (val) => {
210
- if (val) {
211
- savedOpenKeysRef.current = new Set(openKeysRef.current);
212
- setOpenKeys(/* @__PURE__ */ new Set());
213
- } else {
214
- setOpenKeys(new Set(savedOpenKeysRef.current));
215
- }
216
- setCollapsedState(val);
217
- },
218
- openKeys,
219
- toggleMenuOpen,
220
- menuItemClick,
221
234
  setExpandedWidth,
222
235
  setCollapsedWidth,
223
236
  setLayoutMode,
@@ -240,16 +253,11 @@ function LayoutProvider({ children }) {
240
253
  setUserAvatarAdapter,
241
254
  menuData,
242
255
  activePath,
243
- collapsed,
244
256
  expandedWidth,
245
257
  collapsedWidth,
246
258
  layoutMode,
247
259
  setMenus,
248
260
  setActivePath,
249
- toggleCollapse,
250
- openKeys,
251
- toggleMenuOpen,
252
- menuItemClick,
253
261
  isConcretePage,
254
262
  getFullPath,
255
263
  setNavigateAdapter,
@@ -260,22 +268,21 @@ function LayoutProvider({ children }) {
260
268
  setMaxTabs
261
269
  ]
262
270
  );
263
- return /* @__PURE__ */ jsx(LayoutContext.Provider, { value, children });
271
+ return /* @__PURE__ */ jsx(LayoutContext.Provider, { value, children: /* @__PURE__ */ jsx(LayoutInteractionContext.Provider, { value: interactionValue, children }) });
264
272
  }
265
273
 
266
274
  function LayoutAside() {
267
275
  const {
268
276
  adapters,
269
- collapsed,
270
277
  expandedWidth,
271
278
  collapsedWidth,
272
279
  menuData,
273
280
  activePath,
274
281
  layoutMode,
275
282
  getFullPath,
276
- navigate,
277
- toggleCollapse
283
+ navigate
278
284
  } = useLayoutContext();
285
+ const { collapsed, toggleCollapse, openKeys, menuItemClick } = useLayoutInteractionContext();
279
286
  const menuMode = layoutMode === "top-menu" ? "top" : "side";
280
287
  const handleMenuSelect = useCallback(
281
288
  (path) => {
@@ -297,7 +304,9 @@ function LayoutAside() {
297
304
  collapsed,
298
305
  mode: menuMode,
299
306
  width: asideWidth,
300
- onSelect: handleMenuSelect
307
+ openKeys,
308
+ onSelect: handleMenuSelect,
309
+ onMenuItemClick: menuItemClick
301
310
  }
302
311
  ),
303
312
  /* @__PURE__ */ jsx(
@@ -488,7 +497,6 @@ function openTabInData(tabData, path, config) {
488
497
  function LayoutHeader() {
489
498
  const {
490
499
  adapters,
491
- collapsed,
492
500
  expandedWidth,
493
501
  collapsedWidth,
494
502
  menuData,
@@ -498,6 +506,7 @@ function LayoutHeader() {
498
506
  isConcretePage,
499
507
  navigate
500
508
  } = useLayoutContext();
509
+ const { collapsed, menuItemClick } = useLayoutInteractionContext();
501
510
  const isTopMenu = layoutMode === "top-menu";
502
511
  const isSideMenu = layoutMode === "side-menu";
503
512
  const breadcrumbData = useMemo(
@@ -546,7 +555,9 @@ function LayoutHeader() {
546
555
  collapsed: false,
547
556
  mode: "top",
548
557
  width: logoWidth,
549
- onSelect: handleMenuSelect
558
+ openKeys: /* @__PURE__ */ new Set(),
559
+ onSelect: handleMenuSelect,
560
+ onMenuItemClick: menuItemClick
550
561
  }
551
562
  ),
552
563
  BreadcrumbAdapter && isSideMenu && /* @__PURE__ */ jsx(
@@ -579,4 +590,4 @@ function Layout({ children }) {
579
590
  return /* @__PURE__ */ jsx(LayoutProvider, { children: /* @__PURE__ */ jsx(LayoutInner, { children }) });
580
591
  }
581
592
 
582
- export { Layout, LayoutProvider, useLayoutContext as useLayout, useLayoutContext };
593
+ export { Layout, LayoutProvider, useLayoutContext as useLayout };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tker-react/layout",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "type": "module",