@tmagic/form 1.2.0-beta.21 → 1.2.0-beta.23

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.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.2.0-beta.21",
2
+ "version": "1.2.0-beta.23",
3
3
  "name": "@tmagic/form",
4
4
  "type": "module",
5
5
  "sideEffects": [
@@ -36,8 +36,8 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "@element-plus/icons-vue": "^2.0.9",
39
- "@tmagic/design": "1.2.0-beta.21",
40
- "@tmagic/utils": "1.2.0-beta.21",
39
+ "@tmagic/design": "1.2.0-beta.23",
40
+ "@tmagic/utils": "1.2.0-beta.23",
41
41
  "lodash-es": "^4.17.21",
42
42
  "sortablejs": "^1.14.0",
43
43
  "vue": "^3.2.37"
@@ -99,11 +99,16 @@ const getOptions = async () => {
99
99
 
100
100
  const { config } = props;
101
101
  const { option } = config;
102
- let { body } = option;
102
+ const { root = '', totalKey = 'total' } = option;
103
+ let { body = {}, url } = option;
104
+
105
+ if (typeof url === 'function') {
106
+ url = await url(mForm, { model: props.model, formValue: mForm?.values });
107
+ }
103
108
 
104
109
  let postOptions: Record<string, any> = {
105
110
  method: option.method || 'POST',
106
- url: option.url,
111
+ url,
107
112
  cache: option.cache,
108
113
  timeout: option.timeout,
109
114
  mode: option.mode,
@@ -111,22 +116,21 @@ const getOptions = async () => {
111
116
  json: option.json || false,
112
117
  };
113
118
 
114
- if (body) {
115
- if (typeof body === 'function') {
116
- body = body(mForm, {
117
- model: props.model,
118
- formValue: mForm?.values,
119
- formValues: mForm?.values,
120
- config: props.config,
121
- }) as Record<string, any>;
122
- }
123
-
124
- body.query = query.value;
125
- body.pgSize = pgSize.value;
126
- body.pgIndex = pgIndex.value;
127
- postOptions.data = body;
119
+ if (typeof body === 'function') {
120
+ body = body(mForm, {
121
+ model: props.model,
122
+ formValue: mForm?.values,
123
+ formValues: mForm?.values,
124
+ config: props.config,
125
+ }) as Record<string, any>;
128
126
  }
129
127
 
128
+ body.query = query.value;
129
+ body.pgSize = pgSize.value;
130
+ body.pgIndex = pgIndex.value;
131
+
132
+ postOptions.data = body;
133
+
130
134
  const requestFuc = getConfig('request') as Function;
131
135
 
132
136
  if (typeof option.beforeRequest === 'function') {
@@ -151,10 +155,14 @@ const getOptions = async () => {
151
155
  });
152
156
  }
153
157
 
154
- const optionsData = res[option.root];
158
+ const optionsData = root.split('.').reduce((accumulator, currentValue: any) => accumulator[currentValue], res);
155
159
 
156
- if (res.total > 0) {
157
- total.value = res.total;
160
+ const resTotal = globalThis.parseInt(
161
+ totalKey.split('.').reduce((accumulator, currentValue: any) => accumulator[currentValue], res),
162
+ 10,
163
+ );
164
+ if (resTotal > 0) {
165
+ total.value = resTotal;
158
166
  }
159
167
 
160
168
  remoteData.value = remoteData.value.concat(optionsData);
@@ -214,6 +222,8 @@ const getInitOption = async () => {
214
222
 
215
223
  const { config } = props;
216
224
  const { option } = config;
225
+ const { root = '', initRoot = '' } = option;
226
+ let { initBody = {} } = option;
217
227
 
218
228
  let options: SelectOption[] | SelectGroupOption[] = [];
219
229
 
@@ -226,24 +236,53 @@ const getInitOption = async () => {
226
236
  url = await url(mForm, { model: props.model, formValue: mForm?.values });
227
237
  }
228
238
 
229
- const postOptions: Record<string, any> = {
239
+ if (typeof initBody === 'function') {
240
+ initBody = initBody(mForm, {
241
+ model: props.model,
242
+ formValue: mForm?.values,
243
+ formValues: mForm?.values,
244
+ config: props.config,
245
+ }) as Record<string, any>;
246
+ }
247
+
248
+ let postOptions: Record<string, any> = {
230
249
  method: option.method || 'POST',
231
250
  url,
232
251
  data: {
233
252
  id: props.model[props.name],
253
+ ...initBody,
234
254
  },
235
255
  mode: option.mode,
236
256
  headers: option.headers || {},
237
257
  json: option.json || false,
238
258
  };
259
+
260
+ if (typeof option.beforeInitRequest === 'function') {
261
+ postOptions = option.beforeInitRequest(mForm, postOptions, {
262
+ model: props.model,
263
+ formValue: mForm?.values,
264
+ });
265
+ }
266
+
239
267
  if (option.method?.toLocaleLowerCase() === 'jsonp') {
240
268
  postOptions.jsonpCallback = option.jsonpCallback || 'callback';
241
269
  }
242
270
 
243
271
  const requestFuc = getConfig('request') as Function;
244
- const res = await requestFuc(postOptions);
272
+ let res = await requestFuc(postOptions);
273
+
274
+ if (typeof option.afterRequest === 'function') {
275
+ res = option.afterRequest(mForm, res, {
276
+ model: props.model,
277
+ formValue: mForm?.values,
278
+ formValues: mForm?.values,
279
+ config: props.config,
280
+ });
281
+ }
245
282
 
246
- let initData = res[option.root];
283
+ let initData = (initRoot || root)
284
+ .split('.')
285
+ .reduce((accumulator, currentValue: any) => accumulator[currentValue], res);
247
286
  if (initData) {
248
287
  if (!Array.isArray(initData)) {
249
288
  initData = [initData];
package/src/schema.ts CHANGED
@@ -433,7 +433,7 @@ export interface SelectConfig extends FormItem, Input {
433
433
  options: SelectConfigOption[] | SelectConfigGroupOption[] | SelectOptionFunction;
434
434
  remote: true;
435
435
  option: {
436
- url: string;
436
+ url: string | ((mForm: FormState | undefined, data: { model: any; formValue: any }) => string);
437
437
  initUrl?: string | ((mForm: FormState | undefined, data: { model: any; formValue: any }) => string);
438
438
  method?: 'jsonp' | string;
439
439
  cache?: boolean;
@@ -444,13 +444,22 @@ export interface SelectConfig extends FormItem, Input {
444
444
  };
445
445
  json?: false | boolean;
446
446
  body?: Record<string, any> | RemoteSelectOptionBodyFunction;
447
+ initBody?: Record<string, any> | RemoteSelectOptionBodyFunction;
447
448
  jsonpCallback?: 'callback' | string;
448
449
  afterRequest?: RemoteSelectOptionRequestFunction;
450
+ afterInitRequest?: RemoteSelectOptionRequestFunction;
449
451
  beforeRequest?: (mForm: FormState | undefined, postOptions: Record<string, any>, data: any) => Record<string, any>;
450
- root: string;
452
+ beforeInitRequest?: (
453
+ mForm: FormState | undefined,
454
+ postOptions: Record<string, any>,
455
+ data: any,
456
+ ) => Record<string, any>;
457
+ root?: string;
458
+ totalKey?: string;
459
+ initRoot?: string;
451
460
  item?: RemoteSelectOptionItemFunction;
452
- value: string | SelectOptionValueFunction;
453
- text: string | SelectOptionTextFunction;
461
+ value?: string | SelectOptionValueFunction;
462
+ text?: string | SelectOptionTextFunction;
454
463
  };
455
464
  }
456
465
 
package/types/schema.d.ts CHANGED
@@ -345,7 +345,10 @@ export interface SelectConfig extends FormItem, Input {
345
345
  options: SelectConfigOption[] | SelectConfigGroupOption[] | SelectOptionFunction;
346
346
  remote: true;
347
347
  option: {
348
- url: string;
348
+ url: string | ((mForm: FormState | undefined, data: {
349
+ model: any;
350
+ formValue: any;
351
+ }) => string);
349
352
  initUrl?: string | ((mForm: FormState | undefined, data: {
350
353
  model: any;
351
354
  formValue: any;
@@ -359,13 +362,18 @@ export interface SelectConfig extends FormItem, Input {
359
362
  };
360
363
  json?: false | boolean;
361
364
  body?: Record<string, any> | RemoteSelectOptionBodyFunction;
365
+ initBody?: Record<string, any> | RemoteSelectOptionBodyFunction;
362
366
  jsonpCallback?: 'callback' | string;
363
367
  afterRequest?: RemoteSelectOptionRequestFunction;
368
+ afterInitRequest?: RemoteSelectOptionRequestFunction;
364
369
  beforeRequest?: (mForm: FormState | undefined, postOptions: Record<string, any>, data: any) => Record<string, any>;
365
- root: string;
370
+ beforeInitRequest?: (mForm: FormState | undefined, postOptions: Record<string, any>, data: any) => Record<string, any>;
371
+ root?: string;
372
+ totalKey?: string;
373
+ initRoot?: string;
366
374
  item?: RemoteSelectOptionItemFunction;
367
- value: string | SelectOptionValueFunction;
368
- text: string | SelectOptionTextFunction;
375
+ value?: string | SelectOptionValueFunction;
376
+ text?: string | SelectOptionTextFunction;
369
377
  };
370
378
  }
371
379
  /**