@base-stone/hooks 1.0.6 → 1.0.8

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