@zcrkey/js-utils 0.0.4 → 0.0.5

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.
@@ -0,0 +1,85 @@
1
+ /**
2
+ * title: sessionStorage
3
+ */
4
+
5
+ import { CrStorage } from '@zcrkey/js-utils';
6
+ import React, { useState } from 'react';
7
+
8
+ const LOGIN_USER = 'loginUser';
9
+
10
+ export default () => {
11
+ const [data, setData] = useState<any>();
12
+
13
+ const onSet = () => {
14
+ CrStorage.setSessionItem(LOGIN_USER, {
15
+ name: 'zcr',
16
+ sex: '男',
17
+ age: 18,
18
+ });
19
+ };
20
+
21
+ const onGet = () => {
22
+ setData(CrStorage.getSessionItem(LOGIN_USER));
23
+ };
24
+
25
+ const onClear = () => {
26
+ CrStorage.removeSessionItem(LOGIN_USER);
27
+ setData('');
28
+ };
29
+
30
+ const onAllClear = () => {
31
+ CrStorage.clearSession();
32
+ setData('');
33
+ };
34
+
35
+ return (
36
+ <>
37
+ <div className="flex flex-direction">
38
+ <div className="text-lg">CrUtil.setSessionItem</div>
39
+ <div>
40
+ <button
41
+ type="button"
42
+ className="cr-btn line-blue radius"
43
+ onClick={onSet}
44
+ >
45
+ 设置用户数据
46
+ </button>
47
+ </div>
48
+ <div className="text-lg">CrUtil.getSessionItem</div>
49
+ <div>
50
+ <button
51
+ type="button"
52
+ className="cr-btn line-blue radius"
53
+ onClick={onGet}
54
+ >
55
+ 获取用户数据
56
+ </button>
57
+ </div>
58
+ <div className="text-lg">CrUtil.removeSessionItem</div>
59
+ <div>
60
+ <button
61
+ type="button"
62
+ className="cr-btn line-blue radius"
63
+ onClick={onClear}
64
+ >
65
+ 清除用户数据
66
+ </button>
67
+ </div>
68
+ </div>
69
+ <div className="text-lg">CrUtil.clearSession</div>
70
+ <div>
71
+ <button
72
+ type="button"
73
+ className="cr-btn line-blue radius"
74
+ onClick={onAllClear}
75
+ >
76
+ 清除全部
77
+ </button>
78
+ </div>
79
+
80
+ <div className="divider"></div>
81
+
82
+ <p>用户数据:{JSON.stringify(data)}</p>
83
+ </>
84
+ );
85
+ };
@@ -0,0 +1,5 @@
1
+ # CrTreeUtil 树工具类
2
+
3
+ 树工具类
4
+
5
+ <code src="./index.tsx"></code>
@@ -0,0 +1,266 @@
1
+ /**
2
+ * title: 基本使用
3
+ */
4
+
5
+ import { CrTreeUtil } from '@zcrkey/js-utils';
6
+ import React, { useState } from 'react';
7
+
8
+ const listData = [
9
+ { id: '1', parentId: null, name: '节点 1' },
10
+ { id: '1-1', parentId: '1', name: '节点 1-1' },
11
+ { id: '1-1-1', parentId: '1-1', name: '节点 1-1-1' },
12
+ { id: '1-1-2', parentId: '1-1', name: '节点 1-1-2' },
13
+ { id: '1-2', parentId: '1', name: '节点 1-2' },
14
+ { id: '1-2-1', parentId: '1-2', name: '节点 1-2-1' },
15
+ { id: '1-2-1-1', parentId: '1-2-1', name: '节点 1-2-1-1' },
16
+
17
+ { id: '2', parentId: null, name: '节点 2' },
18
+ { id: '2-1', parentId: '2', name: '节点 2-1' },
19
+ { id: '2-1-1', parentId: '2-1', name: '节点 2-1-1' },
20
+ { id: '2-1-2', parentId: '2-1', name: '节点 2-1-2' },
21
+ { id: '2-2', parentId: '2', name: '节点 2-2' },
22
+
23
+ { id: '3', parentId: null, name: '节点 3' },
24
+ { id: '3-1', parentId: '3', name: '节点 3-1' },
25
+ { id: '3-1-1', parentId: '3-1', name: '节点 3-1-1' },
26
+ { id: '3-1-1-1', parentId: '3-1-1', name: '节点 3-1-1-1' },
27
+ { id: '3-2', parentId: '3', name: '节点 3-2' },
28
+ { id: '3-2-1', parentId: '3-2', name: '节点 3-2-1' },
29
+
30
+ { id: '4', parentId: null, name: '节点 4' },
31
+ ];
32
+
33
+ const treeData = [
34
+ {
35
+ id: '1',
36
+ parentId: null,
37
+ name: '节点 1',
38
+ children: [
39
+ {
40
+ id: '1-1',
41
+ parentId: '1',
42
+ name: '节点 1-1',
43
+ children: [
44
+ {
45
+ id: '1-1-1',
46
+ parentId: '1-1',
47
+ name: '节点 1-1-1',
48
+ },
49
+ {
50
+ id: '1-1-2',
51
+ parentId: '1-1',
52
+ name: '节点 1-1-2',
53
+ },
54
+ ],
55
+ },
56
+ {
57
+ id: '1-2',
58
+ parentId: '1',
59
+ name: '节点 1-2',
60
+ children: [
61
+ {
62
+ id: '1-2-1',
63
+ parentId: '1-2',
64
+ name: '节点 1-2-1',
65
+ children: [
66
+ {
67
+ id: '1-2-1-1',
68
+ parentId: '1-2-1',
69
+ name: '节点 1-2-1-1',
70
+ },
71
+ ],
72
+ },
73
+ ],
74
+ },
75
+ ],
76
+ },
77
+ {
78
+ id: '2',
79
+ parentId: null,
80
+ name: '节点 2',
81
+ children: [
82
+ {
83
+ id: '2-1',
84
+ parentId: '2',
85
+ name: '节点 2-1',
86
+ children: [
87
+ {
88
+ id: '2-1-1',
89
+ parentId: '2-1',
90
+ name: '节点 2-1-1',
91
+ },
92
+ {
93
+ id: '2-1-2',
94
+ parentId: '2-1',
95
+ name: '节点 2-1-2',
96
+ },
97
+ ],
98
+ },
99
+ {
100
+ id: '2-2',
101
+ parentId: '2',
102
+ name: '节点 2-2',
103
+ },
104
+ ],
105
+ },
106
+ {
107
+ id: '3',
108
+ parentId: null,
109
+ name: '节点 3',
110
+ children: [
111
+ {
112
+ id: '3-1',
113
+ parentId: '3',
114
+ name: '节点 3-1',
115
+ children: [
116
+ {
117
+ id: '3-1-1',
118
+ parentId: '3-1',
119
+ name: '节点 3-1-1',
120
+ children: [
121
+ {
122
+ id: '3-1-1-1',
123
+ parentId: '3-1-1',
124
+ name: '节点 3-1-1-1',
125
+ },
126
+ ],
127
+ },
128
+ ],
129
+ },
130
+ {
131
+ id: '3-2',
132
+ parentId: '3',
133
+ name: '节点 3-2',
134
+ children: [
135
+ {
136
+ id: '3-2-1',
137
+ parentId: '3-2',
138
+ name: '节点 3-2-1',
139
+ },
140
+ ],
141
+ },
142
+ ],
143
+ },
144
+ {
145
+ id: '4',
146
+ parentId: null,
147
+ name: '节点 4',
148
+ },
149
+ ];
150
+
151
+ export default () => {
152
+ const [result, setResult] = useState<any>({});
153
+
154
+ const listToTree = () => {
155
+ let res = CrTreeUtil.listToTree(listData, {
156
+ isDeepCopy: true,
157
+ });
158
+ setResult({
159
+ ...result,
160
+ listToTree: res,
161
+ });
162
+ };
163
+
164
+ const treeToList = () => {
165
+ let res = CrTreeUtil.treeToList(treeData, {
166
+ isDeepCopy: true,
167
+ });
168
+ setResult({
169
+ ...result,
170
+ treeToList: res,
171
+ });
172
+ };
173
+
174
+ const getFlatParentDatas = () => {
175
+ let res = CrTreeUtil.getFlatParentDatas(listData, '3-1-1-1', {
176
+ valueField: 'id',
177
+ getData: (item: any) => item,
178
+ });
179
+ setResult({
180
+ ...result,
181
+ getFlatParentDatas: res,
182
+ });
183
+ };
184
+
185
+ return (
186
+ <>
187
+ <div className="flex flex-direction">
188
+ <div>列表数据转树型数据</div>
189
+ <div className="text-lg">CrTreeUtil.listToTree</div>
190
+ <div>
191
+ <button
192
+ type="button"
193
+ className="cr-btn line-blue radius"
194
+ onClick={listToTree}
195
+ >
196
+ 调用
197
+ </button>
198
+ <div>
199
+ <div style={{ display: 'inline-grid', width: '50%' }}>
200
+ <pre style={{ marginBottom: '0px' }}>
201
+ {JSON.stringify(listData, null, 2)}
202
+ </pre>
203
+ </div>
204
+ <div style={{ display: 'inline-grid', width: '50%' }}>
205
+ <pre style={{ marginBottom: '0px' }}>
206
+ {JSON.stringify(result.listToTree, null, 2)}
207
+ </pre>
208
+ </div>
209
+ </div>
210
+ </div>
211
+ </div>
212
+ <div className="divider"></div>
213
+ <div className="flex flex-direction">
214
+ <div>树型数据转列表数据</div>
215
+ <div className="text-lg">CrTreeUtil.treeToList</div>
216
+ <div>
217
+ <button
218
+ type="button"
219
+ className="cr-btn line-blue radius"
220
+ onClick={treeToList}
221
+ >
222
+ 调用
223
+ </button>
224
+ <div>
225
+ <div style={{ display: 'inline-grid', width: '50%' }}>
226
+ <pre style={{ marginBottom: '0px' }}>
227
+ {JSON.stringify(treeData, null, 2)}
228
+ </pre>
229
+ </div>
230
+ <div style={{ display: 'inline-grid', width: '50%' }}>
231
+ <pre style={{ marginBottom: '0px' }}>
232
+ {JSON.stringify(result.treeToList, null, 2)}
233
+ </pre>
234
+ </div>
235
+ </div>
236
+ </div>
237
+ </div>
238
+ <div className="divider"></div>
239
+ <div className="flex flex-direction">
240
+ <div>获取扁平化父级数据</div>
241
+ <div className="text-lg">CrTreeUtil.getFlatParentDatas</div>
242
+ <div>
243
+ <button
244
+ type="button"
245
+ className="cr-btn line-blue radius"
246
+ onClick={getFlatParentDatas}
247
+ >
248
+ 调用
249
+ </button>
250
+ <div>
251
+ <div style={{ display: 'inline-grid', width: '50%' }}>
252
+ <pre style={{ marginBottom: '0px' }}>
253
+ 值:'3-1-1-1' 数据源:{JSON.stringify(listData, null, 2)}
254
+ </pre>
255
+ </div>
256
+ <div style={{ display: 'inline-grid', width: '50%' }}>
257
+ <pre style={{ marginBottom: '0px' }}>
258
+ {JSON.stringify(result.getFlatParentDatas, null, 2)}
259
+ </pre>
260
+ </div>
261
+ </div>
262
+ </div>
263
+ </div>
264
+ </>
265
+ );
266
+ };
@@ -0,0 +1,6 @@
1
+ # CrUtil 实用函数
2
+
3
+ 实用函数
4
+
5
+ <code src="./index.tsx"></code>
6
+ <code src="./is.tsx"></code>