listpage-next 0.0.39 → 0.0.41

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.
@@ -1,2 +1,2 @@
1
1
  import { MenuItem } from '../types';
2
- export declare function useActiveMenuKey(items: MenuItem[]): [string | null, (key: string) => void];
2
+ export declare function useActiveMenuKey(items: MenuItem[], baseUrl?: string): [string | null, (key: string) => void];
@@ -1,6 +1,6 @@
1
1
  import { useCallback, useMemo } from "react";
2
2
  import { useLocation, useNavigate } from "react-router-dom";
3
- function useActiveMenuKey(items) {
3
+ function useActiveMenuKey(items, baseUrl = '') {
4
4
  const location = useLocation();
5
5
  const navigate = useNavigate();
6
6
  const setActiveKey = useCallback((key)=>{
@@ -29,17 +29,33 @@ function useActiveMenuKey(items) {
29
29
  navigate
30
30
  ]);
31
31
  const activeKey = useMemo(()=>{
32
- const paths = getPaths(location.pathname);
32
+ const filteredPaths = removeBaseUrl(location.pathname, baseUrl);
33
+ const paths = getPaths(filteredPaths);
33
34
  return matchRoutes(items, paths);
34
35
  }, [
35
36
  location.pathname,
36
- items
37
+ items,
38
+ baseUrl
37
39
  ]);
38
40
  return [
39
41
  activeKey,
40
42
  setActiveKey
41
43
  ];
42
44
  }
45
+ const removeBaseUrl = (pathname, baseUrl)=>{
46
+ if ('' === baseUrl) return pathname;
47
+ let normalizedBaseUrl = baseUrl;
48
+ if (!baseUrl.startsWith('/')) normalizedBaseUrl = '/' + baseUrl;
49
+ let normalizedPathname = pathname;
50
+ if (!pathname.startsWith('/')) normalizedPathname = '/' + pathname;
51
+ if (normalizedPathname.startsWith(normalizedBaseUrl)) {
52
+ if (normalizedPathname === normalizedBaseUrl || '/' === normalizedPathname.charAt(normalizedBaseUrl.length)) {
53
+ const result = normalizedPathname.substring(normalizedBaseUrl.length);
54
+ return '' === result ? '/' : result.startsWith('/') ? result : '/' + result;
55
+ }
56
+ }
57
+ return pathname;
58
+ };
43
59
  const matchRoutes = (routes, paths)=>{
44
60
  for (const route of routes){
45
61
  const routePaths = getPaths(route.path || route.key);
@@ -3,8 +3,8 @@ import { useMemo } from "react";
3
3
  import { Menu } from "antd";
4
4
  import { useActiveMenuKey } from "./hooks/useMenuNavigation.js";
5
5
  const Menu_Menu = (props)=>{
6
- const { menus, theme, style } = props;
7
- const [activeKey, setActiveKey] = useActiveMenuKey(menus);
6
+ const { menus, theme, style, baseUrl } = props;
7
+ const [activeKey, setActiveKey] = useActiveMenuKey(menus, baseUrl);
8
8
  const menuItems = useMemo(()=>menus.map((item)=>({
9
9
  key: item.key,
10
10
  label: item.label,
@@ -11,4 +11,5 @@ export interface MenuProps {
11
11
  menus: MenuItem[];
12
12
  theme?: 'light' | 'dark';
13
13
  style?: CSSProperties;
14
+ baseUrl?: string;
14
15
  }
@@ -1,6 +1,6 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
- import styled_components from "styled-components";
3
2
  import "react";
3
+ import styled_components from "styled-components";
4
4
  import { Layout } from "antd";
5
5
  import { useControllableValue } from "ahooks";
6
6
  import { PageContextProvider } from "../../context/page.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "listpage-next",
3
- "version": "0.0.39",
3
+ "version": "0.0.41",
4
4
  "description": "A React component library for creating filter forms with Ant Design",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",