kn-hooks 0.0.12 → 0.0.14

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.
@@ -2,12 +2,66 @@
2
2
  ## <span style="display:inline-flex;align-items:center;justify-content:center;width:34px;height:34px;border-radius:4px;color:rgb(71, 128, 227);background:#ede6e6;">G</span> useDictionary
3
3
 
4
4
  * [ useDictionary](#module_usedictionary)
5
- * [ useDictionary(props)](#module_usedictionary__usedictionary) ⇒ <code>UseDictionaryResult</code>
6
- * [ SET(params)](#module_usedictionary__set) ⇒ <code>void</code>
7
- * [ Api](#module_usedictionary__api) ⇒ <code>Object</code>
8
- * [ DictionaryItem](#module_usedictionary__dictionaryitem)
9
- * [ UseDictionaryResult](#module_usedictionary__usedictionaryresult)
5
+ * _static_
6
+ * [ SetConfig(params)](#module_usedictionary_setconfig) ⇒ <code>void</code>
7
+ * [ createDictionary(options)](#module_usedictionary_createdictionary) ⇒ <code>callback</code>
8
+ * _inner_
9
+ * [ useDictionary(props)](#module_usedictionary__usedictionary) ⇒ <code>UseDictionaryResult</code>
10
+ * [ Api](#module_usedictionary__api) ⇒ <code>Object</code>
11
+ * [ DictionaryItem](#module_usedictionary__dictionaryitem)
12
+ * [ UseDictionaryResult](#module_usedictionary__usedictionaryresult)
13
+
14
+ <a id="module_usedictionary_setconfig"></a>
15
+ ### <span style="display:inline-flex;align-items:center;justify-content:center;width:34px;height:34px;border-radius:4px;color:rgb(71, 128, 227);background:#ede6e6;">M</span> SetConfig(params) ⇒ <code>void</code>
16
+ 全局设置SelectOption和RadioOption
17
+
18
+
19
+ | Param | Type | Description |
20
+ | --- | --- | --- |
21
+ | params | <code>Object</code> | |
22
+ | params.SelectOption | <code>ReactDom</code> | Antd的SelectOption组件 |
23
+ | params.RadioOption | <code>ReactDom</code> | Antd的SelectOption组件 |
24
+
25
+ <a id="module_usedictionary_createdictionary"></a>
26
+ ### <span style="display:inline-flex;align-items:center;justify-content:center;width:34px;height:34px;border-radius:4px;color:rgb(71, 128, 227);background:#ede6e6;">M</span> createDictionary(options) ⇒ <code>callback</code>
27
+ 创建字典hooks工具
28
+
29
+
30
+ | Param | Type | Default | Description |
31
+ | --- | --- | --- | --- |
32
+ | options | <code>Object</code> | | |
33
+ | options.api | <code>Api</code> | | 用于获取字典列表的接口 |
34
+ | [options.idKey] | <code>string</code> | <code>&quot;&#x27;id&#x27;&quot;</code> | 字段id的key键值 |
35
+ | [options.nameKey] | <code>string</code> | <code>&quot;&#x27;name&#x27;&quot;</code> | 字段name的key键值 |
36
+ | [options.labelKey] | <code>string</code> | <code>&quot;&#x27;label&#x27;&quot;</code> | 字段label的key键值 |
37
+ | [options.SelectOption] | <code>ReactDom</code> | | 指定\<Select.Option\>对象是谁 |
38
+ | [options.RadioOption] | <code>ReactDom</code> | | 指定\<Radio.Button\>对象是谁 |
39
+ | [options.beforeApi] | <code>callback</code> | | (request:object)=>object 接口调用前的参数拦截器 |
40
+ | [options.afterApi] | <code>callback</code> | | (reponse:object)=>object[] 接口调用后的拦截器 |
41
+ | [options.defaultTypes] | <code>Array.&lt;DictionaryItem&gt;</code> | | 如果字典不是通过api获取,可以在这里设置字典的内容 |
10
42
 
43
+ **Example**
44
+ ```js
45
+ // emUserType.jsx
46
+ export const userType = createDictionary({
47
+ api:()=>Promise.resolve([{id:'1001',label:'项目1001',name:'pm1001'},]),
48
+ afterApi:(response)=>{
49
+ return response?.data;
50
+ }
51
+ });
52
+ // index.jsx
53
+ import {userType} from './emUserType.jsx'
54
+ const Page=()=>{
55
+ const emUserType = userType();
56
+ return (
57
+ <section>
58
+ <Select defaultValue={emUserType.getId('项目1001')}>
59
+ {emUserType.selectOptions}
60
+ </Select>
61
+ </section>
62
+ )
63
+ }
64
+ ```
11
65
  <a id="module_usedictionary__usedictionary"></a>
12
66
  ### <span style="display:inline-flex;align-items:center;justify-content:center;width:34px;height:34px;border-radius:4px;color:rgb(71, 128, 227);background:#ede6e6;">M</span> useDictionary(props) ⇒ <code>UseDictionaryResult</code>
13
67
  字典管理
@@ -26,12 +80,13 @@
26
80
  | [props.RadioOption] | <code>ReactDom</code> | | 指定\<Radio.Button\>对象是谁 |
27
81
  | [props.beforeApi] | <code>callback</code> | | (request:object)=>object 接口调用前的参数拦截器 |
28
82
  | [props.afterApi] | <code>callback</code> | | (reponse:object)=>object[] 接口调用后的拦截器 |
83
+ | [props.defaultTypes] | <code>Array.&lt;DictionaryItem&gt;</code> | | 如果字典不是通过api获取,可以在这里设置字典的内容 |
29
84
 
30
85
  **Example**
31
86
  ```js
32
87
  import {Select,Radio} from 'antd';
33
- import useDictionary from '@/useDictionary';
34
- useDictionary.SET({SelectOption:Select.Option,RadioOption:Radio.Button});
88
+ import useDictionary,{SetConfig} from '@/useDictionary';
89
+ SetConfig({SelectOption:Select.Option,RadioOption:Radio.Button});
35
90
 
36
91
  const Index=()=>{
37
92
 
@@ -69,17 +124,6 @@ const Index=()=>{
69
124
  )
70
125
  }
71
126
  ```
72
- <a id="module_usedictionary__set"></a>
73
- ### <span style="display:inline-flex;align-items:center;justify-content:center;width:34px;height:34px;border-radius:4px;color:rgb(71, 128, 227);background:#ede6e6;">M</span> SET(params) ⇒ <code>void</code>
74
- 全局设置SelectOption和RadioOption
75
-
76
-
77
- | Param | Type | Description |
78
- | --- | --- | --- |
79
- | params | <code>Object</code> | |
80
- | params.SelectOption | <code>ReactDom</code> | Antd的SelectOption组件 |
81
- | params.RadioOption | <code>ReactDom</code> | Antd的SelectOption组件 |
82
-
83
127
  <a id="module_usedictionary__api"></a>
84
128
  ### <span style="display:inline-flex;align-items:center;justify-content:center;width:34px;height:34px;border-radius:4px;color:rgb(71, 128, 227);background:#ede6e6;">T</span> Api ⇒ <code>Object</code>
85
129
  **Properties**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kn-hooks",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
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",
package/readme.md CHANGED
@@ -63,7 +63,14 @@ webpack增加`bable-plugin-import`插件,给babel plugins增加配置
63
63
  // }
64
64
  // ]
65
65
 
66
- ```<a id="module_useclipboard"></a>
66
+ ```
67
+
68
+
69
+
70
+ ## API
71
+
72
+
73
+ <a id="module_useclipboard"></a>
67
74
  ## <span style="display:inline-flex;align-items:center;justify-content:center;width:34px;height:34px;border-radius:4px;color:rgb(71, 128, 227);background:#ede6e6;">G</span> useClipboard
68
75
 
69
76
  * [ useClipboard](#module_useclipboard)
@@ -144,12 +151,66 @@ return <span>当前计数器:{counter.count}</span>
144
151
  ## <span style="display:inline-flex;align-items:center;justify-content:center;width:34px;height:34px;border-radius:4px;color:rgb(71, 128, 227);background:#ede6e6;">G</span> useDictionary
145
152
 
146
153
  * [ useDictionary](#module_usedictionary)
147
- * [ useDictionary(props)](#module_usedictionary__usedictionary) ⇒ <code>UseDictionaryResult</code>
148
- * [ SET(params)](#module_usedictionary__set) ⇒ <code>void</code>
149
- * [ Api](#module_usedictionary__api) ⇒ <code>Object</code>
150
- * [ DictionaryItem](#module_usedictionary__dictionaryitem)
151
- * [ UseDictionaryResult](#module_usedictionary__usedictionaryresult)
154
+ * _static_
155
+ * [ SetConfig(params)](#module_usedictionary_setconfig) ⇒ <code>void</code>
156
+ * [ createDictionary(options)](#module_usedictionary_createdictionary) ⇒ <code>callback</code>
157
+ * _inner_
158
+ * [ useDictionary(props)](#module_usedictionary__usedictionary) ⇒ <code>UseDictionaryResult</code>
159
+ * [ Api](#module_usedictionary__api) ⇒ <code>Object</code>
160
+ * [ DictionaryItem](#module_usedictionary__dictionaryitem)
161
+ * [ UseDictionaryResult](#module_usedictionary__usedictionaryresult)
162
+
163
+ <a id="module_usedictionary_setconfig"></a>
164
+ ### <span style="display:inline-flex;align-items:center;justify-content:center;width:34px;height:34px;border-radius:4px;color:rgb(71, 128, 227);background:#ede6e6;">M</span> SetConfig(params) ⇒ <code>void</code>
165
+ 全局设置SelectOption和RadioOption
166
+
167
+
168
+ | Param | Type | Description |
169
+ | --- | --- | --- |
170
+ | params | <code>Object</code> | |
171
+ | params.SelectOption | <code>ReactDom</code> | Antd的SelectOption组件 |
172
+ | params.RadioOption | <code>ReactDom</code> | Antd的SelectOption组件 |
173
+
174
+ <a id="module_usedictionary_createdictionary"></a>
175
+ ### <span style="display:inline-flex;align-items:center;justify-content:center;width:34px;height:34px;border-radius:4px;color:rgb(71, 128, 227);background:#ede6e6;">M</span> createDictionary(options) ⇒ <code>callback</code>
176
+ 创建字典hooks工具
177
+
178
+
179
+ | Param | Type | Default | Description |
180
+ | --- | --- | --- | --- |
181
+ | options | <code>Object</code> | | |
182
+ | options.api | <code>Api</code> | | 用于获取字典列表的接口 |
183
+ | [options.idKey] | <code>string</code> | <code>&quot;&#x27;id&#x27;&quot;</code> | 字段id的key键值 |
184
+ | [options.nameKey] | <code>string</code> | <code>&quot;&#x27;name&#x27;&quot;</code> | 字段name的key键值 |
185
+ | [options.labelKey] | <code>string</code> | <code>&quot;&#x27;label&#x27;&quot;</code> | 字段label的key键值 |
186
+ | [options.SelectOption] | <code>ReactDom</code> | | 指定\<Select.Option\>对象是谁 |
187
+ | [options.RadioOption] | <code>ReactDom</code> | | 指定\<Radio.Button\>对象是谁 |
188
+ | [options.beforeApi] | <code>callback</code> | | (request:object)=>object 接口调用前的参数拦截器 |
189
+ | [options.afterApi] | <code>callback</code> | | (reponse:object)=>object[] 接口调用后的拦截器 |
190
+ | [options.defaultTypes] | <code>Array.&lt;DictionaryItem&gt;</code> | | 如果字典不是通过api获取,可以在这里设置字典的内容 |
152
191
 
192
+ **Example**
193
+ ```js
194
+ // emUserType.jsx
195
+ export const userType = createDictionary({
196
+ api:()=>Promise.resolve([{id:'1001',label:'项目1001',name:'pm1001'},]),
197
+ afterApi:(response)=>{
198
+ return response?.data;
199
+ }
200
+ });
201
+ // index.jsx
202
+ import {userType} from './emUserType.jsx'
203
+ const Page=()=>{
204
+ const emUserType = userType();
205
+ return (
206
+ <section>
207
+ <Select defaultValue={emUserType.getId('项目1001')}>
208
+ {emUserType.selectOptions}
209
+ </Select>
210
+ </section>
211
+ )
212
+ }
213
+ ```
153
214
  <a id="module_usedictionary__usedictionary"></a>
154
215
  ### <span style="display:inline-flex;align-items:center;justify-content:center;width:34px;height:34px;border-radius:4px;color:rgb(71, 128, 227);background:#ede6e6;">M</span> useDictionary(props) ⇒ <code>UseDictionaryResult</code>
155
216
  字典管理
@@ -168,12 +229,13 @@ return <span>当前计数器:{counter.count}</span>
168
229
  | [props.RadioOption] | <code>ReactDom</code> | | 指定\<Radio.Button\>对象是谁 |
169
230
  | [props.beforeApi] | <code>callback</code> | | (request:object)=>object 接口调用前的参数拦截器 |
170
231
  | [props.afterApi] | <code>callback</code> | | (reponse:object)=>object[] 接口调用后的拦截器 |
232
+ | [props.defaultTypes] | <code>Array.&lt;DictionaryItem&gt;</code> | | 如果字典不是通过api获取,可以在这里设置字典的内容 |
171
233
 
172
234
  **Example**
173
235
  ```js
174
236
  import {Select,Radio} from 'antd';
175
- import useDictionary from '@/useDictionary';
176
- useDictionary.SET({SelectOption:Select.Option,RadioOption:Radio.Button});
237
+ import useDictionary,{SetConfig} from '@/useDictionary';
238
+ SetConfig({SelectOption:Select.Option,RadioOption:Radio.Button});
177
239
 
178
240
  const Index=()=>{
179
241
 
@@ -211,17 +273,6 @@ const Index=()=>{
211
273
  )
212
274
  }
213
275
  ```
214
- <a id="module_usedictionary__set"></a>
215
- ### <span style="display:inline-flex;align-items:center;justify-content:center;width:34px;height:34px;border-radius:4px;color:rgb(71, 128, 227);background:#ede6e6;">M</span> SET(params) ⇒ <code>void</code>
216
- 全局设置SelectOption和RadioOption
217
-
218
-
219
- | Param | Type | Description |
220
- | --- | --- | --- |
221
- | params | <code>Object</code> | |
222
- | params.SelectOption | <code>ReactDom</code> | Antd的SelectOption组件 |
223
- | params.RadioOption | <code>ReactDom</code> | Antd的SelectOption组件 |
224
-
225
276
  <a id="module_usedictionary__api"></a>
226
277
  ### <span style="display:inline-flex;align-items:center;justify-content:center;width:34px;height:34px;border-radius:4px;color:rgb(71, 128, 227);background:#ede6e6;">T</span> Api ⇒ <code>Object</code>
227
278
  **Properties**
package/src/.DS_Store ADDED
Binary file
@@ -22,14 +22,15 @@ const DEFAULT_CONFIG={
22
22
  * @param {ReactDom} [props.RadioOption] - 指定\<Radio.Button\>对象是谁
23
23
  * @param {callback} [props.beforeApi] - (request:object)=>object 接口调用前的参数拦截器
24
24
  * @param {callback} [props.afterApi] - (reponse:object)=>object[] 接口调用后的拦截器
25
+ * @param {DictionaryItem[]} [props.defaultTypes] - 如果字典不是通过api获取,可以在这里设置字典的内容
25
26
  *
26
27
  *
27
28
  * @return {UseDictionaryResult}
28
29
  *
29
30
  * @example
30
31
  import {Select,Radio} from 'antd';
31
- import useDictionary from '@/useDictionary';
32
- useDictionary.SET({SelectOption:Select.Option,RadioOption:Radio.Button});
32
+ import useDictionary,{SetConfig} from '@/useDictionary';
33
+ SetConfig({SelectOption:Select.Option,RadioOption:Radio.Button});
33
34
 
34
35
  const Index=()=>{
35
36
 
@@ -76,12 +77,14 @@ const useDictionary=(props)=>{
76
77
  labelKey = 'label',
77
78
  beforeApi,
78
79
  afterApi,
80
+ defaultTypes,
79
81
  SelectOption= DEFAULT_CONFIG.SelectOption,
80
82
  RadioOption= DEFAULT_CONFIG.RadioOption } = props;
81
83
 
82
- const [types, setTypes] = useState(null);
84
+ const [types, setTypes] = useState(defaultTypes||null);
83
85
 
84
86
  const init = async () => {
87
+ if(!api)return;
85
88
  let params={};
86
89
  if(beforeApi){
87
90
  params = beforeApi();
@@ -176,11 +179,166 @@ const useDictionary=(props)=>{
176
179
 
177
180
  }
178
181
 
182
+ /**
183
+ * @function
184
+ * @description 全局设置SelectOption和RadioOption
185
+ * @param {Object} params
186
+ * @param {ReactDom} params.SelectOption - Antd的SelectOption组件
187
+ * @param {ReactDom} params.RadioOption - Antd的SelectOption组件
188
+ * @returns {void}
189
+ */
179
190
  export const SetConfig = ({SelectOption,RadioOption})=>{
180
191
  if(SelectOption)DEFAULT_CONFIG.SelectOption = SelectOption;
181
192
  if(RadioOption)DEFAULT_CONFIG.RadioOption = RadioOption;
182
193
  };
183
194
 
195
+ /**
196
+ * @function
197
+ * @description 创建字典hooks工具
198
+ * @param {Object} options
199
+ * @param {Api} options.api - 用于获取字典列表的接口
200
+ * @param {string} [options.idKey='id'] - 字段id的key键值
201
+ * @param {string} [options.nameKey='name'] - 字段name的key键值
202
+ * @param {string} [options.labelKey='label'] - 字段label的key键值
203
+ * @param {ReactDom} [options.SelectOption] - 指定\<Select.Option\>对象是谁
204
+ * @param {ReactDom} [options.RadioOption] - 指定\<Radio.Button\>对象是谁
205
+ * @param {callback} [options.beforeApi] - (request:object)=>object 接口调用前的参数拦截器
206
+ * @param {callback} [options.afterApi] - (reponse:object)=>object[] 接口调用后的拦截器
207
+ * @param {DictionaryItem[]} [options.defaultTypes] - 如果字典不是通过api获取,可以在这里设置字典的内容
208
+ *
209
+ * @returns {callback}
210
+ *
211
+ * @example
212
+ // emUserType.jsx
213
+ export const userType = createDictionary({
214
+ api:()=>Promise.resolve([{id:'1001',label:'项目1001',name:'pm1001'},]),
215
+ afterApi:(response)=>{
216
+ return response?.data;
217
+ }
218
+ });
219
+ // index.jsx
220
+ import {userType} from './emUserType.jsx'
221
+ const Page=()=>{
222
+ const emUserType = userType();
223
+ return (
224
+ <section>
225
+ <Select defaultValue={emUserType.getId('项目1001')}>
226
+ {emUserType.selectOptions}
227
+ </Select>
228
+ </section>
229
+ )
230
+ }
231
+ */
232
+ export const createDictionary=options=>{
233
+ const {
234
+ api,
235
+ idKey = 'id',
236
+ nameKey = 'name',
237
+ labelKey = 'label',
238
+ beforeApi,
239
+ afterApi,
240
+ defaultTypes=null,
241
+ SelectOption= DEFAULT_CONFIG.SelectOption,
242
+ RadioOption= DEFAULT_CONFIG.RadioOption } = options;
243
+
244
+ return (props)=>{
245
+ const [types, setTypes] = useState(defaultTypes||null);
246
+
247
+ const init = async () => {
248
+ if(!api)return;
249
+ let params={};
250
+ if(beforeApi){
251
+ params = beforeApi();
252
+ }
253
+ const ret = await api(params);
254
+ if(afterApi){
255
+ ret = afterApi(ret);
256
+ setTypes(ret||[]);
257
+ }else{
258
+ if (+ret?.code === 0) {
259
+ setTypes(ret.data.body || []);
260
+ }
261
+ }
262
+ };
263
+
264
+ useEffect(() => {
265
+ init();
266
+ }, []);
267
+
268
+ const getLabel = (id) => {
269
+ if (types) {
270
+ for (let i = 0; i < types.length; i++) {
271
+ if ('' + types[i][idKey] === '' + id) {
272
+ return types[i][labelKey];
273
+ }
274
+ }
275
+ }
276
+ return '';
277
+ };
278
+ const getName = (id) => {
279
+ if (types) {
280
+ for (let i = 0; i < types.length; i++) {
281
+ if ('' + types[i][idKey] === '' + id) {
282
+ return types[i][nameKey];
283
+ }
284
+ }
285
+ }
286
+ return '';
287
+ };
288
+ const getId = (labelOrName) => {
289
+ if (types) {
290
+ for (let i = 0; i < types.length; i++) {
291
+ const label = '' + types[i][labelKey];
292
+ const name = '' + types[i][nameKey];
293
+ const txt = '' + labelOrName;
294
+ if (label === txt || name === txt) {
295
+ return types[i][idKey];
296
+ }
297
+ }
298
+ }
299
+ return '';
300
+ };
301
+
302
+ const selectOptions = useMemo(() => {
303
+ if(!SelectOption)return <>SelectOption未指定</>;
304
+ return (
305
+ types?.map((item) => {
306
+ return (
307
+ <SelectOption key={item[idKey]} value={item[idKey]} data-keyname={item[nameKey]}>
308
+ {item[labelKey]}
309
+ </SelectOption>
310
+ );
311
+ }) || ''
312
+ );
313
+ }, [types,SelectOption]);
314
+
315
+ const radioOptions = useMemo(() => {
316
+ if(!RadioOption)return <>RadioOption没指定</>;
317
+ return (
318
+ types?.map((item) => {
319
+ return (
320
+ <RadioOption key={item[idKey]} value={item[idKey]} data-keyname={item[nameKey]}>
321
+ {item[labelKey]}
322
+ </RadioOption>
323
+ );
324
+ }) || ''
325
+ );
326
+ }, [types,RadioOption]);
327
+
328
+ const isReady = () => {
329
+ return types !== null;
330
+ };
331
+ const reload = () => {
332
+ init();
333
+ };
334
+
335
+ const actions = useMemo(() => {
336
+ return { types, getLabel, getId, getName, selectOptions, isReady, reload, radioOptions };
337
+ }, [types]);
338
+
339
+ return actions;
340
+ }
341
+ }
184
342
 
185
343
 
186
344
  /**
@@ -212,15 +370,7 @@ export const SetConfig = ({SelectOption,RadioOption})=>{
212
370
  *
213
371
  */
214
372
 
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
- */
373
+
224
374
 
225
375
 
226
376
  export default useDictionary
package/test/.DS_Store ADDED
Binary file