@hzab/hierarchical-statistics 0.0.1

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/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ # @hzab/component-boilerplate@0.0.1
2
+ 组件初始化
package/README.md ADDED
@@ -0,0 +1,125 @@
1
+ # @hzab/group-user-selector
2
+
3
+ 组件模板
4
+
5
+ - node@16.16.0
6
+
7
+ - 注意:首次克隆先执行 npm run prepare 命令
8
+
9
+ # 组件
10
+
11
+ ## 示例
12
+
13
+ ```jsx
14
+ import { InfiniteListUIPc, InfiniteListUIMobile } from "@hzab/hierarchical-statistics";
15
+ <InfiniteListUIPc
16
+ ref={listRef}
17
+ style={{
18
+ maxWidth: "400px",
19
+ }}
20
+ listModal={
21
+ new DataModel({
22
+ getListApi: "/api/v1/sysOrg/getDeptTree",
23
+ query: {
24
+ pageNum: 1,
25
+ pageSize: 8,
26
+ },
27
+ })
28
+ }
29
+ fieldList={{
30
+ value: "orgId",
31
+ label: "orgName",
32
+ }}
33
+ virtualPagedListProps={{
34
+ height: 300,
35
+ dynamicHeightMode: true,
36
+ renderItem: (record) => (
37
+ <div
38
+ onClick={() => {
39
+ listRef.current.handleNodeClick(record);
40
+ }}
41
+ style={{ padding: "12px 30px", border: "1px solid red" }}
42
+ >
43
+ <div>{record?.orgName}</div>
44
+ </div>
45
+ ),
46
+ noMoreComponent: <div>没有更多数据了</div>,
47
+ }}
48
+ ></InfiniteListUIPc>
49
+ ```
50
+
51
+ ## API
52
+
53
+ ### InfoPanel Attributes
54
+
55
+ | 参数 | 类型 | 必填 | 默认值 | 说明 |
56
+ | -------------------- | ------------------------------| ----- | --------- | ----------------------------------- |
57
+ | listModal | Object | 是 | - | 列表数据模型DataModel,必须包含getListApi |
58
+ | breadModel | Object | 否 | - | 回显数据模型DataModel,必须包含getApi |
59
+ | showHome | boolean | 否 | - | 是否显示面包屑首页 |
60
+ | onNodeClick | function(item) | 否 | - | item点击事件 |
61
+ | onNodeSelect | function(item) | 否 | - | item选中事件 |
62
+ | onBreadcrumbClick | function(item) | 否 | - | 回显点击事件 |
63
+ | fieldList | {value: number, label: string}| 否 | - | 列表默认关联字段 |
64
+ | fieldBreadcrumb | {value: number, label: string}| 否 | - | 面包屑显示对应关联字段 |
65
+ | parentField | string | 否 | parentId | 请求接口关联字段 |
66
+ | homeBreadcrumb | string | 否 | 首页 | 面包屑首页名称 |
67
+ | style | CSSProperties | 否 | - | 组件外层样式 |
68
+ | listStyle | CSSProperties | 否 | - | 列表外层样式 |
69
+ | readcrumbStyle | CSSProperties | 否 | - | 面包屑外层样式 |
70
+ | separatorStyle | CSSProperties | 否 | - | 面包屑分割符样式 |
71
+ | listRenderProps | Object | 否 | - | list-render-mobile 配置 |
72
+ | virtualPagedListProps| Object | 否 | - | virtualPagedList 配置 |
73
+ | separator | string | 否 | - | 分隔符 默认"-" |
74
+ | Slots | Object | 是 | - | 插槽 |
75
+
76
+
77
+
78
+ ### Slots
79
+
80
+ | 参数 | 类型 | 必填 | 默认值 | 说明 |
81
+ | -------------------- | -------------------| ----- | ------- | ----------------------------------- |
82
+ | fixRenderCenter | Function | 否 | - | 固定在头部 面包屑和列表之间插槽 |
83
+ | itemRender | Function | 是 | - | 自定义列表项 |
84
+
85
+ # 组件开发流程
86
+
87
+ - 在 config/webpack.config.js 中按需修改 library 配置的文件名
88
+ - 在 config/webpack.config.js 中按需修改 alias 配置的包名,便于本地调试
89
+ - 在 tsconfig.json 中按需修改 paths 配置的包名,解决 ts 报错问题
90
+ - npm run dev
91
+
92
+ ## 文件目录
93
+
94
+ - example 本地开发测试代码
95
+ - src 组件源码
96
+
97
+ ## 命令
98
+
99
+ - Mac 执行该命令,设置 pre-commit 为可执行文件
100
+
101
+ - npm run mac-chmod
102
+ - chmod +x .husky && chmod +x .husky/pre-commit
103
+
104
+ - 生成文档:npm run docs
105
+ - 本地运行:npm run dev
106
+ - 打包编译:npm run build
107
+
108
+ ## 发布
109
+
110
+ - 注意:示例代码生效,但发布之后未生效。确认是否执行了编译!!!
111
+
112
+ - 编译组件:npm run build
113
+ - 命令:npm publish --access public
114
+ - 发布目录:
115
+ - src
116
+
117
+ ## 配置
118
+
119
+ ### 配置文件
120
+
121
+ - 本地配置文件:config/config.js
122
+
123
+ ### webpack 配置文件
124
+
125
+ - config/webpack.config.js
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@hzab/hierarchical-statistics",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "main": "src",
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 version patch && npm publish --access public",
10
+ "publish-minor": "npm version minor && npm publish --access public",
11
+ "publish-major": "npm version major && npm publish --access public",
12
+ "prepare": "husky install",
13
+ "mac-chmod": "chmod +x .husky && chmod +x .husky/pre-commit",
14
+ "docs": "typedoc"
15
+ },
16
+ "files": [
17
+ "src",
18
+ "CHANGELOG.md"
19
+ ],
20
+ "keywords": [],
21
+ "author": "CaiYansong",
22
+ "license": "ISC",
23
+ "devDependencies": {
24
+ "@hzab/permissions": "^1.0.0",
25
+ "@hzab/webpack-config": "^1.0.0",
26
+ "@types/react": "^17.0.62",
27
+ "@types/react-dom": "^17.0.20",
28
+ "antd": "^4.14.0",
29
+ "axios": "^1.4.0",
30
+ "eslint": "^8.30.0",
31
+ "less": "^4.1.3",
32
+ "mobx": "^6.7.0",
33
+ "mobx-react": "^7.6.0",
34
+ "react": "^17.0.2",
35
+ "react-dom": "^17.0.2",
36
+ "react-router-dom": "^6.14.1",
37
+ "typedoc": "^0.24.8",
38
+ "typescript": "^4.9.4",
39
+ "webpack": "5.75.0"
40
+ },
41
+ "directories": {
42
+ "src": "src"
43
+ },
44
+ "lint-staged": {
45
+ "**.{js,jsx,ts,tsx,css,scss,less,json,html}": [
46
+ "prettier --write"
47
+ ]
48
+ },
49
+ "dependencies": {
50
+ "@hzab/data-model": "^1.7.5",
51
+ "@hzab/headless-components": "^0.0.3",
52
+ "@hzab/list-render-mobile": "^1.1.7"
53
+ }
54
+ }
@@ -0,0 +1,28 @@
1
+ import styles from "../index.module.less";
2
+ const BreadcrumbUI = (props: any) => {
3
+ const { readcrumbStyle, separatorStyle, separator, breadcrumbData, terminal, handleBreadcrumbClick } = props;
4
+ return (
5
+ <div
6
+ className={`${styles["readcrumb-box"]} ${terminal !== "pc" ? styles["readcrumb-box-mobile"] : null}`}
7
+ style={readcrumbStyle}
8
+ >
9
+ {breadcrumbData.map((item, index) => (
10
+ <div className={`${styles["readcrumb-item"]}`} key={index}>
11
+ <span
12
+ className={styles["title"]}
13
+ style={{ color: breadcrumbData.length - 1 == index ? "#aeadad" : "#403ef5" }}
14
+ key={item?.value}
15
+ onClick={() => handleBreadcrumbClick(item, index)}
16
+ >
17
+ {item?.label}
18
+ </span>
19
+ <span className={styles["separator"]} style={separatorStyle}>
20
+ {breadcrumbData?.length - 1 != index ? separator : ""}
21
+ </span>
22
+ </div>
23
+ ))}
24
+ </div>
25
+ );
26
+ };
27
+
28
+ export default BreadcrumbUI;
@@ -0,0 +1,60 @@
1
+ import { ForwardedRef, forwardRef, useImperativeHandle, useRef } from "react";
2
+ import { ConfigProvider, Spin } from "antd";
3
+ import ListRender from "@hzab/list-render-mobile";
4
+ import { useInfiniteList } from "../../use-infinite-list";
5
+ import { InfiniteListProps, InfiniteListInstance } from "../../type";
6
+ import Breadcrumb from "../Breadcrumb";
7
+ import styles from "../index.module.less";
8
+ const InfiniteListUIMobile = (props: InfiniteListProps, ref: ForwardedRef<InfiniteListInstance>) => {
9
+ const {
10
+ states: {
11
+ readcrumbStyle,
12
+ separatorStyle,
13
+ separator,
14
+ listRenderProps,
15
+ style,
16
+ listModal,
17
+ breadcrumbData,
18
+ Slots,
19
+ loading,
20
+ listRef,
21
+ listStyle,
22
+ breadModel,
23
+ },
24
+ actions: { fetchBreadData, handleSelectId, handleNodeClick, handleBreadcrumbClick },
25
+ } = useInfiniteList({ ...props, terminal: "mobile" });
26
+ useImperativeHandle(ref, () => ({
27
+ handleNodeClick,
28
+ handleSelectId,
29
+ listRef: listRef?.current,
30
+ listModal,
31
+ breadModel,
32
+ fetchBreadData,
33
+ }));
34
+ return (
35
+ <div className={styles["contain"]} style={style}>
36
+ <Spin spinning={loading}>
37
+ {/* 面包屑导航 */}
38
+ <div className={styles["breadcrumb-box-main"]}>
39
+ <Breadcrumb
40
+ terminal="mobile"
41
+ readcrumbStyle={readcrumbStyle}
42
+ separatorStyle={separatorStyle}
43
+ separator={separator}
44
+ breadcrumbData={breadcrumbData}
45
+ handleBreadcrumbClick={handleBreadcrumbClick}
46
+ Slots={Slots}
47
+ ></Breadcrumb>
48
+ {Slots?.fixRenderCenter && <Slots.fixRenderCenter></Slots.fixRenderCenter>}
49
+ </div>
50
+
51
+ <div className={styles["list-box-main"]} style={listStyle}>
52
+ {/* 移动端 */}
53
+ <ListRender ref={listRef} model={listModal} {...listRenderProps} renderItem={Slots.renderItem} />
54
+ </div>
55
+ </Spin>
56
+ </div>
57
+ );
58
+ };
59
+
60
+ export default forwardRef<InfiniteListInstance, InfiniteListProps>(InfiniteListUIMobile);
@@ -0,0 +1,73 @@
1
+ import { ForwardedRef, forwardRef, useImperativeHandle, useRef } from "react";
2
+ import VirtualPagedList from "@hzab/headless-components/src/VirtualPagedList";
3
+ import { ConfigProvider, Spin, Empty, Button } from "antd";
4
+ import { useInfiniteList } from "../../use-infinite-list";
5
+ import Breadcrumb from "../Breadcrumb";
6
+ import { InfiniteListProps, InfiniteListInstance } from "../../type";
7
+ import styles from "../index.module.less";
8
+ const InfiniteListUIPc = (props: InfiniteListProps, ref: ForwardedRef<InfiniteListInstance>) => {
9
+ const {
10
+ states: {
11
+ listModal,
12
+ readcrumbStyle,
13
+ separatorStyle,
14
+ separator,
15
+ style,
16
+ selectedLoadIdNode,
17
+ breadcrumbData,
18
+ Slots,
19
+ loading,
20
+ listStyle,
21
+ listRef,
22
+ virtualPagedListProps,
23
+ },
24
+ actions: { handleSelectId, handleNodeClick, handleBreadcrumbClick },
25
+ } = useInfiniteList({ ...props, terminal: "pc" });
26
+
27
+ useImperativeHandle(ref, () => ({
28
+ handleNodeClick,
29
+ handleSelectId,
30
+ listRef: listRef?.current,
31
+ listModal,
32
+ }));
33
+
34
+ const loadData = async (pageNum) => {
35
+ listModal.query = { ...listModal.query, pageNum: pageNum };
36
+ const res = await listModal?.getList();
37
+ const list = res?.list ?? [];
38
+ return {
39
+ list,
40
+ isMore: res?.pagination?.total > (pageNum - 1) * listModal.query.pageSize + res?.list?.length ? true : false,
41
+ };
42
+ };
43
+ return (
44
+ <div className={styles["contain"]} style={style}>
45
+ <Spin spinning={loading}>
46
+ {/* 面包屑导航 */}
47
+ <div className={styles["breadcrumb-box-main"]}>
48
+ <Breadcrumb
49
+ terminal="pc"
50
+ readcrumbStyle={readcrumbStyle}
51
+ separatorStyle={separatorStyle}
52
+ separator={separator}
53
+ breadcrumbData={breadcrumbData}
54
+ handleBreadcrumbClick={handleBreadcrumbClick}
55
+ ></Breadcrumb>
56
+ {Slots?.fixRenderCenter && <Slots.fixRenderCenter></Slots.fixRenderCenter>}
57
+ </div>
58
+
59
+ <div className={styles["list-box-main"]} style={listStyle}>
60
+ <VirtualPagedList
61
+ key={selectedLoadIdNode}
62
+ ref={listRef}
63
+ {...virtualPagedListProps}
64
+ renderItem={Slots.renderItem}
65
+ loadMoreItems={loadData}
66
+ />
67
+ </div>
68
+ </Spin>
69
+ </div>
70
+ );
71
+ };
72
+
73
+ export default forwardRef<InfiniteListInstance, InfiniteListProps>(InfiniteListUIPc);
@@ -0,0 +1,30 @@
1
+ .contain {
2
+ -webkit-user-select: none; /* Safari */
3
+ -moz-user-select: none; /* Firefox */
4
+ -ms-user-select: none; /* IE/Edge */
5
+ user-select: none; /* Standard syntax */
6
+ }
7
+ .breadcrumb-box-main {
8
+ position: sticky;
9
+ top: 0px;
10
+ z-index: 9;
11
+ .readcrumb-box {
12
+ height: 40px;
13
+ width: 100%; /* 或者其他固定宽度 */
14
+ overflow: hidden; /* 隐藏水平滚动条 */
15
+ white-space: nowrap; /* 文字在一行内显示 */
16
+ overflow-x: auto;
17
+ display: flex;
18
+ align-items: center;
19
+
20
+ > div {
21
+ display: inline-block;
22
+ .separator {
23
+ margin: 0 4px;
24
+ }
25
+ }
26
+ }
27
+ .readcrumb-box-mobile::-webkit-scrollbar {
28
+ display: none; /* 隐藏滚动条(适用于 Chrome/Edge/Safari) */
29
+ }
30
+ }
package/src/index.tsx ADDED
File without changes
package/src/type.d.ts ADDED
@@ -0,0 +1,72 @@
1
+ export type ListItem = {
2
+ children?: ListItem[];
3
+ value: number;
4
+ label: string;
5
+ [key: string]: any;
6
+ };
7
+
8
+ export type FieldItem = {
9
+ /** 选中当前关联id */
10
+ value: string;
11
+ /** 选中当前关联名称 */
12
+ label: string;
13
+ };
14
+ export type PathItem = {
15
+ value: number;
16
+ label: string;
17
+ };
18
+ export type InitSelectItem = PathItem;
19
+ export type OnNodeBreadcrumbClickType = PathItem & { index: number };
20
+ export interface InfiniteListInstance {
21
+ /** 点击下级节点事件 */
22
+ handleNodeClick?: (item: ListItem) => void;
23
+ /** 选中事件 */
24
+ handleSelectId?: (item: ListItem) => void;
25
+ listRef?: any;
26
+ listModal?: any;
27
+ }
28
+ export interface InfiniteListProps {
29
+ /** 面包屑显示对应关联字段默认 value,label */
30
+ fieldBreadcrumb?: FieldItem;
31
+ /** 是否有首页,默认false */
32
+ showHome?: boolean;
33
+ /** 关联字段默认 value,label */
34
+ fieldList?: FieldItem;
35
+ /** 列表请求接口下一级关联字段 */
36
+ parentField?: string;
37
+ /** 面包屑首页名称 */
38
+ homeBreadcrumb?: string;
39
+ /** 自定义面包屑 和 自定义列表 */
40
+ Slots: {
41
+ /** 固定在头部 面包屑和列表之间插槽 */
42
+ fixRenderCenter?: any;
43
+ /** 自定义列表项 */
44
+ renderItem: any;
45
+ };
46
+ /** 面包屑分割符 */
47
+ separator?: string;
48
+ /** 组件外层样式 */
49
+ style?: object;
50
+ /** 面包屑分割符css 样式 */
51
+ separatorStyle?: object;
52
+ /** 面包屑外层 自定义css 样式 */
53
+ readcrumbStyle?: object;
54
+ /** 设备是pc,还是mobile */
55
+ terminal?: string;
56
+ /** 列表外层 css 样式 */
57
+ listStyle?: object;
58
+ /** list modal */
59
+ listModal: any;
60
+ /** bread modal */
61
+ breadModel?: any;
62
+ /** list-render-mobile配置 */
63
+ listRenderProps?: object;
64
+ /** virtualPagedList 配置 */
65
+ virtualPagedListProps?: any;
66
+ /** 选中点击事件 */
67
+ onNodeClick?: (node: ListItem) => void;
68
+ /** 选中id */
69
+ onNodeSelect?: (node: ListItem) => void;
70
+ /** 选中回显点击事件 */
71
+ onBreadcrumbClick?: (node: OnNodeBreadcrumbClickType) => void;
72
+ }
@@ -0,0 +1,142 @@
1
+ import { useState, useEffect, useRef } from "react";
2
+ import { ListItem, PathItem, InfiniteListProps } from "./type";
3
+
4
+ export function useInfiniteList(props: InfiniteListProps) {
5
+ const {
6
+ style,
7
+ listStyle,
8
+ separatorStyle,
9
+ readcrumbStyle,
10
+ listRenderProps,
11
+ showHome = true,
12
+ onNodeClick,
13
+ onNodeSelect,
14
+ onBreadcrumbClick,
15
+ parentField = "parentId",
16
+ homeBreadcrumb = "首页",
17
+ fieldBreadcrumb = {
18
+ value: "value",
19
+ label: "label",
20
+ },
21
+ fieldList = {
22
+ value: "value",
23
+ label: "label",
24
+ ...props?.fieldList,
25
+ },
26
+ Slots,
27
+ separator = "/",
28
+ listModal,
29
+ breadModel,
30
+ virtualPagedListProps,
31
+ } = props;
32
+ const [loading, setLoading] = useState(false);
33
+ const [selectedLoadIdNode, setSelectedLoadIdNode] = useState<number | null>(null);
34
+ const [selecteId, setSelectedId] = useState<number | null>();
35
+ const [breadcrumbData, setBreadcrumbData] = useState<PathItem[]>(
36
+ showHome ? [{ value: 0, label: homeBreadcrumb }] : [],
37
+ );
38
+ const listRef = useRef<any>(null);
39
+
40
+ /**
41
+ * 下一级请求函数
42
+ * @param {ListItem} item - 选中的节点数据
43
+ *
44
+ */
45
+ const handleNodeClick = (item: ListItem) => {
46
+ onNodeClick?.({
47
+ ...item,
48
+ value: item[fieldList.value],
49
+ label: item[fieldList.label],
50
+ });
51
+ listModal.query = { ...listModal.query, [parentField]: item[fieldList.value] };
52
+ setSelectedLoadIdNode(item[fieldList.value]);
53
+ setBreadcrumbData((pre) => [
54
+ ...pre,
55
+ {
56
+ value: item[fieldList.value],
57
+ label: item[fieldList.label],
58
+ },
59
+ ]);
60
+ listRef.current?.onSearch();
61
+ };
62
+
63
+ /**
64
+ * 选中节点id
65
+ * @param {ListItem} item - 选中的节点
66
+ *
67
+ */
68
+ const handleSelectId = (item: ListItem) => {
69
+ setSelectedId(item[fieldList.value]);
70
+ onNodeSelect?.({ ...item });
71
+ };
72
+
73
+ /**
74
+ * 处理面包屑点击事件
75
+ * @param {PathItem} item - 被点击的面包屑节点
76
+ * @param {number} index - 被点击的面包屑节点的索引
77
+ */
78
+ const handleBreadcrumbClick = (item: PathItem, index: number) => {
79
+ if (breadcrumbData.length - 1 == index) return;
80
+ onBreadcrumbClick?.({
81
+ ...item,
82
+ index: index,
83
+ });
84
+ listModal.query = { ...listModal.query, [parentField]: item.value };
85
+ setSelectedLoadIdNode(item.value);
86
+ setBreadcrumbData(breadcrumbData.slice(0, index + 1));
87
+ listRef.current?.onSearch();
88
+ };
89
+
90
+ /**
91
+ * 面包屑数据回显
92
+ *
93
+ */
94
+ const fetchBreadData = async () => {
95
+ setLoading(true);
96
+ try {
97
+ const res = await breadModel?.get();
98
+ if (!res && !Array.isArray(res)) return;
99
+ let relPath =
100
+ res[0]?.path?.map((v: any) => {
101
+ return {
102
+ ...v,
103
+ value: v[fieldBreadcrumb.value],
104
+ label: v[fieldBreadcrumb.label],
105
+ };
106
+ }) || [];
107
+ relPath && setBreadcrumbData(relPath);
108
+ } finally {
109
+ setLoading(false);
110
+ }
111
+ };
112
+
113
+ useEffect(() => {
114
+ fetchBreadData();
115
+ }, []);
116
+
117
+ return {
118
+ states: {
119
+ style,
120
+ listStyle,
121
+ separatorStyle,
122
+ readcrumbStyle,
123
+ selectedLoadIdNode,
124
+ breadcrumbData,
125
+ Slots,
126
+ listRenderProps,
127
+ separator,
128
+ loading,
129
+ selecteId,
130
+ listRef,
131
+ virtualPagedListProps,
132
+ listModal,
133
+ breadModel,
134
+ },
135
+ actions: {
136
+ handleSelectId,
137
+ handleNodeClick,
138
+ handleBreadcrumbClick,
139
+ fetchBreadData,
140
+ },
141
+ };
142
+ }