@tker-react/layout 0.2.10 → 0.2.12

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.
Files changed (2) hide show
  1. package/dist/index.mjs +41 -29
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import "./layout.css";
2
2
  import { jsx, jsxs } from 'react/jsx-runtime';
3
- import { useRef, useCallback, useMemo, useState, useEffect, createContext, useContext } from 'react';
3
+ import { useRef, useCallback, useMemo, useState, useEffect, createContext, useContext, memo } from 'react';
4
4
 
5
5
  function findMenuItemByPath(menuData, path) {
6
6
  for (const item of menuData) {
@@ -131,6 +131,10 @@ function useLayoutContext() {
131
131
  if (!ctx) throw new Error("useLayoutContext \u5FC5\u987B\u5728 <Layout> \u5185\u90E8\u4F7F\u7528");
132
132
  return ctx;
133
133
  }
134
+ const ActivePathContext = createContext("");
135
+ function useActivePathContext() {
136
+ return useContext(ActivePathContext);
137
+ }
134
138
  function LayoutProvider({
135
139
  children,
136
140
  activePath = "",
@@ -258,7 +262,6 @@ function LayoutProvider({
258
262
  () => ({
259
263
  adapters,
260
264
  menuData: config.menus,
261
- activePath,
262
265
  expandedWidth: config.expandedWidth,
263
266
  collapsedWidth: config.collapsedWidth,
264
267
  layoutMode: config.layoutMode,
@@ -271,7 +274,6 @@ function LayoutProvider({
271
274
  [
272
275
  adapters,
273
276
  config.menus,
274
- activePath,
275
277
  config.expandedWidth,
276
278
  config.collapsedWidth,
277
279
  config.layoutMode,
@@ -282,7 +284,7 @@ function LayoutProvider({
282
284
  navigate
283
285
  ]
284
286
  );
285
- return /* @__PURE__ */ jsx(LayoutContext.Provider, { value, children: /* @__PURE__ */ jsx(LayoutInteractionContext.Provider, { value: interactionValue, children: /* @__PURE__ */ jsx(LayoutOpenKeysContext.Provider, { value: openKeysValue, children }) }) });
287
+ return /* @__PURE__ */ jsx(LayoutContext.Provider, { value, children: /* @__PURE__ */ jsx(LayoutInteractionContext.Provider, { value: interactionValue, children: /* @__PURE__ */ jsx(LayoutOpenKeysContext.Provider, { value: openKeysValue, children: /* @__PURE__ */ jsx(ActivePathContext.Provider, { value: activePath, children }) }) }) });
286
288
  }
287
289
 
288
290
  function LayoutAside() {
@@ -291,11 +293,11 @@ function LayoutAside() {
291
293
  expandedWidth,
292
294
  collapsedWidth,
293
295
  menuData,
294
- activePath,
295
296
  layoutMode,
296
297
  getFullPath,
297
298
  navigate
298
299
  } = useLayoutContext();
300
+ const activePath = useActivePathContext();
299
301
  const { collapsed, toggleCollapse, menuItemClick } = useLayoutInteractionContext();
300
302
  const { openKeys } = useLayoutOpenKeysContext();
301
303
  const menuMode = layoutMode === "top-menu" ? "top" : "side";
@@ -347,25 +349,37 @@ function LayoutAside() {
347
349
  ] });
348
350
  }
349
351
 
350
- function LayoutContent({ children }) {
352
+ const LayoutContent = memo(function LayoutContent2({ children }) {
353
+ const { layoutMode } = useLayoutContext();
354
+ const isTopMenu = layoutMode === "top-menu";
355
+ return /* @__PURE__ */ jsxs("main", { className: "tker-layout-content", children: [
356
+ /* @__PURE__ */ jsx(LayoutTabs, { isTopMenu }),
357
+ /* @__PURE__ */ jsx("div", { className: "tker-layout-content__body", children })
358
+ ] });
359
+ });
360
+ function LayoutTabs({ isTopMenu }) {
351
361
  const {
352
362
  adapters,
353
363
  menuData,
354
- activePath,
355
- layoutMode,
356
364
  homePath,
357
365
  maxTabs,
358
366
  getFullPath,
359
367
  navigate
360
368
  } = useLayoutContext();
361
- const isTopMenu = layoutMode === "top-menu";
362
- const [tabData, setTabData] = useState([]);
363
- const [activeTab, setActiveTab] = useState("");
369
+ const activePath = useActivePathContext();
364
370
  const menuDataRef = useRef(menuData);
365
371
  menuDataRef.current = menuData;
372
+ const initialTab = activePath && activePath !== "/" ? openTabInData([], activePath, { homePath, maxTabs, menuData }) : [];
373
+ const [tabData, setTabData] = useState(() => initialTab);
374
+ const [activeTab, setActiveTab] = useState(() => initialTab.length > 0 ? activePath : "");
366
375
  const activeTabRef = useRef(activeTab);
367
376
  activeTabRef.current = activeTab;
377
+ const isFirstRender = useRef(true);
368
378
  useEffect(() => {
379
+ if (isFirstRender.current) {
380
+ isFirstRender.current = false;
381
+ return;
382
+ }
369
383
  if (!activePath || activePath === "/") return;
370
384
  setTabData((prev) => {
371
385
  const exists = prev.find((t) => t.path === activePath);
@@ -445,21 +459,19 @@ function LayoutContent({ children }) {
445
459
  );
446
460
  }, []);
447
461
  const TabAdapter = adapters.tab;
448
- return /* @__PURE__ */ jsxs("main", { className: "tker-layout-content", children: [
449
- !isTopMenu && TabAdapter && /* @__PURE__ */ jsx(
450
- TabAdapter,
451
- {
452
- tabData,
453
- activeTab,
454
- onSelect: handleTabSelect,
455
- onClose: handleTabClose,
456
- onCloseOther: handleCloseOtherTabs,
457
- onCloseAll: handleCloseAllTabs,
458
- onSetFixed: handleSetFixed
459
- }
460
- ),
461
- /* @__PURE__ */ jsx("div", { className: "tker-layout-content__body", children })
462
- ] });
462
+ if (isTopMenu || !TabAdapter) return null;
463
+ return /* @__PURE__ */ jsx(
464
+ TabAdapter,
465
+ {
466
+ tabData,
467
+ activeTab,
468
+ onSelect: handleTabSelect,
469
+ onClose: handleTabClose,
470
+ onCloseOther: handleCloseOtherTabs,
471
+ onCloseAll: handleCloseAllTabs,
472
+ onSetFixed: handleSetFixed
473
+ }
474
+ );
463
475
  }
464
476
  function openTabInData(tabData, path, config) {
465
477
  const { homePath, maxTabs, menuData } = config;
@@ -510,12 +522,12 @@ function LayoutHeader() {
510
522
  expandedWidth,
511
523
  collapsedWidth,
512
524
  menuData,
513
- activePath,
514
525
  layoutMode,
515
526
  getFullPath,
516
527
  isConcretePage,
517
528
  navigate
518
529
  } = useLayoutContext();
530
+ const activePath = useActivePathContext();
519
531
  const { collapsed, menuItemClick } = useLayoutInteractionContext();
520
532
  const isTopMenu = layoutMode === "top-menu";
521
533
  const isSideMenu = layoutMode === "side-menu";
@@ -586,7 +598,7 @@ function LayoutHeader() {
586
598
  ] });
587
599
  }
588
600
 
589
- function LayoutInner({ children }) {
601
+ const LayoutInner = memo(function LayoutInner2({ children }) {
590
602
  const { layoutMode } = useLayoutContext();
591
603
  const isSideMenu = layoutMode === "side-menu";
592
604
  return /* @__PURE__ */ jsxs("div", { className: "tker-layout", children: [
@@ -596,7 +608,7 @@ function LayoutInner({ children }) {
596
608
  /* @__PURE__ */ jsx(LayoutContent, { children })
597
609
  ] })
598
610
  ] });
599
- }
611
+ });
600
612
  function Layout({ children, ...props }) {
601
613
  return /* @__PURE__ */ jsx(LayoutProvider, { ...props, children: /* @__PURE__ */ jsx(LayoutInner, { children }) });
602
614
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tker-react/layout",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "type": "module",