kn-hooks 0.0.8 → 0.0.10
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 +28 -19
- package/readme.md +40 -177
- package/src/index.js +16 -1
- package/src/useClipboard/index.js +30 -7
- package/src/useCounter/index.js +34 -0
- package/src/useDictionary/index.js +102 -31
- package/src/usePagination/index.js +264 -0
- package/src/usePaginationWithForm/index.js +86 -0
- package/src/useSwitch/index.js +23 -8
- package/test/useCounter/index.jsx +15 -0
- package/test/useDictionary/index.jsx +2 -2
- package/test/usePagination/index.jsx +119 -0
- package/test/usePaginationWithForm/index.jsx +101 -0
- package/test/useSwitch/index.jsx +1 -0
- package/src/.DS_Store +0 -0
- package/src/useAntdFormTableSearch/index.js +0 -165
- package/test/.DS_Store +0 -0
package/package.json
CHANGED
|
@@ -1,31 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kn-hooks",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"dev": "cross-env env_api=dev env_package=dev webpack-dev-server --progress",
|
|
6
6
|
"build": "cross-env env_api=prod env_package=prod webpack --config webpack.config.js",
|
|
7
7
|
"build:dev": "cross-env env_api=dev env_package=dev webpack",
|
|
8
|
-
"report": "cross-env env_api=prod env_package=prod report=true webpack --config webpack.config.js"
|
|
8
|
+
"report": "cross-env env_api=prod env_package=prod report=true webpack --config webpack.test.config.js",
|
|
9
|
+
"doc": "./node_modules/.bin/jsdoc src/**/*.js --configure .jsdoc.json --verbose",
|
|
10
|
+
"md": "./node_modules/.bin/jsdoc2md --plugin ./dmd/lib/index.js --files src/useRollingPagination/*.js src/useSwitch/*.js > md/api.md"
|
|
9
11
|
},
|
|
10
12
|
"main": "src/index.js",
|
|
11
13
|
"dependencies": {
|
|
12
14
|
"clipboard": "2.0.8"
|
|
13
15
|
},
|
|
14
16
|
"devDependencies": {
|
|
15
|
-
"@babel/preset-react": "7.12.13",
|
|
16
|
-
"antd": "4.16.13",
|
|
17
|
-
"css-loader": "3.4.2",
|
|
18
|
-
"html-webpack-plugin": "4.0.3",
|
|
19
|
-
"mini-css-extract-plugin": "0.9.0",
|
|
20
|
-
"react": "16.13.0",
|
|
21
|
-
"react-dom": "16.13.0",
|
|
22
|
-
"babel-loader": "8.2.2",
|
|
23
|
-
"cross-env": "7.0.3",
|
|
24
|
-
"terser-webpack-plugin": "4.2.3",
|
|
25
|
-
"webpack": "4.46.0",
|
|
26
|
-
"webpack-bundle-analyzer": "3.6.1",
|
|
27
|
-
"webpack-cli": "3.3.11",
|
|
28
|
-
"webpack-dev-server": "3.11.2",
|
|
29
17
|
"@babel/core": "7.13.10",
|
|
30
18
|
"@babel/plugin-proposal-class-properties": "7.8.3",
|
|
31
19
|
"@babel/plugin-proposal-decorators": "7.8.3",
|
|
@@ -36,13 +24,34 @@
|
|
|
36
24
|
"@babel/plugin-transform-react-jsx": "^7.14.3",
|
|
37
25
|
"@babel/plugin-transform-runtime": "7.13.10",
|
|
38
26
|
"@babel/preset-env": "7.13.10",
|
|
27
|
+
"@babel/preset-react": "7.12.13",
|
|
39
28
|
"@babel/runtime-corejs3": "7.13.10",
|
|
29
|
+
"@rollup/plugin-babel": "^5.3.0",
|
|
30
|
+
"@rollup/plugin-commonjs": "^19.0.0",
|
|
31
|
+
"@rollup/plugin-node-resolve": "^13.0.0",
|
|
32
|
+
"antd": "4.16.13",
|
|
33
|
+
"babel-loader": "8.2.2",
|
|
34
|
+
"babel-plugin-import": "^1.13.3",
|
|
35
|
+
"cross-env": "7.0.3",
|
|
36
|
+
"css-loader": "3.4.2",
|
|
37
|
+
"dmd-bitbucket": "^0.1.10",
|
|
38
|
+
"dmd-plugin-example": "^0.1.0",
|
|
39
|
+
"docdash": "^2.0.1",
|
|
40
|
+
"html-webpack-plugin": "4.0.3",
|
|
41
|
+
"jsdoc": "^4.0.2",
|
|
42
|
+
"jsdoc-to-markdown": "^8.0.0",
|
|
43
|
+
"mini-css-extract-plugin": "0.9.0",
|
|
44
|
+
"react": "16.13.0",
|
|
45
|
+
"react-dom": "16.13.0",
|
|
40
46
|
"rollup": "^2.50.5",
|
|
41
47
|
"rollup-plugin-jsx": "^1.0.3",
|
|
42
48
|
"rollup-plugin-terser": "^7.0.2",
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
49
|
+
"taffydb": "^2.7.3",
|
|
50
|
+
"terser-webpack-plugin": "4.2.3",
|
|
51
|
+
"webpack": "4.46.0",
|
|
52
|
+
"webpack-bundle-analyzer": "3.6.1",
|
|
53
|
+
"webpack-cli": "3.3.11",
|
|
54
|
+
"webpack-dev-server": "3.11.2"
|
|
46
55
|
},
|
|
47
56
|
"files": [
|
|
48
57
|
"test",
|
package/readme.md
CHANGED
|
@@ -2,9 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
## 目录
|
|
4
4
|
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
|
|
5
|
+
* [使用方法](#start)
|
|
6
|
+
* [按需打包](#package)
|
|
7
|
+
|
|
8
|
+
* API
|
|
9
|
+
* [useDictionary - 字典管理及下拉选项框渲染](md/useDictionary.md)
|
|
10
|
+
* [useSwitch - 开关控制](md/useSwitch.md)
|
|
11
|
+
* [useClipboard - 剪贴板](md/useClipboard.md)
|
|
12
|
+
* [useCounter - 计数器](md/useCounter.md)
|
|
13
|
+
* [usePagination - 分页管理](md/usePagination.md)
|
|
14
|
+
* [usePaginationWithForm - 支持Antd-From的分页管理](md/usePaginationWithForm.md)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
<a id='start'></a>
|
|
8
19
|
|
|
9
20
|
## 使用方法
|
|
10
21
|
|
|
@@ -23,181 +34,33 @@ const Index=()=>{
|
|
|
23
34
|
```
|
|
24
35
|
|
|
25
36
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
控制开关
|
|
29
|
-
|
|
30
|
-
#### Demo
|
|
31
|
-
[](https://codesandbox.io/s/useswitch-qd7dr?file=/index.js)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
#### API
|
|
35
|
-
|
|
36
|
-
```typescript
|
|
37
|
-
const result:Result = useSwitch(switch?:boolean);
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
#### Params
|
|
41
|
-
|
|
42
|
-
参数|说明|类型|默认值
|
|
43
|
-
--|:--:|:--:|--:
|
|
44
|
-
?switch|默认为打开还是关闭状态|boolean|false
|
|
45
|
-
|
|
46
|
-
#### Result: Object
|
|
47
|
-
|
|
48
|
-
参数|说明|类型
|
|
49
|
-
--|:--:|--:
|
|
50
|
-
state|当前开关状态|boolean
|
|
51
|
-
count|当前开关计数器|number
|
|
52
|
-
toggle|切换开关|()=>void
|
|
53
|
-
open|打开开关|(force?: boolean)=>void
|
|
54
|
-
close|关闭开关|(force?: boolean)=>void
|
|
55
|
-
|
|
56
|
-
> 当使用open和close控制开关时为成对计数模式
|
|
57
|
-
> 每次open都会增加`开计数器`,而每次close会减少`开计数器`
|
|
58
|
-
> 当`开计数器`为0时才会真正关闭开关
|
|
59
|
-
|
|
60
|
-
##### open: (force?: boolean)=>void
|
|
61
|
-
|
|
62
|
-
参数|说明|类型|默认值
|
|
63
|
-
--|:--:|:--:|--:
|
|
64
|
-
?force|是否强制切换到打开状态|boolean|false
|
|
65
|
-
|
|
66
|
-
##### close: (force?: boolean)=>void
|
|
67
|
-
|
|
68
|
-
参数|说明|类型|默认值
|
|
69
|
-
--|:--:|:--:|--:
|
|
70
|
-
?force|是否强制切换到关闭状态|boolean|false
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
## @useClipboard
|
|
74
|
-
|
|
75
|
-
将字符串复制到剪贴板
|
|
76
|
-
|
|
77
|
-
#### Demo
|
|
78
|
-
|
|
79
|
-
[](https://codesandbox.io/s/useclipboard-b4ryc?file=/index.js)
|
|
37
|
+
<a id='package'></a>
|
|
80
38
|
|
|
39
|
+
## 按需打包
|
|
81
40
|
|
|
82
|
-
|
|
41
|
+
> webpack4及以上版本无需下面配置
|
|
83
42
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
## @useDictionary
|
|
104
|
-
|
|
105
|
-
字典管理
|
|
106
|
-
|
|
107
|
-
#### DEMO
|
|
108
|
-
|
|
109
|
-
[](https://codesandbox.io/s/usedictionary-j7cw8?file=/index.js)
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
#### API
|
|
113
|
-
|
|
114
|
-
```typescript
|
|
115
|
-
const result:Result = useDictionary({
|
|
116
|
-
api:()=>Promise,
|
|
117
|
-
});
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
#### Params
|
|
121
|
-
|
|
122
|
-
参数|说明|类型|默认值
|
|
123
|
-
--|:--:|:--:|--:
|
|
124
|
-
api|获取字典数据的接口函数|()=>Promise|无
|
|
125
|
-
?beforeApi|调用接口前的钩子,用于处理传参|(params)=>any|无
|
|
126
|
-
?afterApi|调用接口后的钩子,用于处理返回结果|(params)=>TypeItem[]|无
|
|
127
|
-
?SelectOption|指定<Select.Option>对象|any|无
|
|
128
|
-
?RadioOption|指定<Radio.Button>对象|any|无
|
|
129
|
-
?idKey|字典项目中id的键值名|string|id
|
|
130
|
-
?nameKey|字典项目中name的键值名|string|name
|
|
131
|
-
?labelKey|字典项目中label的键值名|string|label
|
|
132
|
-
|
|
133
|
-
#### TypeItem-字典项数据结构
|
|
134
|
-
|
|
135
|
-
参数|说明|类型|默认值
|
|
136
|
-
--|:--:|:--:|--:
|
|
137
|
-
id|数据唯一id|string|无
|
|
138
|
-
name|数据唯一id对应的别名|string|无
|
|
139
|
-
label|展示给用户看的文字|string|无
|
|
140
|
-
|
|
141
|
-
#### Result: Object
|
|
142
|
-
|
|
143
|
-
参数|说明|类型
|
|
144
|
-
--|:--:|--:
|
|
145
|
-
types|字典项的值|TypeItem[]
|
|
146
|
-
selectOptions|供渲染<Select>的列表,Antd下用|any
|
|
147
|
-
radioOptions|供渲染<Radio>的列表,Antd下用|any
|
|
148
|
-
getId|搜索字典项中,label或name匹配labelOrName的项目,返回其id的值|(labelOrName:string)=>string
|
|
149
|
-
getName|搜索字典项中,id匹配id的项目,返回其name的值|(id:string)=>string
|
|
150
|
-
getLabel|搜索字典项中,id匹配id的项目,返回其label的值|(id:string)=>string
|
|
151
|
-
reload|重新调用字典接口刷新字典列表|()=>void
|
|
152
|
-
|
|
153
|
-
#### useDictionary.SET({SelectOption,RadioOption})=>void
|
|
154
|
-
全局指定渲染selectbox和radio的组件
|
|
155
|
-
|
|
156
|
-
参数|说明|类型
|
|
157
|
-
--|:--:|--:
|
|
158
|
-
SelectOption|对应Antd里 Select.Option|antd组件
|
|
159
|
-
RadioOption|对应Antd里 Radio.Button|antd组件
|
|
160
|
-
|
|
161
|
-
#### Example
|
|
162
|
-
|
|
163
|
-
```typescript
|
|
164
|
-
import {Select,Radio} from 'antd';
|
|
165
|
-
import useDictionary from '@/useDictionary';
|
|
166
|
-
useDictionary.SET({SelectOption:Select.Option,RadioOption:Radio.Button});
|
|
167
|
-
|
|
168
|
-
const Index=()=>{
|
|
169
|
-
|
|
170
|
-
const emCity = useDictionary({
|
|
171
|
-
api:()=>{
|
|
172
|
-
return new Promise(resolve=>{
|
|
173
|
-
resolve({
|
|
174
|
-
code:0,
|
|
175
|
-
data:{
|
|
176
|
-
body:[
|
|
177
|
-
{id:1,name:'shanghai',label:'上海'},
|
|
178
|
-
{id:2,name:'beijing',label:'北京'},
|
|
179
|
-
{id:3,name:'guangzhou',label:'广州'}
|
|
43
|
+
webpack增加`bable-plugin-import`插件,给babel plugins增加配置
|
|
44
|
+
```javascript
|
|
45
|
+
// rules: [
|
|
46
|
+
// {
|
|
47
|
+
// test: /\.js[x]?$/,
|
|
48
|
+
// exclude: /node_modules/,
|
|
49
|
+
// use: {
|
|
50
|
+
// loader:'babel-loader',
|
|
51
|
+
// options:{
|
|
52
|
+
// plugins: [
|
|
53
|
+
[
|
|
54
|
+
import, {
|
|
55
|
+
libraryName: "kn-hooks" ,
|
|
56
|
+
camel2DashComponentName:false,
|
|
57
|
+
libraryDirectory:"src",
|
|
58
|
+
}
|
|
180
59
|
]
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
<section >
|
|
189
|
-
<Select defaultValue={1}>
|
|
190
|
-
{
|
|
191
|
-
emCity.selectOptions
|
|
192
|
-
}
|
|
193
|
-
</Select>
|
|
194
|
-
<p>分割线</p>
|
|
195
|
-
<Radio.Group defaultValue={1}>
|
|
196
|
-
{
|
|
197
|
-
emCity.radioOptions
|
|
198
|
-
}
|
|
199
|
-
</Radio.Group>
|
|
200
|
-
</section>
|
|
201
|
-
)
|
|
202
|
-
}
|
|
203
|
-
```
|
|
60
|
+
// ]
|
|
61
|
+
// }
|
|
62
|
+
// }
|
|
63
|
+
// }
|
|
64
|
+
// ]
|
|
65
|
+
|
|
66
|
+
```
|
package/src/index.js
CHANGED
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
import useClipboard from './useClipboard/index.js';
|
|
2
2
|
import useSwitch from './useSwitch/index.js';
|
|
3
3
|
import useDictionary from './useDictionary/index.js';
|
|
4
|
+
import useCounter from './useCounter/index.js';
|
|
5
|
+
import usePagination from './usePagination/index.js';
|
|
6
|
+
import usePaginationWithForm from './usePaginationWithForm/index.js';
|
|
7
|
+
|
|
8
|
+
|
|
4
9
|
export {
|
|
5
10
|
useClipboard,
|
|
6
11
|
useSwitch,
|
|
7
|
-
useDictionary
|
|
12
|
+
useDictionary,
|
|
13
|
+
useCounter,
|
|
14
|
+
usePagination,
|
|
15
|
+
usePaginationWithForm
|
|
8
16
|
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
// export {default as useClipboard} from './useClipboard';
|
|
20
|
+
// export {default as useSwitch} from './useSwitch';
|
|
21
|
+
// export {default as useRefSwitch} from './useRefSwitch';
|
|
22
|
+
// export {default as useDictionary} from './useDictionary';
|
|
23
|
+
|
|
@@ -1,17 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module useClipboard
|
|
3
|
+
*/
|
|
1
4
|
import { useState, useMemo, useEffect, useRef } from 'react';
|
|
2
5
|
import ClipboardJS from 'clipboard';
|
|
3
6
|
|
|
7
|
+
|
|
4
8
|
/**
|
|
5
9
|
* 剪贴板工具
|
|
6
|
-
* props:{
|
|
7
|
-
* onSuccess?: Function;//复制成功后的通用回调
|
|
8
|
-
* }
|
|
9
10
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* )=>void
|
|
11
|
+
* [Demo - CodeSandBox]{@link https://codesandbox.io/s/useclipboard-b4ryc?file=/index.js}
|
|
12
|
+
*
|
|
13
|
+
* @param {Object} [props] -
|
|
14
|
+
* @param {callback} [props.onSuccess] - 成功复制到剪贴板后的回调 ()=>void
|
|
15
|
+
* @return {UseClipboardResult}
|
|
14
16
|
*
|
|
17
|
+
* @example
|
|
18
|
+
const clipboard= useClipboard();
|
|
19
|
+
const onCopy=()=>{
|
|
20
|
+
clipboard.copy('text',()=>{console.log('success')})
|
|
21
|
+
}
|
|
15
22
|
*/
|
|
16
23
|
const useClipboard = (props) => {
|
|
17
24
|
const { onSuccess } = props;
|
|
@@ -77,4 +84,20 @@ const useClipboard = (props) => {
|
|
|
77
84
|
return actions;
|
|
78
85
|
};
|
|
79
86
|
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 复制函数
|
|
90
|
+
* @typedef {callback} FunCopy
|
|
91
|
+
* @property {string} text - 需要被复制的字符串
|
|
92
|
+
* @property {callback} onCurSuccess - 复制成功的回调 ()=>void
|
|
93
|
+
*/
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* UseClipboardResult返回的hook
|
|
98
|
+
* @typedef {Object} UseClipboardResult
|
|
99
|
+
* @property {FunCopy} copy - 触发复制字符串的函数
|
|
100
|
+
*
|
|
101
|
+
*/
|
|
102
|
+
|
|
80
103
|
export default useClipboard;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module useCounter
|
|
3
|
+
*/
|
|
4
|
+
import { useState, useMemo } from 'react';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 加法计数器
|
|
9
|
+
* @return {UseCounterResult}
|
|
10
|
+
* @example
|
|
11
|
+
const counter= useCounter();
|
|
12
|
+
const onAdd=()=>{counter.addCount()}
|
|
13
|
+
return <span>当前计数器:{counter.count}</span>
|
|
14
|
+
*/
|
|
15
|
+
const useCounter = () => {
|
|
16
|
+
const [count,setCount] = useState(1);
|
|
17
|
+
const action = useMemo(()=>{
|
|
18
|
+
return {
|
|
19
|
+
count,
|
|
20
|
+
addCount:()=>{setCount(v=>v+1)}
|
|
21
|
+
}
|
|
22
|
+
},[count,setCount]);
|
|
23
|
+
return action;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @typedef UseCounterResult
|
|
28
|
+
* @property {Object} req
|
|
29
|
+
* @property {number} req.count - 当前计数器的值
|
|
30
|
+
* @property {function} req.addCount - 计数器的值+1。 ()=>void
|
|
31
|
+
*
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
export default useCounter;
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module useDictionary
|
|
3
|
+
*/
|
|
1
4
|
import React,{ useState, useMemo, useEffect } from 'react';
|
|
2
5
|
|
|
3
6
|
const DEFAULT_CONFIG={
|
|
@@ -5,43 +8,67 @@ const DEFAULT_CONFIG={
|
|
|
5
8
|
RadioOption:null
|
|
6
9
|
};
|
|
7
10
|
|
|
8
|
-
|
|
9
|
-
|
|
10
11
|
/**
|
|
11
12
|
* 字典管理
|
|
12
|
-
* props:{
|
|
13
|
-
* api:async ()=>any, 调用字段查询的接口函数,默认返回的数据格式为{ code:0 ,data:{body:[{label:'',id:'',name:''},]}}
|
|
14
|
-
* beforeApi?: (params:any)=>any , 调用接口前的钩子,用于处理接口的参数
|
|
15
|
-
* afterApi?: (response:any)=>any[], 接口调用后的钩子,用于处理接口返回的值
|
|
16
|
-
* SelectOption?: any, 指定<Select.Option>对象是谁
|
|
17
|
-
* RadioOption?: any, 指定<Radio.Button>对象是谁
|
|
18
|
-
* idKey?: string,//字段id的key键值
|
|
19
|
-
* nameKey?: string,//字段name的key键值
|
|
20
|
-
* labelKey?: string,//字段label的key键值
|
|
21
|
-
* }
|
|
22
|
-
* 字典数据的结构为
|
|
23
|
-
* {
|
|
24
|
-
* id:'',数据唯一id
|
|
25
|
-
* name:'',数据唯一id对应的别名
|
|
26
|
-
* label:'',展示给用户看的文字
|
|
27
|
-
* }
|
|
28
13
|
*
|
|
14
|
+
* [Demo - CodeSandBox]{@link https://codesandbox.io/s/usedictionary-j7cw8?file=/index.js}
|
|
29
15
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* }
|
|
16
|
+
* @param {Object} props
|
|
17
|
+
* @param {Api} props.api - 用于获取字典列表的接口
|
|
18
|
+
* @param {string} [props.idKey='id'] - 字段id的key键值
|
|
19
|
+
* @param {string} [props.nameKey='name'] - 字段name的key键值
|
|
20
|
+
* @param {string} [props.labelKey='label'] - 字段label的key键值
|
|
21
|
+
* @param {ReactDom} [props.SelectOption] - 指定\<Select.Option\>对象是谁
|
|
22
|
+
* @param {ReactDom} [props.RadioOption] - 指定\<Radio.Button\>对象是谁
|
|
23
|
+
* @param {callback} [props.beforeApi] - (request:object)=>object 接口调用前的参数拦截器
|
|
24
|
+
* @param {callback} [props.afterApi] - (reponse:object)=>object[] 接口调用后的拦截器
|
|
39
25
|
*
|
|
40
|
-
* useDictionary.SET:({SelectOption,RadioOption})=>void,全局设置SelectOption和RadioOption
|
|
41
26
|
*
|
|
27
|
+
* @return {UseDictionaryResult}
|
|
42
28
|
*
|
|
29
|
+
* @example
|
|
30
|
+
import {Select,Radio} from 'antd';
|
|
31
|
+
import useDictionary from '@/useDictionary';
|
|
32
|
+
useDictionary.SET({SelectOption:Select.Option,RadioOption:Radio.Button});
|
|
33
|
+
|
|
34
|
+
const Index=()=>{
|
|
35
|
+
|
|
36
|
+
const emCity = useDictionary({
|
|
37
|
+
api:()=>{
|
|
38
|
+
return new Promise(resolve=>{
|
|
39
|
+
resolve({
|
|
40
|
+
code:0,
|
|
41
|
+
data:{
|
|
42
|
+
body:[
|
|
43
|
+
{id:1,name:'shanghai',label:'上海'},
|
|
44
|
+
{id:2,name:'beijing',label:'北京'},
|
|
45
|
+
{id:3,name:'guangzhou',label:'广州'}
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<section >
|
|
55
|
+
<Select defaultValue={1}>
|
|
56
|
+
{
|
|
57
|
+
emCity.selectOptions
|
|
58
|
+
}
|
|
59
|
+
</Select>
|
|
60
|
+
<p>分割线</p>
|
|
61
|
+
<Radio.Group defaultValue={1}>
|
|
62
|
+
{
|
|
63
|
+
emCity.radioOptions
|
|
64
|
+
}
|
|
65
|
+
</Radio.Group>
|
|
66
|
+
</section>
|
|
67
|
+
)
|
|
68
|
+
}
|
|
43
69
|
*/
|
|
44
70
|
const useDictionary=(props)=>{
|
|
71
|
+
|
|
45
72
|
const {
|
|
46
73
|
api,
|
|
47
74
|
idKey = 'id',
|
|
@@ -148,8 +175,52 @@ const useDictionary=(props)=>{
|
|
|
148
175
|
return actions;
|
|
149
176
|
|
|
150
177
|
}
|
|
151
|
-
|
|
178
|
+
|
|
179
|
+
export const SetConfig = ({SelectOption,RadioOption})=>{
|
|
152
180
|
if(SelectOption)DEFAULT_CONFIG.SelectOption = SelectOption;
|
|
153
181
|
if(RadioOption)DEFAULT_CONFIG.RadioOption = RadioOption;
|
|
154
|
-
}
|
|
155
|
-
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* @typedef Api
|
|
188
|
+
* @property {Object} params - 调用接口用到的参数
|
|
189
|
+
* @returns {Object}
|
|
190
|
+
*/
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @typedef DictionaryItem
|
|
196
|
+
* 字典数据的结构
|
|
197
|
+
* @property {string} id - 数据唯一ID
|
|
198
|
+
* @property {string} name - 数据唯一id对应的别名
|
|
199
|
+
* @property {string} label - 展示给用户看的文字
|
|
200
|
+
*
|
|
201
|
+
*/
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* @typedef UseDictionaryResult
|
|
205
|
+
* @property {DictionaryItem[]} types - 字典数据列表
|
|
206
|
+
* @property {ReactDOM[]} selectOptions - 供Antd渲染\<Select\>的列表
|
|
207
|
+
* @property {ReactDOM[]} radioOptions - 供Antd渲染\<Radio\>的列表
|
|
208
|
+
* @property {function} getId - (labelOrName:string)=>string,搜索字典项中,label或name匹配labelOrName的项目,返回其id的值
|
|
209
|
+
* @property {function} getName - (id:string)=>string,搜索字典项中,id匹配id的项目,返回其name的值
|
|
210
|
+
* @property {function} getLabel - (id:string)=>string,搜索字典项中,id匹配id的项目,返回其label的值
|
|
211
|
+
* @property {function} reload - ()=>void,重新调用字典接口刷新字典列表
|
|
212
|
+
*
|
|
213
|
+
*/
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @function SET
|
|
217
|
+
* @description 全局设置SelectOption和RadioOption
|
|
218
|
+
*
|
|
219
|
+
* @param {Object} params
|
|
220
|
+
* @param {ReactDom} params.SelectOption - Antd的SelectOption组件
|
|
221
|
+
* @param {ReactDom} params.RadioOption - Antd的SelectOption组件
|
|
222
|
+
* @returns {void}
|
|
223
|
+
*/
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
export default useDictionary
|