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.
@@ -1,165 +0,0 @@
1
- import { useState, useMemo, useEffect, useRef } from 'react';
2
-
3
-
4
- const useFormTableSearch = (props) => {
5
- const [search] = useState(props.initSearch || {});
6
- const [formRef] = useState(props.formRef);
7
- const [service] = useState(() => props.service);
8
- const [beforeSearch] = useState(() => props.beforeSearch);
9
- const [beforeService] = useState(() => props.beforeService);
10
- const [loading, setLoading] = useState(props.loading || false);
11
-
12
- const [pagination, setPagination] = useState(() => {
13
- const temp = { ...props.pagination, page: 1, pageSize: 10, total: 0, startIdx: 0 };
14
- if (props.pagination) {
15
- temp.startIdx = temp.pageSize * (temp.page - 1);
16
- }
17
- return temp;
18
- });
19
- const [updateData] = useState(() => props.updateData);
20
- const [orderInfo, setOrderInfo] = useState({});
21
-
22
- const getSearchValue = async () => {
23
- try {
24
- let value = {};
25
- if (formRef) {
26
- value = await formRef.validateFields();
27
- } else {
28
- value = search;
29
- }
30
- if (beforeSearch) {
31
- value = beforeSearch(value);
32
- }
33
- return value;
34
- } catch (ex) {
35
- return;
36
- }
37
- };
38
- const formatSearchValue = async ({ searchValue, pageValue, orderValue }) => {
39
- if (!searchValue) {
40
- searchValue = await getSearchValue();
41
- }
42
- searchValue = searchValue || search;
43
- pageValue = pageValue || pagination;
44
- orderValue = orderValue || orderInfo;
45
- const order: any[] = [];
46
- if (orderValue) {
47
- Object.keys(orderValue).map((name) => {
48
- if (orderValue[name] === 'ascend') {
49
- order.push({ [`${name}`]: 'ASC' });
50
- } else if (orderValue[name] === 'descend') {
51
- order.push({ [`${name}`]: 'DESC' });
52
- }
53
- });
54
- }
55
- const { page, pageSize } = pageValue;
56
- let params = {
57
- ...searchValue,
58
- page: {
59
- pageNum: page,
60
- pageSize: pageSize,
61
- },
62
- };
63
- if (order && order.length > 0) {
64
- params.page.orders = order;
65
- }
66
- if (beforeService) params = beforeService(params);
67
- return params;
68
- };
69
- const refresh = async ({ searchValue, pageValue, orderValue }) => {
70
- setLoading(true);
71
- searchValue = searchValue || search;
72
- pageValue = pageValue || pagination;
73
- orderValue = orderValue || orderInfo;
74
-
75
- const params = await formatSearchValue({ searchValue, pageValue, orderValue });
76
- if (!params) {
77
- setLoading(false);
78
- return;
79
- }
80
- const ret: any = await service(params);
81
-
82
- if (ret?.data?.page) {
83
- const { pageNum, pageSize: size, totalRows } = ret.data.page;
84
- setPagination({
85
- page: pageNum || 1,
86
- pageSize: size || 10,
87
- total: totalRows,
88
- startIdx: (pageNum - 1) * size || 0,
89
- });
90
- }
91
-
92
- if (updateData) updateData(ret);
93
- setLoading(false);
94
- return ret;
95
- };
96
-
97
- const onPaginationChange = async (pageInfo: any, sorterInfo?: any) => {
98
- let { page: current, pageSize }: any = pagination;
99
- let orderValue = orderInfo;
100
- if (pageInfo) {
101
- current = pageInfo.current;
102
- pageSize = pageInfo.pageSize;
103
- }
104
-
105
- if (sorterInfo) {
106
- const { field, order } = sorterInfo;
107
- if (field) {
108
- if (order) {
109
- orderInfo[field] = order;
110
- } else {
111
- delete orderInfo[field];
112
- }
113
- }
114
- orderValue = orderInfo;
115
- setOrderInfo({ ...orderInfo });
116
- }
117
-
118
- const searchValue: any = await getSearchValue();
119
-
120
- refresh({
121
- searchValue,
122
- pageValue: { page: current, pageSize },
123
- orderValue,
124
- });
125
- };
126
-
127
- const onSorter = async (sorter: any) => {
128
- const { field, order } = sorter;
129
- if (field) {
130
- if (order) {
131
- orderInfo[field] = order;
132
- } else {
133
- delete orderInfo[field];
134
- }
135
- }
136
- setOrderInfo({ ...orderInfo });
137
- console.log(`${JSON.stringify(orderInfo)}`);
138
- refresh();
139
- };
140
-
141
- const btnSearch = async () => {
142
- const value: any = await getSearchValue();
143
- if (value) {
144
- refresh({ searchValue: value });
145
- }
146
- };
147
-
148
- const btnReset = () => {
149
- formRef.resetFields();
150
- };
151
-
152
- return {
153
- onPaginationChange,
154
- pagination,
155
- btnSearch,
156
- search,
157
- refresh,
158
- btnReset,
159
- onSorter,
160
- loading,
161
- formatSearchValue,
162
- };
163
- };
164
-
165
- export default useFormTableSearch
@@ -1,15 +0,0 @@
1
-
2
- import { useEffect,useRef, useMemo } from 'react';
3
- import useSwitch from '../useSwitch';
4
-
5
- const useRefSwitch=(props=false)=>{
6
- const loading = useSwitch(props);
7
- const ref = useRef();
8
- ref.current = loading;
9
- useEffect(()=>{
10
- ref.current = loading;
11
- },[loading]);
12
-
13
- return useMemo(()=>ref,[]);
14
- }
15
- export default useRefSwitch;
package/test/.DS_Store DELETED
Binary file
@@ -1,19 +0,0 @@
1
- import React,{ useState, useMemo, useEffect, useRef } from 'react';
2
- import ReactDOM from 'react-dom';
3
- import useRefSwitch from '@/useRefSwitch';
4
-
5
- const Index=()=>{
6
- const loading = useRefSwitch(false);
7
- return (
8
- <section>
9
- <h1>当前状态:{loading.current.state?"打开":"关闭"}</h1>
10
- <h2>计数器:{loading.current.count}</h2>
11
- <button onClick={()=>{loading.current.toggle();}}>切换</button>
12
- <button onClick={()=>{loading.current.open();}}>打开</button>
13
- <button onClick={()=>{loading.current.close();}}>关闭</button>
14
- <button onClick={()=>{loading.current.open(true);}}>强制打开</button>
15
- <button onClick={()=>{loading.current.close(true);}}>强制关闭</button>
16
- </section>
17
- )
18
- }
19
- ReactDOM.render(<Index />, document.getElementById('main-view'));