@base-stone/hooks 0.9.7 → 1.0.0

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