kn-hooks 0.0.34 → 0.0.35

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kn-hooks",
3
- "version": "0.0.34",
3
+ "version": "0.0.35",
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",
@@ -18,10 +18,10 @@ const DEFAULT_CONFIG={
18
18
  * @param {string} [props.idKey='id'] - 字段id的key键值
19
19
  * @param {string} [props.nameKey='name'] - 字段name的key键值
20
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[] 接口调用后的拦截器
21
+ * @param {JSX.Element} [props.SelectOption] - 指定\<Select.Option\>对象是谁
22
+ * @param {JSX.Element} [props.RadioOption] - 指定\<Radio.Button\>对象是谁
23
+ * @param {(request:object)=>object} [props.beforeApi] - (request:object)=>object 接口调用前的参数拦截器
24
+ * @param {(response:object)=>object} [props.afterApi] - (reponse:object)=>object[] 接口调用后的拦截器
25
25
  * @param {DictionaryItem[]} [props.defaultTypes] - 如果字典不是通过api获取,可以在这里设置字典的内容
26
26
  *
27
27
  *
@@ -138,6 +138,41 @@ const useDictionary=(props)=>{
138
138
  return '';
139
139
  };
140
140
 
141
+ const getOptions=(props)=>{
142
+ const {onFilter,onDisabled}=props;
143
+ let req=[];
144
+ types.forEach(item=>{
145
+ let show=true;
146
+ let disabled=false;
147
+
148
+ if(onFilter){
149
+ show = onFilter(item);
150
+ }
151
+ if(onDisabled){
152
+ disabled = onDisabled(item);
153
+ }
154
+ if(show){
155
+ req.push({
156
+ ...item,
157
+ disabled:disabled,
158
+ })
159
+ }
160
+ })
161
+ return req;
162
+ }
163
+
164
+ const renderOptions=(props)=>{
165
+ const {options=[],OptionComponent}=props;
166
+ let req= options.map(item=>{
167
+ if(OptionComponent){
168
+ // @ts-ignore
169
+ return <OptionComponent value={item[idKey]} key={item[idKey]} disabled={item.disabled}>{item[labelKey]}</OptionComponent>
170
+ }
171
+ })
172
+ return req;
173
+ }
174
+
175
+
141
176
  const selectOptions = useMemo(() => {
142
177
  if(!SelectOption)return <>SelectOption未指定</>;
143
178
  return (
@@ -172,7 +207,7 @@ const useDictionary=(props)=>{
172
207
  };
173
208
 
174
209
  const actions = useMemo(() => {
175
- return { types, getLabel, getId, getName, selectOptions, isReady, reload, radioOptions };
210
+ return { types, getLabel, getId, getName, selectOptions, isReady, reload, radioOptions, renderOptions, getOptions };
176
211
  }, [types]);
177
212
 
178
213
  return actions;
@@ -204,13 +239,13 @@ export const SetConfig = ({SelectOption,RadioOption})=>{
204
239
  * @param {string} [options.idKey='id'] - 字段id的key键值
205
240
  * @param {string} [options.nameKey='name'] - 字段name的key键值
206
241
  * @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[] 接口调用后的拦截器
242
+ * @param {JSX.Element} [options.SelectOption] - 指定\<Select.Option\>对象是谁
243
+ * @param {JSX.Element} [options.RadioOption] - 指定\<Radio.Button\>对象是谁
244
+ * @param {(request:object)=>object} [options.beforeApi] - (request:object)=>object 接口调用前的参数拦截器
245
+ * @param {(response:object)=>object} [options.afterApi] - (reponse:object)=>object[] 接口调用后的拦截器
211
246
  * @param {DictionaryItem[]} [options.defaultTypes] - 如果字典不是通过api获取,可以在这里设置字典的内容
212
247
  *
213
- * @returns {callback}
248
+ * @returns {()=>UseDictionaryResult}
214
249
  *
215
250
  * @example
216
251
  // emUserType.jsx
@@ -313,6 +348,40 @@ export const createDictionary=options=>{
313
348
  return '';
314
349
  };
315
350
 
351
+ const getOptions=(props)=>{
352
+ const {onFilter,onDisabled}=props;
353
+ let req=[];
354
+ types.forEach(item=>{
355
+ let show=true;
356
+ let disabled=false;
357
+
358
+ if(onFilter){
359
+ show = onFilter(item);
360
+ }
361
+ if(onDisabled){
362
+ disabled = onDisabled(item);
363
+ }
364
+ if(show){
365
+ req.push({
366
+ ...item,
367
+ disabled:disabled,
368
+ })
369
+ }
370
+ })
371
+ return req;
372
+ }
373
+
374
+ const renderOptions=(props)=>{
375
+ const {options=[],OptionComponent}=props;
376
+ let req= options.map(item=>{
377
+ if(OptionComponent){
378
+ // @ts-ignore
379
+ return <OptionComponent value={item[idKey]} key={item[idKey]} disabled={item.disabled}>{item[labelKey]}</OptionComponent>
380
+ }
381
+ })
382
+ return req;
383
+ }
384
+
316
385
  const selectOptions = useMemo(() => {
317
386
  if(!SelectOption)return <>SelectOption未指定</>;
318
387
  return (
@@ -347,7 +416,7 @@ export const createDictionary=options=>{
347
416
  };
348
417
 
349
418
  const actions = useMemo(() => {
350
- return { types, getLabel, getId, getName, selectOptions, isReady, reload, radioOptions };
419
+ return { types, getLabel, getId, getName, selectOptions, isReady, reload, radioOptions,getOptions,renderOptions };
351
420
  }, [types]);
352
421
 
353
422
  return actions;
@@ -370,7 +439,23 @@ export const createDictionary=options=>{
370
439
  * @property {string} id - 数据唯一ID
371
440
  * @property {string} name - 数据唯一id对应的别名
372
441
  * @property {string} label - 展示给用户看的文字
373
- *
442
+ * @property {boolean} [disabled] - 是否禁用
443
+ * @property {object} [data] - 原始数据
444
+ */
445
+
446
+
447
+ /**
448
+ * @typedef GetOptionsProps
449
+ * @property {object} props
450
+ * @property {(item:DictionaryItem)=>boolean} [props.onFilter] - 数据唯一id对应的别名
451
+ * @property {(item:DictionaryItem)=>boolean} [props.onDisabled] - 数据唯一id对应的别名
452
+ */
453
+
454
+ /**
455
+ * @typedef RenderOptionsProps
456
+ * @property {object} props
457
+ * @property {DictionaryItem[]} [props.options] - 数据唯一id对应的别名
458
+ * @property {JSX.Element} [props.OptionComponent] - 数据唯一id对应的别名
374
459
  */
375
460
 
376
461
  /**
@@ -382,6 +467,8 @@ export const createDictionary=options=>{
382
467
  * @property {function} getName - (id:string)=>string,搜索字典项中,id匹配id的项目,返回其name的值
383
468
  * @property {function} getLabel - (id:string)=>string,搜索字典项中,id匹配id的项目,返回其label的值
384
469
  * @property {function} reload - ()=>void,重新调用字典接口刷新字典列表
470
+ * @property {(props?:GetOptionsProps)=>DictionaryItem[]} getOptions - 获取选项列表
471
+ * @property {(props:RenderOptionsProps)=>JSX.Element} renderOptions - 渲染选项列表
385
472
  *
386
473
  */
387
474
 
@@ -5,16 +5,19 @@
5
5
  import { useState,useMemo,useRef } from 'react';
6
6
  import useSwitch from '../useSwitch';
7
7
 
8
+ /**
9
+ * @typedef UsePaginationProps
10
+ * @property {FunServices} service - 发送请求的方法
11
+ * @property {Pagination} [pagination] - 默认分页信息
12
+ * @property {Array<Function>} [beforeService] - api调用前监听方法列表
13
+ * @property {Array<Function>} [afterService] - api调用后监听方法列表
14
+ */
15
+
8
16
  /**
9
17
  * 分页管理器
10
18
  *
11
19
  *
12
- * @param {Object} props
13
- * @param {FunServices} props.service - 发送请求的方法
14
- * @param {Pagination} [props.pagination] - 默认分页信息
15
- * @param {Array<Function>} [props.beforeService] - api调用前监听方法列表
16
- * @param {Array<Function>} [props.afterService] - api调用后监听方法列表
17
- *
20
+ * @param {UsePaginationProps & *} props
18
21
  * @version 1.0.1
19
22
  *
20
23
  * @example
@@ -252,9 +255,9 @@ const usePagination=(props)=>{
252
255
  /**
253
256
  * 分页信息
254
257
  * @typedef {Object} Pagination
255
- * @property {number} pageSize=20 - 分页大小
256
- * @property {number} current=1 - 当前页码
257
- * @property {number} total=1 - 总记录数
258
+ * @property {number} [pageSize=20] - 分页大小
259
+ * @property {number} [current=1] - 当前页码
260
+ * @property {number} [total=1] - 总记录数
258
261
  * @property {number} [startIdx=0] - 当前页面下第一条数据的序号
259
262
  * @property {boolean} [more] - 是否还有下一页
260
263
  *
@@ -5,14 +5,17 @@
5
5
  import { useState, useMemo, useEffect, useRef } from 'react';
6
6
  import usePagination from '../usePagination';
7
7
 
8
+ /**
9
+ * @typedef UsePaginationWithFormProps
10
+ * @property {Object} form - Form表单的ref,在接口调用前会校验获取form表单内的数据,提交到接口参数内查询
11
+ */
8
12
 
9
13
  /**
10
14
  * 支持Antd-Form的usePagination
11
15
  * 作用是在查询接口前自动获取form表单内的字段,并作为接口查询参数进行查询
12
16
  * 使用方法及参数字段参考 [usePagination]{@link usePagination.md}
13
17
  *
14
- * @param {Object} props
15
- * @param {Object} props.form - Form表单的ref,在接口调用前会校验获取form表单内的数据,提交到接口参数内查询
18
+ * @param {import('../usePagination').UsePaginationProps & UsePaginationWithFormProps} props
16
19
  *
17
20
  * @example
18
21
  const [form] = Form.useForm();
@@ -102,10 +105,13 @@ const usePaginationWithForm = (props) => {
102
105
 
103
106
  /**
104
107
  * usePaginationWithForm的返回对象
105
- * @typedef {Object} UsePaginationWithFormResult
108
+ * @typedef {Object} _UsePaginationWithFormResult
106
109
  * @property {Function} reset - 重置方法,返回Promise
107
110
  */
108
111
 
112
+ /**
113
+ * @typedef {UsePaginationResult & _UsePaginationWithFormResult} UsePaginationWithFormResult
114
+ */
109
115
 
110
116
 
111
117
  export default usePaginationWithForm