@hzab/sider 0.0.2

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/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@hzab/sider",
3
+ "version": "0.0.2",
4
+ "description": "侧边栏",
5
+ "main": "lib",
6
+ "scripts": {
7
+ "dev": "webpack serve -c ./config/webpack.config.js --env local",
8
+ "build": "webpack -c ./config/webpack.config.js --env production",
9
+ "publish-patch": "npm run build && git add . && git commit -m \"build: patch\" && npm version patch && npm publish --access public",
10
+ "publish-minor": "npm run build && git add . && git commit -m \"build: minor\" && npm version minor && npm publish --access public",
11
+ "publish-major": "npm run build && git add . && git commit -m \"build: major\" && npm version major && npm publish --access public"
12
+ },
13
+ "files": [
14
+ "lib",
15
+ "src"
16
+ ],
17
+ "keywords": [],
18
+ "author": "CaiYansong",
19
+ "license": "ISC",
20
+ "devDependencies": {
21
+ "@hzab/webpack-config": "0.0.9",
22
+ "@types/react": "^18.0.26",
23
+ "@types/react-dom": "^18.0.9",
24
+ "eslint": "^8.30.0",
25
+ "less": "^4.1.3",
26
+ "react": "^18.2.0",
27
+ "react-dom": "^18.2.0",
28
+ "react-router-dom": "^6.8.1",
29
+ "typescript": "^4.9.4"
30
+ },
31
+ "directories": {
32
+ "lib": "lib"
33
+ },
34
+ "dependencies": {
35
+ "@ant-design/icons": "^4.8.0",
36
+ "antd": "^4.14.0"
37
+ }
38
+ }
@@ -0,0 +1,23 @@
1
+ .sider-box {
2
+ height: 100%;
3
+ overflow-x: hidden;
4
+ overflow-y: auto;
5
+ /* 滚动条整体样式 */
6
+ &::-webkit-scrollbar {
7
+ width: 4px;
8
+ height: 4px;
9
+ }
10
+ /* 滚动条轨道 */
11
+ &::-webkit-scrollbar-track {
12
+ background: rgb(239, 239, 239);
13
+ }
14
+ /* 滚动条里面的滑块 */
15
+ &::-webkit-scrollbar-thumb {
16
+ background: rgba(144, 147, 153, 0.3);
17
+ border-radius: 4px;
18
+ }
19
+ /* 滚动条里面的滑块 hover 的情况 */
20
+ &::-webkit-scrollbar-thumb:hover {
21
+ background: rgba(144, 147, 153, 0.5);
22
+ }
23
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,82 @@
1
+ import React, { useEffect, useState } from "react";
2
+ import { useNavigate, useLocation } from "react-router-dom";
3
+ import { Menu } from "antd";
4
+ import * as Icon from "@ant-design/icons";
5
+ import _ from "lodash";
6
+
7
+ import style from "./index.module.less";
8
+
9
+ function Sider(props) {
10
+ const navigate = useNavigate();
11
+ const location = useLocation();
12
+ const [selected, setSelected] = useState([location.pathname]);
13
+ const [open, setOpen] = useState(location.pathname.split(/(?=\/)/)?.slice(0, 1));
14
+ const [menus, setMenus] = useState(props.menus || []);
15
+
16
+ useEffect(() => {
17
+ const keyPaths: Array<string> = location.pathname.split(/(?=\/)/);
18
+
19
+ if (keyPaths.length > 0) {
20
+ // 处理侧边栏激活状态,解决详情等子页面无激活态的问题
21
+ // 注意:子页面必须以父页面的路由为前缀。如:/user-server/user-list /user-server/user-list/detail
22
+ const _open = [];
23
+ const _selected = [];
24
+ let cur = "";
25
+
26
+ keyPaths.forEach((it) => {
27
+ cur = `${cur}${it}`;
28
+ _open.push(cur);
29
+ _selected.push(cur);
30
+ });
31
+
32
+ setSelected(_selected);
33
+ setOpen(_open);
34
+ }
35
+ }, [location.pathname]);
36
+
37
+ useEffect(() => {
38
+ const _menus = _.cloneDeep(props.menus);
39
+ _menus?.map((it) => {
40
+ let iconDom = it.icon;
41
+ if (/^[A-Z]/.test(iconDom) && Icon && (Icon as any)[iconDom]) {
42
+ iconDom = React.createElement(Icon && (Icon as any)[iconDom], {
43
+ style: { fontSize: "14px" },
44
+ });
45
+ } else if (iconDom?.startsWith("icon-")) {
46
+ iconDom = <i className={"sider-icon icon-iconfont " + iconDom}></i>;
47
+ }
48
+ it.icon = iconDom;
49
+ });
50
+ setMenus(_menus);
51
+ }, [props.menus]);
52
+
53
+ async function onClick(item) {
54
+ if (props.onClick) {
55
+ const res = await props.onClick(item);
56
+ if (res === false) {
57
+ return;
58
+ }
59
+ }
60
+ navigate(item.key);
61
+ }
62
+
63
+ function onOpenChange(openKeys) {
64
+ setOpen(openKeys);
65
+ props.onOpenChange && props.onOpenChange(openKeys);
66
+ }
67
+
68
+ return (
69
+ <Menu
70
+ className={`sider-box ${style["sider-box"]} ${props.className || ""}`}
71
+ style={{ width: "200px", ...(props.style || {}) }}
72
+ onClick={onClick}
73
+ onOpenChange={onOpenChange}
74
+ selectedKeys={selected}
75
+ openKeys={open}
76
+ mode="inline"
77
+ items={menus}
78
+ />
79
+ );
80
+ }
81
+
82
+ export default Sider;