@base-stone/hooks 1.0.2 → 1.0.4

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