@base-stone/hooks 1.0.1 → 1.0.3

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.
@@ -78,11 +78,18 @@ export declare interface QueryParamsData {
78
78
  [key: string]: any;
79
79
  }
80
80
 
81
+ declare type QueryParamsData_2 = Record<string, any>;
82
+
81
83
  declare interface QueryResponse {
82
84
  status: string;
83
85
  data: Record<string, any>[];
84
86
  }
85
87
 
88
+ declare interface QueryResponse_2<T> {
89
+ status: string;
90
+ data: T;
91
+ }
92
+
86
93
  declare interface RequestFn {
87
94
  (val?: unknown): Promise<{
88
95
  status: string;
@@ -90,15 +97,25 @@ declare interface RequestFn {
90
97
  }>;
91
98
  }
92
99
 
100
+ declare interface RequestQueryOptions<T, P = Record<string, any>> {
101
+ queryFn: (data: QueryParamsData_2) => Promise<QueryResponse_2<T>>;
102
+ initialValue?: T;
103
+ params?: P;
104
+ }
105
+
106
+ declare interface RequestQueryResult<T> {
107
+ data: T;
108
+ loading: boolean;
109
+ refresh: () => void;
110
+ }
111
+
93
112
  declare interface SelectConfig<K extends string> {
94
113
  queryFn: (data: Record<string, any>) => Promise<QueryResponse>;
95
- options: {
96
- params?: Record<string, any>;
97
- selectKey: K;
98
- fieldNames?: {
99
- label: string;
100
- value: string;
101
- };
114
+ params?: Record<string, any>;
115
+ selectKey: K;
116
+ fieldNames?: {
117
+ label: string;
118
+ value: string;
102
119
  };
103
120
  }
104
121
 
@@ -123,10 +140,8 @@ declare interface SuccessFn {
123
140
 
124
141
  declare interface TableOptions<T, P = Record<string, any>> {
125
142
  queryFn: (data: QueryParamsData) => Promise<TableResponse<T>>;
126
- options?: {
127
- params: P;
128
- rowSelection: boolean;
129
- };
143
+ params?: P;
144
+ rowSelection?: boolean;
130
145
  }
131
146
 
132
147
  export declare interface TableProps<T> {
@@ -176,8 +191,10 @@ export declare function useFormSubmit(requestFn: RequestFn, successFn: SuccessFn
176
191
 
177
192
  export declare function useMemoizedFn<T extends noop>(fn: T): PickFunction<T>;
178
193
 
179
- export declare function useSelectOptions<T extends string | number = string | number, K extends string = string>({ queryFn, options }: SelectConfig<K>): SelectOptionsResult<K, T>;
194
+ export declare function useRequestQuery<T, P = Record<string, any>>({ queryFn, params, initialValue }: RequestQueryOptions<T, P>): RequestQueryResult<T>;
195
+
196
+ export declare function useSelectOptions<T extends string | number = string | number, K extends string = string>({ queryFn, params, selectKey, fieldNames }: SelectConfig<K>): SelectOptionsResult<K, T>;
180
197
 
181
- export declare function useTableList<T extends Record<string, any> = Record<string, any>>({ queryFn, options }: TableOptions<T>): TableResult<T>;
198
+ export declare function useTableList<T extends Record<string, any> = Record<string, any>>({ queryFn, params: initParams, rowSelection }: TableOptions<T>): TableResult<T>;
182
199
 
183
200
  export { }
@@ -1,259 +1,273 @@
1
- import { useRef as w, useMemo as L, useState as y, useEffect as D, useId as U, useCallback as W } from "react";
2
- import { Empty as A, App as X } from "antd";
3
- import { create as K } from "zustand";
4
- var k = { exports: {} }, b = {};
5
- var j;
6
- function ee() {
7
- if (j) return b;
8
- j = 1;
9
- var s = /* @__PURE__ */ Symbol.for("react.transitional.element"), a = /* @__PURE__ */ Symbol.for("react.fragment");
10
- function o(t, r, n) {
11
- var u = null;
12
- if (n !== void 0 && (u = "" + n), r.key !== void 0 && (u = "" + r.key), "key" in r) {
13
- n = {};
14
- for (var c in r)
15
- c !== "key" && (n[c] = r[c]);
16
- } else n = r;
17
- return r = n.ref, {
18
- $$typeof: s,
19
- type: t,
20
- key: u,
21
- ref: r !== void 0 ? r : null,
22
- props: n
1
+ import { useRef as x, useMemo as F, useState as v, useEffect as D, useId as B, useCallback as U } from "react";
2
+ import { Empty as N, App as W } from "antd";
3
+ import { create as X } from "zustand";
4
+ var P = { exports: {} }, y = {};
5
+ var q;
6
+ function K() {
7
+ if (q) return y;
8
+ q = 1;
9
+ var a = /* @__PURE__ */ Symbol.for("react.transitional.element"), o = /* @__PURE__ */ Symbol.for("react.fragment");
10
+ function t(e, n, s) {
11
+ var p = null;
12
+ if (s !== void 0 && (p = "" + s), n.key !== void 0 && (p = "" + n.key), "key" in n) {
13
+ s = {};
14
+ for (var d in n)
15
+ d !== "key" && (s[d] = n[d]);
16
+ } else s = n;
17
+ return n = s.ref, {
18
+ $$typeof: a,
19
+ type: e,
20
+ key: p,
21
+ ref: n !== void 0 ? n : null,
22
+ props: s
23
23
  };
24
24
  }
25
- return b.Fragment = a, b.jsx = o, b.jsxs = o, b;
25
+ return y.Fragment = o, y.jsx = t, y.jsxs = t, y;
26
26
  }
27
- var F;
28
- function te() {
29
- return F || (F = 1, k.exports = ee()), k.exports;
27
+ var A;
28
+ function ee() {
29
+ return A || (A = 1, P.exports = K()), P.exports;
30
30
  }
31
- var oe = te();
32
- function M(s) {
33
- const a = w(s);
34
- a.current = L(() => s, [s]);
35
- const o = w(void 0);
36
- return o.current || (o.current = function(...t) {
37
- return a.current.apply(this, t);
38
- }), o.current;
31
+ var te = ee();
32
+ function M(a) {
33
+ const o = x(a);
34
+ o.current = F(() => a, [a]);
35
+ const t = x(void 0);
36
+ return t.current || (t.current = function(...e) {
37
+ return o.current.apply(this, e);
38
+ }), t.current;
39
39
  }
40
- const x = {
40
+ const R = {
41
41
  sortField: ["orderType", "orderField"],
42
42
  sortOrder: ["ASC", "DESC"],
43
43
  pageSize: 10
44
44
  };
45
- function ie(s) {
46
- Object.keys(s).forEach((a) => {
47
- x[a] = s[a];
45
+ function ne(a) {
46
+ Object.keys(a).forEach((o) => {
47
+ R[o] = a[o];
48
48
  });
49
49
  }
50
- function le({
51
- queryFn: s,
52
- options: a
50
+ function ie({
51
+ queryFn: a,
52
+ params: o,
53
+ rowSelection: t
53
54
  }) {
54
- const { rowSelection: o, params: t } = a || {}, r = x.pageSize, [n, u] = y({
55
+ const e = R.pageSize, [n, s] = v({
55
56
  pagination: {
56
57
  showSizeChanger: !0,
57
58
  showQuickJumper: !0,
58
59
  total: 0,
59
- pageSize: r,
60
+ pageSize: e,
60
61
  current: 1
61
62
  },
62
63
  list: [],
63
64
  queryParams: {
64
65
  pageNo: 1,
65
- pageSize: r,
66
- ...t
66
+ pageSize: e,
67
+ ...o
67
68
  }
68
- }), { pagination: c, list: p, queryParams: e } = n, { pageNo: d, pageSize: f } = e, [i, S] = y(!0), g = w(e), [v, E] = y([]), C = L(() => {
69
- if (o)
69
+ }), { pagination: p, list: d, queryParams: l } = n, { pageNo: r, pageSize: c } = l, [g, i] = v(!0), S = x(l), [f, b] = v([]), E = F(() => {
70
+ if (t)
70
71
  return {
71
- selectedRowKeys: v,
72
- onChange: (l) => E(l)
72
+ selectedRowKeys: f,
73
+ onChange: (u) => b(u)
73
74
  };
74
- }, [o, v]), z = M(
75
- (l) => `共 ${l} 条记录 第 ${d}/${Math.ceil(l / f)} 页 `
76
- ), m = async (l) => {
77
- const { pageNo: O } = l;
78
- S(!0);
79
- const h = { ...t, pageSize: f, ...l };
80
- l.pageNo === void 0 && (h.pageNo = 1), l.pageSize === void 0 && (h.pageSize = f);
81
- const { data: $ } = await s(h), { list: P = [], totalCount: T = 0 } = $ || {};
82
- o && E([]), u({
83
- list: P,
75
+ }, [t, f]), z = M(
76
+ (u) => `共 ${u} 条记录 第 ${r}/${Math.ceil(u / c)} 页 `
77
+ ), m = async (u) => {
78
+ const { pageNo: O } = u;
79
+ i(!0);
80
+ const h = { ...o, pageSize: c, ...u };
81
+ u.pageNo === void 0 && (h.pageNo = 1), u.pageSize === void 0 && (h.pageSize = c);
82
+ const { data: $ } = await a(h), { list: T = [], totalCount: w = 0 } = $ || {};
83
+ t && b([]), s({
84
+ list: T,
84
85
  queryParams: h,
85
86
  pagination: {
86
- ...c,
87
+ ...p,
87
88
  current: O,
88
89
  pageSize: h.pageSize,
89
- total: T
90
+ total: w
90
91
  }
91
- }), S(!1);
92
- }, N = (l) => {
93
- m({ ...e, ...l, pageNo: 1 });
94
- }, q = (l) => {
95
- m({ ...e, ...l });
96
- }, J = (l) => {
97
- m({ ...l, pageSize: f, pageNo: 1 });
98
- }, H = (l, O, h, $) => {
99
- const { action: P } = $;
100
- if (["paginate", "sort"].includes(P)) {
101
- const { current: T, pageSize: I } = l, { field: G, order: _ } = h, [V, Q] = x.sortField, [Y, Z] = x.sortOrder, B = {
102
- ...e,
103
- [V]: _ ? _ === "ascend" ? Y : Z : void 0,
104
- [Q]: G,
105
- pageNo: T,
106
- pageSize: I
92
+ }), i(!1);
93
+ }, k = (u) => {
94
+ m({ ...l, ...u, pageNo: 1 });
95
+ }, L = (u) => {
96
+ m({ ...l, ...u });
97
+ }, j = (u) => {
98
+ m({ ...u, pageSize: c, pageNo: 1 });
99
+ }, J = (u, O, h, $) => {
100
+ const { action: T } = $;
101
+ if (["paginate", "sort"].includes(T)) {
102
+ const { current: w, pageSize: H } = u, { field: I, order: _ } = h, [G, Q] = R.sortField, [V, Y] = R.sortOrder, Z = {
103
+ ...l,
104
+ [G]: _ ? _ === "ascend" ? V : Y : void 0,
105
+ [Q]: I,
106
+ pageNo: w,
107
+ pageSize: H
107
108
  };
108
- m(B);
109
+ m(Z);
109
110
  }
110
111
  };
111
112
  return D(() => {
112
- N(g.current);
113
+ k(S.current);
113
114
  }, []), {
114
- queryParams: e,
115
- search: M(N),
116
- refresh: M(q),
117
- reset: M(J),
118
- selectedRowKeys: v,
115
+ queryParams: l,
116
+ search: M(k),
117
+ refresh: M(L),
118
+ reset: M(j),
119
+ selectedRowKeys: f,
119
120
  tableProps: {
120
121
  bordered: !0,
121
122
  size: "middle",
122
123
  sticky: !0,
123
- rowSelection: C,
124
- loading: i,
125
- dataSource: p,
126
- pagination: { ...c, showTotal: z },
127
- onChange: M(H),
124
+ rowSelection: E,
125
+ loading: g,
126
+ dataSource: d,
127
+ pagination: { ...p, showTotal: z },
128
+ onChange: M(J),
128
129
  locale: {
129
- emptyText: i ? "" : /* @__PURE__ */ oe.jsx(A, { image: A.PRESENTED_IMAGE_SIMPLE })
130
+ emptyText: g ? "" : /* @__PURE__ */ te.jsx(N, { image: N.PRESENTED_IMAGE_SIMPLE })
130
131
  }
131
132
  }
132
133
  };
133
134
  }
134
- const R = K((s) => ({
135
+ const C = X((a) => ({
135
136
  modals: {},
136
- toggleModal: (a, o) => s((t) => {
137
- const n = t.modals[a]?.visible || !1;
137
+ toggleModal: (o, t) => a((e) => {
138
+ const s = e.modals[o]?.visible || !1;
138
139
  return {
139
140
  modals: {
140
- ...t.modals,
141
- [a]: {
142
- visible: !n,
143
- data: n ? {} : o || {}
141
+ ...e.modals,
142
+ [o]: {
143
+ visible: !s,
144
+ data: s ? {} : t || {}
144
145
  }
145
146
  }
146
147
  };
147
148
  }),
148
- setModal: (a, o, t) => s((r) => ({
149
+ setModal: (o, t, e) => a((n) => ({
149
150
  modals: {
150
- ...r.modals,
151
- [a]: {
152
- visible: o,
153
- data: o ? t || {} : {}
151
+ ...n.modals,
152
+ [o]: {
153
+ visible: t,
154
+ data: t ? e || {} : {}
154
155
  }
155
156
  }
156
157
  })),
157
- clearModals: (a) => s((o) => {
158
- const t = { ...o.modals };
159
- return a.forEach((r) => {
160
- delete t[r];
161
- }), { modals: t };
158
+ clearModals: (o) => a((t) => {
159
+ const e = { ...t.modals };
160
+ return o.forEach((n) => {
161
+ delete e[n];
162
+ }), { modals: e };
162
163
  })
163
164
  }));
164
- function ce(s) {
165
- const a = R((e) => e.modals), o = R((e) => e.toggleModal), t = R((e) => e.setModal), r = R((e) => e.clearModals), n = U(), u = w([]), c = Object.keys(s);
166
- D(() => (u.current = c.map(
167
- (e) => `${n}-${e}`
165
+ function le(a) {
166
+ const o = C((r) => r.modals), t = C((r) => r.toggleModal), e = C((r) => r.setModal), n = C((r) => r.clearModals), s = B(), p = x([]), d = Object.keys(a);
167
+ D(() => (p.current = d.map(
168
+ (r) => `${s}-${r}`
168
169
  ), () => {
169
- r(u.current);
170
+ n(p.current);
170
171
  }), []);
171
- const p = {};
172
- return c.forEach((e) => {
173
- const d = `${n}-${e}`, f = a[d] ?? { visible: !1, data: {} }, i = s[e], S = typeof i.title == "function" ? i.title(f.data) : i.title;
174
- p[`${e}Modal`] = {
175
- visible: f.visible,
176
- data: f.data,
172
+ const l = {};
173
+ return d.forEach((r) => {
174
+ const c = `${s}-${r}`, g = o[c] ?? { visible: !1, data: {} }, i = a[r], S = typeof i.title == "function" ? i.title(g.data) : i.title;
175
+ l[`${r}Modal`] = {
176
+ visible: g.visible,
177
+ data: g.data,
177
178
  modalProps: {
178
179
  width: i.width,
179
180
  title: S,
180
- open: f.visible,
181
+ open: g.visible,
181
182
  maskClosable: i.maskClosable ?? !1,
182
183
  centered: i.centered ?? !0,
183
184
  destroyOnHidden: i.destroyOnHidden ?? !0,
184
- onCancel: i.onCancel ?? (() => t(d, !1))
185
+ onCancel: i.onCancel ?? (() => e(c, !1))
185
186
  },
186
187
  drawerProps: {
187
188
  size: i.width,
188
189
  title: S,
189
- open: f.visible,
190
+ open: g.visible,
190
191
  maskClosable: i.maskClosable ?? !0,
191
192
  destroyOnHidden: i.destroyOnHidden ?? !0,
192
193
  placement: i.placement ?? "right",
193
- onClose: i.onClose ?? (() => t(d, !1))
194
+ onClose: i.onClose ?? (() => e(c, !1))
194
195
  },
195
- toggle: (g) => o(d, g),
196
- open: (g) => t(d, !0, g),
197
- close: () => t(d, !1)
196
+ toggle: (f) => t(c, f),
197
+ open: (f) => e(c, !0, f),
198
+ close: () => e(c, !1)
198
199
  };
199
- }), p.toggle = (e, d) => {
200
- o(`${n}-${e}`, d);
201
- }, p.open = (e, d) => {
202
- t(`${n}-${e}`, !0, d);
203
- }, p.close = (e) => {
204
- t(`${n}-${e}`, !1);
205
- }, p;
200
+ }), l.toggle = (r, c) => {
201
+ t(`${s}-${r}`, c);
202
+ }, l.open = (r, c) => {
203
+ e(`${s}-${r}`, !0, c);
204
+ }, l.close = (r) => {
205
+ e(`${s}-${r}`, !1);
206
+ }, l;
206
207
  }
207
- const ae = (s, a = "value", o = "label") => new Map(s.map((t) => [t[a], t[o]]));
208
- function de({ queryFn: s, options: a }) {
209
- const [o, t] = y({
208
+ const oe = (a, o = "value", t = "label") => new Map(a.map((e) => [e[o], e[t]]));
209
+ function ce({ queryFn: a, params: o, selectKey: t, fieldNames: e }) {
210
+ const [n, s] = v({
210
211
  list: [],
211
212
  mapData: /* @__PURE__ */ new Map()
212
- }), [r, n] = y(!0), { params: u, selectKey: c, fieldNames: p } = a, e = async () => {
213
- n(!0);
214
- const { data: S } = await s(u), g = S || [];
215
- if (p) {
216
- const { label: v, value: E } = p, C = g.map((m) => ({
213
+ }), [p, d] = v(!0), l = async () => {
214
+ d(!0);
215
+ const { data: i } = await a(o), S = i || [];
216
+ if (e) {
217
+ const { label: f, value: b } = e, E = S.map((m) => ({
217
218
  data: m,
218
- label: m[v],
219
- value: m[E]
220
- })), z = ae(C);
221
- t((m) => ({ ...m, options: C, mapData: z }));
219
+ label: m[f],
220
+ value: m[b]
221
+ })), z = oe(E);
222
+ s((m) => ({ ...m, options: E, mapData: z }));
222
223
  } else
223
- t((v) => ({ ...v, options: g }));
224
- n(!1);
225
- }, d = M(void 0);
224
+ s((f) => ({ ...f, options: S }));
225
+ d(!1);
226
+ }, r = M(void 0);
226
227
  D(() => {
227
- e();
228
+ l();
228
229
  }, []);
229
- const { list: f, mapData: i } = o;
230
+ const { list: c, mapData: g } = n;
230
231
  return {
231
- loading: r,
232
- refresh: d,
233
- [`${c}Options`]: f,
234
- [`${c}Map`]: i
232
+ loading: p,
233
+ refresh: r,
234
+ [`${t}Options`]: c,
235
+ [`${t}Map`]: g
235
236
  };
236
237
  }
237
- function ue(s, a) {
238
- const [o, t] = y(!1), { message: r } = X.useApp(), n = W(async (u) => {
238
+ function ue(a, o) {
239
+ const [t, e] = v(!1), { message: n } = W.useApp(), s = U(async (p) => {
239
240
  try {
240
- t(!0);
241
- const c = await s(u), { status: p, info: e } = c;
242
- p == "success" && (a(c), r.success(e));
241
+ e(!0);
242
+ const d = await a(p), { status: l, info: r } = d;
243
+ l == "success" && (o(d), n.success(r));
243
244
  } catch {
244
245
  }
245
- t(!1);
246
- }, [s, a, r]);
246
+ e(!1);
247
+ }, [a, o, n]);
248
+ return {
249
+ loading: t,
250
+ submit: s
251
+ };
252
+ }
253
+ function de({
254
+ queryFn: a,
255
+ params: o,
256
+ initialValue: t
257
+ }) {
258
+ const [e, n] = v(t), [s, p] = v(!1);
247
259
  return {
248
- loading: o,
249
- submit: n
260
+ data: e,
261
+ loading: s,
262
+ refresh: M(void 0)
250
263
  };
251
264
  }
252
265
  export {
253
- ie as configureTableOption,
254
- ce as useCreateModal,
266
+ ne as configureTableOption,
267
+ le as useCreateModal,
255
268
  ue as useFormSubmit,
256
269
  M as useMemoizedFn,
257
- de as useSelectOptions,
258
- le as useTableList
270
+ de as useRequestQuery,
271
+ ce as useSelectOptions,
272
+ ie as useTableList
259
273
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@base-stone/hooks",
3
3
  "license": "MIT",
4
- "version": "1.0.1",
4
+ "version": "1.0.3",
5
5
  "author": {
6
6
  "name": "leafront",
7
7
  "email": "leafront@126.com"