kn-hooks 0.0.9 → 0.0.11
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/md/useClipboard.md +50 -0
- package/md/useCounter.md +29 -0
- package/md/useDictionary.md +116 -0
- package/md/usePagination.md +155 -0
- package/md/usePaginationWithForm.md +40 -0
- package/md/useSwitch.md +36 -0
- package/package.json +12 -3
- package/readme.md +17 -196
- package/src/index.js +7 -2
- package/src/useClipboard/index.js +30 -7
- package/src/useCounter/index.js +34 -0
- package/src/useDictionary/index.js +96 -26
- 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/usePagination/index.jsx +119 -0
- package/test/usePaginationWithForm/index.jsx +101 -0
- package/test/useSwitch/index.jsx +2 -5
- package/src/.DS_Store +0 -0
- package/src/useAntdFormTableSearch/index.js +0 -165
- package/src/useRefSwitch/index.js +0 -15
- package/test/.DS_Store +0 -0
- package/test/useRefSwitch/index.jsx +0 -19
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @module usePagination
|
|
4
|
+
*/
|
|
5
|
+
import { useState,useMemo,useRef } from 'react';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 分页管理器
|
|
9
|
+
* @param {Object} props
|
|
10
|
+
* @param {function} props.service - 发送请求的方法,默认分页使用current和pageSize,如有特殊需求通过beforeService拦截处理
|
|
11
|
+
* @param {Pagination} [props.pagination] - 默认分页信息
|
|
12
|
+
* @version 1.0.0
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
// 移动端滚动加载案例
|
|
16
|
+
const page = usePagination({
|
|
17
|
+
service:GET_LIST,
|
|
18
|
+
pagination:{pageSize:10},
|
|
19
|
+
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
useEffect(()=>{
|
|
23
|
+
const fnFeforeService = (params)=>{
|
|
24
|
+
// 假如你这里需要变更接口字段名称的话
|
|
25
|
+
params.page=params.current;
|
|
26
|
+
params.keyword='abc';
|
|
27
|
+
return params;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const fnAfterService = (response)=>{
|
|
31
|
+
// 这里你可以翻译及二次处理你的接口字段
|
|
32
|
+
let req={
|
|
33
|
+
code:response.errorCode
|
|
34
|
+
data:response.list,
|
|
35
|
+
page:response.pageInfo
|
|
36
|
+
};
|
|
37
|
+
response.code=response;
|
|
38
|
+
return response;
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
page.addListener('beforeService',fnFeforeService);
|
|
42
|
+
page.addListener('afterService',fnAfterService);
|
|
43
|
+
return ()=>{
|
|
44
|
+
page.removeListener('beforeService',fnFeforeService);
|
|
45
|
+
page.removeListener('afterService',fnAfterService)
|
|
46
|
+
}
|
|
47
|
+
},[])
|
|
48
|
+
|
|
49
|
+
const onReset=()=>{page.reset();}
|
|
50
|
+
|
|
51
|
+
const onPageChange=()=>{
|
|
52
|
+
let value=document.querySelector('#inputPage').value;
|
|
53
|
+
page.update({pagination:{current:+value}})
|
|
54
|
+
}
|
|
55
|
+
const onNext=()=>{page.nextPage();}
|
|
56
|
+
|
|
57
|
+
const renderTable=()=>{
|
|
58
|
+
let renderData= [];
|
|
59
|
+
page?.data?.forEach(list=>{
|
|
60
|
+
renderData=[...renderData,...list];
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<ul>
|
|
65
|
+
{
|
|
66
|
+
renderData?.map((item,idx)=>{
|
|
67
|
+
return <li key={idx}>[{idx}]{item}</li>
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
</ul>
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
}
|
|
74
|
+
return (
|
|
75
|
+
<ul>
|
|
76
|
+
{renderTable()}
|
|
77
|
+
</ul>
|
|
78
|
+
)
|
|
79
|
+
*
|
|
80
|
+
* @returns {UsePaginationResult}
|
|
81
|
+
*/
|
|
82
|
+
const usePagination=(props)=>{
|
|
83
|
+
|
|
84
|
+
const {service}= props;
|
|
85
|
+
|
|
86
|
+
const DEFAULT_PAGE_SIZE=20;
|
|
87
|
+
const DEFAULT_PAGE_CURRENT=1;
|
|
88
|
+
const [pagination,setPagination] = useState({
|
|
89
|
+
current: DEFAULT_PAGE_CURRENT,
|
|
90
|
+
pageSize: props?.pagination?.pageSize??DEFAULT_PAGE_SIZE
|
|
91
|
+
});
|
|
92
|
+
const [data,setData] = useState(null);
|
|
93
|
+
const refListener= useRef({
|
|
94
|
+
beforeService:[],
|
|
95
|
+
afterService:[]
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const update= async ({pagination:_pagination,clear=false}={})=>{
|
|
99
|
+
_pagination = _pagination ?? pagination;
|
|
100
|
+
_pagination = {...pagination,..._pagination};
|
|
101
|
+
|
|
102
|
+
const {current,pageSize} = _pagination;
|
|
103
|
+
|
|
104
|
+
let params = {current,pageSize};
|
|
105
|
+
let listener=refListener.current;
|
|
106
|
+
const {beforeService} = listener;
|
|
107
|
+
if(beforeService){
|
|
108
|
+
console.log('[usePagination] beforeService',params)
|
|
109
|
+
for(let i=0;i<beforeService.length;i++){
|
|
110
|
+
params = beforeService[i](params);
|
|
111
|
+
if(typeof params?.then == 'function'){
|
|
112
|
+
params = await params;
|
|
113
|
+
}
|
|
114
|
+
if(!params){return;}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if(!params)requrn;
|
|
118
|
+
console.log('[usePagination] service',params)
|
|
119
|
+
let req = await service(params);
|
|
120
|
+
const {afterService} = listener;
|
|
121
|
+
if(afterService){
|
|
122
|
+
console.log('[usePagination] afterService',req)
|
|
123
|
+
for(let i=0;i<afterService.length;i++){
|
|
124
|
+
req = afterService[i](req);
|
|
125
|
+
if( typeof req?.then == 'function' ){
|
|
126
|
+
req = await req;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
console.log('[usePagination] response',req)
|
|
131
|
+
let response={};
|
|
132
|
+
if(req?.code==0){
|
|
133
|
+
let {page:{current,pageSize,total},data:reqData} = req;
|
|
134
|
+
current=+current;
|
|
135
|
+
pageSize=+pageSize;
|
|
136
|
+
total=+total;
|
|
137
|
+
const startIdx= (current-1)*pageSize;
|
|
138
|
+
const more = current*pageSize<total;
|
|
139
|
+
response.pagination={
|
|
140
|
+
current,pageSize,total,startIdx,more
|
|
141
|
+
};
|
|
142
|
+
setPagination(response.pagination);
|
|
143
|
+
response.data= clear?[]:(data||[]);
|
|
144
|
+
|
|
145
|
+
if(response.data.length<current){
|
|
146
|
+
for(let i=0;i<current;i++){
|
|
147
|
+
response.data[i]=response.data[i]||[];
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
let pageIdx= current-1;
|
|
151
|
+
response.data[pageIdx]= response.data[pageIdx]||[];
|
|
152
|
+
response.data[pageIdx]= reqData||[];
|
|
153
|
+
response.data=[...response.data];
|
|
154
|
+
setData(response.data);
|
|
155
|
+
}else{
|
|
156
|
+
setData(data||[]);
|
|
157
|
+
}
|
|
158
|
+
return response;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const nextPage= async ()=>{
|
|
162
|
+
if(!pagination.more){
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
return update({pagination:{current:pagination.current+1}})
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const reset= ()=>{
|
|
169
|
+
return update({pagination:{current:1},clear:true})
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const addListener=(type,fn)=>{
|
|
173
|
+
if(!refListener.current[type]){
|
|
174
|
+
refListener.current[type]=[];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const repeat= refListener.current[type].some(callback=>{
|
|
178
|
+
if(callback==fn){return true;}
|
|
179
|
+
return false;
|
|
180
|
+
})
|
|
181
|
+
if(repeat){return;}
|
|
182
|
+
refListener.current[type].push(fn);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const removeListener=(type,fn)=>{
|
|
186
|
+
if(!refListener.current[type]){
|
|
187
|
+
refListener.current[type]=[];
|
|
188
|
+
}
|
|
189
|
+
let list = refListener.current[type];
|
|
190
|
+
for(let i=0;i<list.length;i++){
|
|
191
|
+
if(list[i] == fn){
|
|
192
|
+
list.spice(i,1);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const action= useMemo(()=>{
|
|
198
|
+
return {
|
|
199
|
+
pagination,
|
|
200
|
+
update,
|
|
201
|
+
reset,
|
|
202
|
+
nextPage,
|
|
203
|
+
data,
|
|
204
|
+
addListener,
|
|
205
|
+
removeListener
|
|
206
|
+
}
|
|
207
|
+
},[pagination,data,refListener])
|
|
208
|
+
|
|
209
|
+
return action;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 分页信息
|
|
216
|
+
* @typedef {Object} Pagination
|
|
217
|
+
* @property {number} pageSize=20 - 分页大小
|
|
218
|
+
* @property {number} current=1 - 当前页码
|
|
219
|
+
* @property {number} total=1 - 总记录数
|
|
220
|
+
* @property {number} [startIdx=0] - 当前页面下第一条数据的序号
|
|
221
|
+
* @property {boolean} [more] - 是否还有下一页
|
|
222
|
+
*
|
|
223
|
+
*/
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* 分页数据结果
|
|
228
|
+
* @typedef {Object} PageDataResult
|
|
229
|
+
* @property {Pagination} pagination - 最新分页信息
|
|
230
|
+
* @property {Object[][]} data - 分页数据集合
|
|
231
|
+
*/
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 分页查询结果
|
|
237
|
+
* @typedef {callback} FunUpdate
|
|
238
|
+
* @property {Pagination} [pagination] - 最新分页信息
|
|
239
|
+
* @property {boolean} [clear] - 是否清空数据
|
|
240
|
+
* @returns {Promise<PageDataResult>} 最新的分页数据结果
|
|
241
|
+
*/
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* usePagination的返回对象
|
|
246
|
+
* @typedef {Object} UsePaginationResult
|
|
247
|
+
* @property {Object[][]} data - 分页数据集合
|
|
248
|
+
* @property {Pagination} paginnation - 分页信息
|
|
249
|
+
* @property {FunUpdate} update - 查询方法
|
|
250
|
+
* @property {FunUpdate} next - 获取下一页数据
|
|
251
|
+
* @property {function} addListener - 监听事件 (type='beforeService'|'afterService',fn:FunListener)=>object
|
|
252
|
+
* @property {function} removeListener - 移除监听事件 (type,fn:FunListener)=>void
|
|
253
|
+
*/
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* 事件监听方法
|
|
258
|
+
* @typedef {callback} FunListener
|
|
259
|
+
* @property {Object} params - 被拦截的数据对象
|
|
260
|
+
* @returns {Object|Promise<Object>} 处理完毕的数据对象或者Promise
|
|
261
|
+
*/
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
export default usePagination;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @module usePaginationWithForm
|
|
4
|
+
*/
|
|
5
|
+
import { useState, useMemo, useEffect, useRef } from 'react';
|
|
6
|
+
import usePagination from '../usePagination';
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 支持Antd-Form的usePagination
|
|
11
|
+
* 作用是在查询接口前自动获取form表单内的字段,并作为接口查询参数进行查询
|
|
12
|
+
* 使用方法及参数字段参考 [usePagination]{@link usePagination.md}
|
|
13
|
+
*
|
|
14
|
+
* @param {Object} props
|
|
15
|
+
* @param {Object} props.form - Form表单的ref,在接口调用前会校验获取form表单内的数据,提交到接口参数内查询
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
const [form] = Form.useForm();
|
|
19
|
+
const page = usePaginationWithForm({
|
|
20
|
+
service:GET_LIST,
|
|
21
|
+
pagination:{pageSize:10},
|
|
22
|
+
form:form
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
return(
|
|
26
|
+
<Form form={form} style={{width:'600px'}} layout="inline">
|
|
27
|
+
<Form.Item label='关键字' name='keyword' rules={[
|
|
28
|
+
{
|
|
29
|
+
max:5,
|
|
30
|
+
message:'最多5个字符'
|
|
31
|
+
}
|
|
32
|
+
]}>
|
|
33
|
+
<Input />
|
|
34
|
+
</Form.Item>
|
|
35
|
+
<Button onClick={onSearch} type='primary'>查询</Button>
|
|
36
|
+
<Button onClick={onReset} type='primary'>重置</Button>
|
|
37
|
+
</Form>
|
|
38
|
+
)
|
|
39
|
+
*/
|
|
40
|
+
const usePaginationWithForm = (props) => {
|
|
41
|
+
const {service,pagination}= props;
|
|
42
|
+
const [form] = useState(props.form);
|
|
43
|
+
const page = usePagination({service,pagination})
|
|
44
|
+
|
|
45
|
+
useEffect(()=>{
|
|
46
|
+
const fnFeforeService = async (params)=>{
|
|
47
|
+
// 假如你这里需要变更接口字段名称的话
|
|
48
|
+
const values = await getSearchValue();
|
|
49
|
+
if(!values)return;
|
|
50
|
+
params= {...params,...values}
|
|
51
|
+
return params;
|
|
52
|
+
};
|
|
53
|
+
page.addListener('beforeService',fnFeforeService);
|
|
54
|
+
return ()=>{
|
|
55
|
+
page.removeListener('beforeService',fnFeforeService);
|
|
56
|
+
}
|
|
57
|
+
},[])
|
|
58
|
+
|
|
59
|
+
const getSearchValue = async () => {
|
|
60
|
+
try {
|
|
61
|
+
let value = {};
|
|
62
|
+
if (form) {
|
|
63
|
+
value = await form.validateFields();
|
|
64
|
+
}
|
|
65
|
+
return value;
|
|
66
|
+
} catch (ex) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
const reset = () => {
|
|
74
|
+
if(form){
|
|
75
|
+
form.resetFields();
|
|
76
|
+
}
|
|
77
|
+
page.reset();
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
...page,
|
|
82
|
+
reset,
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export default usePaginationWithForm
|
package/src/useSwitch/index.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useSwitch
|
|
3
|
+
* @module useSwitch
|
|
4
|
+
*/
|
|
1
5
|
import { useState, useMemo, useRef, useEffect } from 'react';
|
|
2
6
|
|
|
3
7
|
|
|
8
|
+
|
|
4
9
|
/**
|
|
5
10
|
* 控制开关
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* count: number,当前计数器
|
|
13
|
-
* }
|
|
11
|
+
*
|
|
12
|
+
* [Demo-CodeSandbox]{@link https://codesandbox.io/s/useswitch-qd7dr?file=/index.js}
|
|
13
|
+
*
|
|
14
|
+
* @param {boolean} [props=false] - 默认的开关状态
|
|
15
|
+
* @return {UseSwitchResult}
|
|
16
|
+
*
|
|
14
17
|
*
|
|
15
18
|
*/
|
|
16
19
|
const useSwitch = (props=false) => {
|
|
@@ -62,4 +65,16 @@ const useSwitch = (props=false) => {
|
|
|
62
65
|
return {state:value,count,...actions};
|
|
63
66
|
};
|
|
64
67
|
|
|
68
|
+
/**
|
|
69
|
+
* useSwitchHookResult
|
|
70
|
+
* @typedef {Object} UseSwitchResult
|
|
71
|
+
* @property {boolean} state - 当前开关状态
|
|
72
|
+
* @property {number} count - 当前开关计数器
|
|
73
|
+
* @property {function} open - 切换至开模式 (force?:boolean)=>void
|
|
74
|
+
* @property {function} close - 切换至关模式 (force?:boolean)=>void
|
|
75
|
+
* @property {function} toggle - 切换至反向模式 ()=>void
|
|
76
|
+
*
|
|
77
|
+
*/
|
|
78
|
+
|
|
79
|
+
|
|
65
80
|
export default useSwitch;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React,{ useState, useMemo, useEffect, useRef } from 'react';
|
|
2
|
+
import ReactDOM from 'react-dom';
|
|
3
|
+
import useCounter from '@/useCounter';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
const Index=()=>{
|
|
7
|
+
const [count,add] = useCounter();
|
|
8
|
+
return (
|
|
9
|
+
<section>
|
|
10
|
+
<h2>计数器:{count}</h2>
|
|
11
|
+
<button onClick={add}>触发增加计数器</button>
|
|
12
|
+
</section>
|
|
13
|
+
)
|
|
14
|
+
}
|
|
15
|
+
ReactDOM.render(<Index />, document.getElementById('main-view'));
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import React,{ useState, useMemo, useEffect, useRef } from 'react';
|
|
2
|
+
import ReactDOM, { render } from 'react-dom';
|
|
3
|
+
import usePagination from '@/usePagination';
|
|
4
|
+
import {Pagination,Button,Input,Table} from 'antd';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
const GET_LIST=(params)=>{
|
|
9
|
+
const list=[]
|
|
10
|
+
const total=200;
|
|
11
|
+
for(let i=0;i<total;i++){
|
|
12
|
+
list.push(`数据${i+1}`)
|
|
13
|
+
}
|
|
14
|
+
const {current,pageSize,...others}=params;
|
|
15
|
+
const startIdx = (current-1) * pageSize;
|
|
16
|
+
let data = list.splice(startIdx,pageSize);
|
|
17
|
+
data=data.map((item,idx)=>{
|
|
18
|
+
return {id:`第${current}页-${item}`, value:`第${current}页-${item}`,params:others}
|
|
19
|
+
})
|
|
20
|
+
return Promise.resolve({
|
|
21
|
+
code:0,
|
|
22
|
+
data,
|
|
23
|
+
page:{
|
|
24
|
+
current:current,
|
|
25
|
+
pageSize:pageSize,
|
|
26
|
+
total,
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const Index=()=>{
|
|
32
|
+
const page = usePagination({
|
|
33
|
+
service:GET_LIST,
|
|
34
|
+
pagination:{pageSize:10}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
useEffect(()=>{
|
|
39
|
+
const fnFeforeService = (params)=>{
|
|
40
|
+
// 假如你这里需要变更接口字段名称的话
|
|
41
|
+
params.beforeService='beforeService';
|
|
42
|
+
return params;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const fnAfterService = (response)=>{
|
|
46
|
+
// 这里你可以翻译及二次处理你的接口字段
|
|
47
|
+
response.message='afterService';
|
|
48
|
+
return response;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
page.addListener('beforeService',fnFeforeService);
|
|
52
|
+
page.addListener('afterService',fnAfterService);
|
|
53
|
+
page.update()
|
|
54
|
+
return ()=>{
|
|
55
|
+
page.removeListener('beforeService',fnFeforeService);
|
|
56
|
+
page.removeListener('afterService',fnAfterService)
|
|
57
|
+
}
|
|
58
|
+
},[])
|
|
59
|
+
|
|
60
|
+
const onReset=()=>{page.reset();}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
const onPageChange=(current,pageSize)=>{
|
|
64
|
+
page.update({pagination:{current,pageSize}})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const onNext=()=>{page.nextPage();}
|
|
68
|
+
|
|
69
|
+
const columns=[{
|
|
70
|
+
dataIndex:'value',
|
|
71
|
+
title:'数据值',
|
|
72
|
+
render:(_,record,idx)=>{
|
|
73
|
+
return `[${idx}]${record.value}`;
|
|
74
|
+
}
|
|
75
|
+
},{
|
|
76
|
+
dataIndex:'params',
|
|
77
|
+
title:'入参值',
|
|
78
|
+
render:(_,record,idx)=>{
|
|
79
|
+
return record.params?JSON.stringify(record.params):'-';
|
|
80
|
+
}
|
|
81
|
+
}];
|
|
82
|
+
|
|
83
|
+
const getDataSource=()=>{
|
|
84
|
+
let req=[];
|
|
85
|
+
if(page?.data && page?.data?.length>0){
|
|
86
|
+
for(let i=0;i<page.data.length;i++){
|
|
87
|
+
req= [...req,...page.data[i]];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return req;
|
|
91
|
+
}
|
|
92
|
+
return (
|
|
93
|
+
<section style={{padding:'12px'}}>
|
|
94
|
+
<section>
|
|
95
|
+
<Button onClick={onNext} type='primary'>加载下一页</Button>
|
|
96
|
+
<Button onClick={onReset} type='primary'>重置</Button>
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
<Table
|
|
100
|
+
rowKey={'id'}
|
|
101
|
+
loading={!page?.data?.length>0}
|
|
102
|
+
columns={columns}
|
|
103
|
+
dataSource={getDataSource()}
|
|
104
|
+
pagination={false}
|
|
105
|
+
/>
|
|
106
|
+
|
|
107
|
+
<Pagination
|
|
108
|
+
current={page?.pagination?.current}
|
|
109
|
+
pageSize={page?.pagination?.pageSize}
|
|
110
|
+
onChange={onPageChange}
|
|
111
|
+
total={page?.pagination?.total}
|
|
112
|
+
/>
|
|
113
|
+
|
|
114
|
+
</section>
|
|
115
|
+
|
|
116
|
+
</section>
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
ReactDOM.render(<Index />, document.getElementById('main-view'));
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import ReactDOM from 'react-dom';
|
|
3
|
+
import {Form,Button,Input,Table} from 'antd';
|
|
4
|
+
|
|
5
|
+
import usePaginationWithForm from '@/usePaginationWithForm';
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
const GET_LIST=(params)=>{
|
|
11
|
+
const list=[]
|
|
12
|
+
const total=200;
|
|
13
|
+
for(let i=0;i<total;i++){
|
|
14
|
+
list.push(`数据${i+1}`)
|
|
15
|
+
}
|
|
16
|
+
const {current,pageSize,...others}=params;
|
|
17
|
+
const startIdx = (current-1) * pageSize;
|
|
18
|
+
let data = list.splice(startIdx,pageSize);
|
|
19
|
+
data=data.map((item,idx)=>{
|
|
20
|
+
return {id:''+idx, value:`第${current}页-${item}`,params:others}
|
|
21
|
+
})
|
|
22
|
+
return Promise.resolve({
|
|
23
|
+
code:0,
|
|
24
|
+
data,
|
|
25
|
+
page:{
|
|
26
|
+
current:current,
|
|
27
|
+
pageSize:pageSize,
|
|
28
|
+
total,
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const Index=()=>{
|
|
34
|
+
|
|
35
|
+
const [form] = Form.useForm();
|
|
36
|
+
const page = usePaginationWithForm({
|
|
37
|
+
service:GET_LIST,
|
|
38
|
+
pagination:{pageSize:10},
|
|
39
|
+
form:form
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
const onSearch=()=>{
|
|
44
|
+
page.update();
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const onReset=()=>{page.reset();}
|
|
48
|
+
|
|
49
|
+
const onPageChange=(current,pageSize)=>{
|
|
50
|
+
page.update({pagination:{current,pageSize}})
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const columns=[{
|
|
54
|
+
dataIndex:'value',
|
|
55
|
+
title:'数据值',
|
|
56
|
+
render:(_,record,idx)=>{
|
|
57
|
+
return `[${idx}]${record.value}`;
|
|
58
|
+
}
|
|
59
|
+
},{
|
|
60
|
+
dataIndex:'params',
|
|
61
|
+
title:'入参值',
|
|
62
|
+
render:(_,record,idx)=>{
|
|
63
|
+
return record.params?JSON.stringify(record.params):'-';
|
|
64
|
+
}
|
|
65
|
+
}];
|
|
66
|
+
return (
|
|
67
|
+
<section style={{padding:'12px'}}>
|
|
68
|
+
<section>
|
|
69
|
+
<Form form={form} style={{width:'600px'}} layout="inline">
|
|
70
|
+
<Form.Item label='关键字' name='keyword' rules={[
|
|
71
|
+
{
|
|
72
|
+
max:5,
|
|
73
|
+
message:'最多5个字符'
|
|
74
|
+
}
|
|
75
|
+
]}>
|
|
76
|
+
<Input />
|
|
77
|
+
</Form.Item>
|
|
78
|
+
<Button onClick={onSearch} type='primary'>查询</Button>
|
|
79
|
+
<Button onClick={onReset} type='primary'>重置</Button>
|
|
80
|
+
</Form>
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
<Table
|
|
84
|
+
rowKey={'id'}
|
|
85
|
+
loading={!page?.data?.length>0}
|
|
86
|
+
columns={columns}
|
|
87
|
+
dataSource={page?.data? page?.data[page?.pagination?.current-1]:[]}
|
|
88
|
+
pagination={{
|
|
89
|
+
current:page?.pagination?.current,
|
|
90
|
+
pageSize:page?.pagination?.pageSize,
|
|
91
|
+
total:page?.pagination?.total,
|
|
92
|
+
onChange:onPageChange
|
|
93
|
+
}}
|
|
94
|
+
/>
|
|
95
|
+
|
|
96
|
+
</section>
|
|
97
|
+
|
|
98
|
+
</section>
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
ReactDOM.render(<Index />, document.getElementById('main-view'));
|
package/test/useSwitch/index.jsx
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import React,{ useState, useMemo, useEffect, useRef } from 'react';
|
|
2
2
|
import ReactDOM from 'react-dom';
|
|
3
|
-
import
|
|
3
|
+
import useSwitch from '@/useSwitch';
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
const Index=()=>{
|
|
7
7
|
const loading = useSwitch(false);
|
|
8
|
-
const clip = useClipboard({onSuccess:()=>{alert('已复制到剪贴板')}});
|
|
9
8
|
return (
|
|
10
9
|
<section>
|
|
11
|
-
<h1
|
|
12
|
-
clip('4433')
|
|
13
|
-
}}>当前状态:{loading.state?"打开":"关闭"}</h1>
|
|
10
|
+
<h1>当前状态:{loading.state?"打开":"关闭"}</h1>
|
|
14
11
|
<h2>计数器:{loading.count}</h2>
|
|
15
12
|
<button onClick={()=>{loading.toggle();}}>切换</button>
|
|
16
13
|
<button onClick={()=>{loading.open();}}>打开</button>
|
package/src/.DS_Store
DELETED
|
Binary file
|