kn-hooks 0.0.13 → 0.0.15

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.13",
3
+ "version": "0.0.15",
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
@@ -151,12 +151,66 @@ return <span>当前计数器:{counter.count}</span>
151
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
152
152
 
153
153
  * [ useDictionary](#module_usedictionary)
154
- * [ useDictionary(props)](#module_usedictionary__usedictionary) ⇒ <code>UseDictionaryResult</code>
155
- * [ SET(params)](#module_usedictionary__set) ⇒ <code>void</code>
156
- * [ Api](#module_usedictionary__api) ⇒ <code>Object</code>
157
- * [ DictionaryItem](#module_usedictionary__dictionaryitem)
158
- * [ 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组件 |
159
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获取,可以在这里设置字典的内容 |
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
+ ```
160
214
  <a id="module_usedictionary__usedictionary"></a>
161
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>
162
216
  字典管理
@@ -175,12 +229,13 @@ return <span>当前计数器:{counter.count}</span>
175
229
  | [props.RadioOption] | <code>ReactDom</code> | | 指定\<Radio.Button\>对象是谁 |
176
230
  | [props.beforeApi] | <code>callback</code> | | (request:object)=>object 接口调用前的参数拦截器 |
177
231
  | [props.afterApi] | <code>callback</code> | | (reponse:object)=>object[] 接口调用后的拦截器 |
232
+ | [props.defaultTypes] | <code>Array.&lt;DictionaryItem&gt;</code> | | 如果字典不是通过api获取,可以在这里设置字典的内容 |
178
233
 
179
234
  **Example**
180
235
  ```js
181
236
  import {Select,Radio} from 'antd';
182
- import useDictionary from '@/useDictionary';
183
- useDictionary.SET({SelectOption:Select.Option,RadioOption:Radio.Button});
237
+ import useDictionary,{SetConfig} from '@/useDictionary';
238
+ SetConfig({SelectOption:Select.Option,RadioOption:Radio.Button});
184
239
 
185
240
  const Index=()=>{
186
241
 
@@ -218,17 +273,6 @@ const Index=()=>{
218
273
  )
219
274
  }
220
275
  ```
221
- <a id="module_usedictionary__set"></a>
222
- ### <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>
223
- 全局设置SelectOption和RadioOption
224
-
225
-
226
- | Param | Type | Description |
227
- | --- | --- | --- |
228
- | params | <code>Object</code> | |
229
- | params.SelectOption | <code>ReactDom</code> | Antd的SelectOption组件 |
230
- | params.RadioOption | <code>ReactDom</code> | Antd的SelectOption组件 |
231
-
232
276
  <a id="module_usedictionary__api"></a>
233
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>
234
278
  **Properties**
package/src/.DS_Store CHANGED
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,171 @@ 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
+ * @example
190
+ import {useDictionary} from 'kn-hooks';
191
+ import {Select,Radio} from 'antd';
192
+ useDictionary.SetConfig(Select.Option,Radio.Option);
193
+ */
179
194
  export const SetConfig = ({SelectOption,RadioOption})=>{
180
195
  if(SelectOption)DEFAULT_CONFIG.SelectOption = SelectOption;
181
196
  if(RadioOption)DEFAULT_CONFIG.RadioOption = RadioOption;
182
197
  };
183
198
 
199
+ /**
200
+ * @function
201
+ * @description 创建字典hooks工具
202
+ * @param {Object} options
203
+ * @param {Api} options.api - 用于获取字典列表的接口
204
+ * @param {string} [options.idKey='id'] - 字段id的key键值
205
+ * @param {string} [options.nameKey='name'] - 字段name的key键值
206
+ * @param {string} [options.labelKey='label'] - 字段label的key键值
207
+ * @param {ReactDom} [options.SelectOption] - 指定\<Select.Option\>对象是谁
208
+ * @param {ReactDom} [options.RadioOption] - 指定\<Radio.Button\>对象是谁
209
+ * @param {callback} [options.beforeApi] - (request:object)=>object 接口调用前的参数拦截器
210
+ * @param {callback} [options.afterApi] - (reponse:object)=>object[] 接口调用后的拦截器
211
+ * @param {DictionaryItem[]} [options.defaultTypes] - 如果字典不是通过api获取,可以在这里设置字典的内容
212
+ *
213
+ * @returns {callback}
214
+ *
215
+ * @example
216
+ // emUserType.jsx
217
+ import {useDictionary} from 'kn-hooks';
218
+ export const userType = useDictionary.createDictionary({
219
+ api:()=>Promise.resolve([{id:'1001',label:'项目1001',name:'pm1001'},]),
220
+ afterApi:(response)=>{
221
+ return response?.data;
222
+ }
223
+ });
224
+ // index.jsx
225
+ import {userType} from './emUserType.jsx'
226
+ const Page=()=>{
227
+ const emUserType = userType();
228
+ return (
229
+ <section>
230
+ <Select defaultValue={emUserType.getId('项目1001')}>
231
+ {emUserType.selectOptions}
232
+ </Select>
233
+ </section>
234
+ )
235
+ }
236
+ */
237
+ export const createDictionary=options=>{
238
+ const {
239
+ api,
240
+ idKey = 'id',
241
+ nameKey = 'name',
242
+ labelKey = 'label',
243
+ beforeApi,
244
+ afterApi,
245
+ defaultTypes=null,
246
+ SelectOption= DEFAULT_CONFIG.SelectOption,
247
+ RadioOption= DEFAULT_CONFIG.RadioOption } = options;
248
+
249
+ return (props)=>{
250
+ const [types, setTypes] = useState(defaultTypes||null);
251
+
252
+ const init = async () => {
253
+ if(!api)return;
254
+ let params={};
255
+ if(beforeApi){
256
+ params = beforeApi();
257
+ }
258
+ const ret = await api(params);
259
+ if(afterApi){
260
+ ret = afterApi(ret);
261
+ setTypes(ret||[]);
262
+ }else{
263
+ if (+ret?.code === 0) {
264
+ setTypes(ret.data.body || []);
265
+ }
266
+ }
267
+ };
268
+
269
+ useEffect(() => {
270
+ init();
271
+ }, []);
272
+
273
+ const getLabel = (id) => {
274
+ if (types) {
275
+ for (let i = 0; i < types.length; i++) {
276
+ if ('' + types[i][idKey] === '' + id) {
277
+ return types[i][labelKey];
278
+ }
279
+ }
280
+ }
281
+ return '';
282
+ };
283
+ const getName = (id) => {
284
+ if (types) {
285
+ for (let i = 0; i < types.length; i++) {
286
+ if ('' + types[i][idKey] === '' + id) {
287
+ return types[i][nameKey];
288
+ }
289
+ }
290
+ }
291
+ return '';
292
+ };
293
+ const getId = (labelOrName) => {
294
+ if (types) {
295
+ for (let i = 0; i < types.length; i++) {
296
+ const label = '' + types[i][labelKey];
297
+ const name = '' + types[i][nameKey];
298
+ const txt = '' + labelOrName;
299
+ if (label === txt || name === txt) {
300
+ return types[i][idKey];
301
+ }
302
+ }
303
+ }
304
+ return '';
305
+ };
306
+
307
+ const selectOptions = useMemo(() => {
308
+ if(!SelectOption)return <>SelectOption未指定</>;
309
+ return (
310
+ types?.map((item) => {
311
+ return (
312
+ <SelectOption key={item[idKey]} value={item[idKey]} data-keyname={item[nameKey]}>
313
+ {item[labelKey]}
314
+ </SelectOption>
315
+ );
316
+ }) || ''
317
+ );
318
+ }, [types,SelectOption]);
319
+
320
+ const radioOptions = useMemo(() => {
321
+ if(!RadioOption)return <>RadioOption没指定</>;
322
+ return (
323
+ types?.map((item) => {
324
+ return (
325
+ <RadioOption key={item[idKey]} value={item[idKey]} data-keyname={item[nameKey]}>
326
+ {item[labelKey]}
327
+ </RadioOption>
328
+ );
329
+ }) || ''
330
+ );
331
+ }, [types,RadioOption]);
332
+
333
+ const isReady = () => {
334
+ return types !== null;
335
+ };
336
+ const reload = () => {
337
+ init();
338
+ };
339
+
340
+ const actions = useMemo(() => {
341
+ return { types, getLabel, getId, getName, selectOptions, isReady, reload, radioOptions };
342
+ }, [types]);
343
+
344
+ return actions;
345
+ }
346
+ }
184
347
 
185
348
 
186
349
  /**
@@ -212,15 +375,8 @@ export const SetConfig = ({SelectOption,RadioOption})=>{
212
375
  *
213
376
  */
214
377
 
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
378
 
379
+ useDictionary.createDictionary=createDictionary;
380
+ useDictionary.SetConfig = SetConfig;
225
381
 
226
382
  export default useDictionary
package/test/.DS_Store CHANGED
Binary file