@hi-ui/form 4.0.0-beta.24 → 4.0.0-beta.25

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/lib/cjs/Form.js CHANGED
@@ -72,14 +72,14 @@ var Form = /*#__PURE__*/React.forwardRef(function (_a, ref) {
72
72
  validate: formContext.submitForm,
73
73
  reset: formContext.resetForm,
74
74
  validateField: formContext.validateFieldState,
75
- // validateFields: formContext.va,
76
75
  setFieldValue: formContext.setFieldValue,
77
76
  setFieldsValue: formContext.setFieldsValue,
78
77
  getFieldValue: formContext.getFieldValue,
79
78
  getFieldsValue: formContext.getFieldsValue,
80
79
  getFieldError: formContext.getFieldError,
81
80
  getFieldsError: formContext.getFieldsError,
82
- clearValidates: formContext.resetErrors
81
+ clearValidates: formContext.resetErrors,
82
+ clearFieldValidate: formContext.resetFieldError
83
83
  };
84
84
  });
85
85
  var providedValue = React.useMemo(function () {
@@ -309,7 +309,6 @@ var useForm = function useForm(_a) {
309
309
  */
310
310
 
311
311
  var setFieldValue = React.useCallback(function (field, value, shouldValidate) {
312
- // @ts-ignore
313
312
  formDispatch({
314
313
  type: 'SET_FIELD_VALUE',
315
314
  payload: {
@@ -318,20 +317,20 @@ var useForm = function useForm(_a) {
318
317
  }
319
318
  }); // touched 给外部控制展示,而不是当做参数暴露
320
319
 
321
- var shouldValidateField = // shouldValidate ?? (validateAfterTouched ? getNested(formState.touched, field) : true)
322
- validateAfterTouched ? objectUtils.getNested(formState.touched, field) && shouldValidate : shouldValidate;
320
+ var shouldValidateField = validateAfterTouched ? objectUtils.getNested(formState.touched, field) && shouldValidate : shouldValidate;
323
321
 
324
322
  if (shouldValidateField) {
325
323
  validateField(field, value);
326
324
  }
327
325
  }, [validateField, validateAfterTouched, formState]);
328
- var setFieldsValue = React.useCallback(function (fields, shouldValidate) {
329
- Object.entries(fields).forEach(function (_ref) {
330
- var fieldName = _ref[0],
331
- value = _ref[1];
332
- setFieldValue(fieldName, value, shouldValidate);
326
+ var setFieldsValue = React.useCallback(function (fieldsState) {
327
+ formDispatch({
328
+ type: 'SET_VALUES',
329
+ payload: function payload(prevState) {
330
+ return typeAssertion.isFunction(fieldsState) ? fieldsState(prevState) : objectUtils.merge(objectUtils.clone(prevState), fieldsState);
331
+ }
333
332
  });
334
- }, [setFieldValue]);
333
+ }, []);
335
334
  var normalizeValueFromChange = React.useCallback(function (eventOrValue, valuePropName) {
336
335
  var nextValue = eventOrValue;
337
336
 
@@ -539,11 +538,13 @@ var useForm = function useForm(_a) {
539
538
  }, [resetForm]);
540
539
  var resetErrors = React.useCallback(function () {
541
540
  formDispatch({
542
- // TODO: reset errorMsg
543
541
  type: 'SET_ERRORS',
544
542
  payload: {}
545
543
  });
546
544
  }, []);
545
+ var resetFieldError = React.useCallback(function (field) {
546
+ setFieldError(field, '');
547
+ }, [setFieldError]);
547
548
  var setFormState = React__default["default"].useCallback(function (stateOrFunc) {
548
549
  // @ts-ignore
549
550
  formDispatch({
@@ -609,14 +610,14 @@ var useForm = function useForm(_a) {
609
610
  submitForm: submitForm,
610
611
  resetForm: resetForm,
611
612
  resetErrors: resetErrors,
613
+ resetFieldError: resetFieldError,
612
614
  validateFieldState: validateFieldState,
613
615
  validateValue: validateField,
614
616
  getFieldsValue: getFieldsValue,
615
617
  setFieldsValue: setFieldsValue,
616
618
  getFieldsError: getFieldsError
617
619
  });
618
- }; // TODO: field 支持数组
619
-
620
+ };
620
621
 
621
622
  function formReducer(state, action) {
622
623
  switch (action.type) {
@@ -625,8 +626,9 @@ function formReducer(state, action) {
625
626
  return Object.assign(Object.assign({}, state), nextState);
626
627
 
627
628
  case 'SET_VALUES':
629
+ var nextValues = typeAssertion.isFunction(action.payload) ? action.payload(state.values) : action.payload;
628
630
  return Object.assign(Object.assign({}, state), {
629
- values: action.payload
631
+ values: nextValues
630
632
  });
631
633
 
632
634
  case 'SET_ERRORS':
package/lib/esm/Form.js CHANGED
@@ -50,14 +50,14 @@ var Form = /*#__PURE__*/forwardRef(function (_a, ref) {
50
50
  validate: formContext.submitForm,
51
51
  reset: formContext.resetForm,
52
52
  validateField: formContext.validateFieldState,
53
- // validateFields: formContext.va,
54
53
  setFieldValue: formContext.setFieldValue,
55
54
  setFieldsValue: formContext.setFieldsValue,
56
55
  getFieldValue: formContext.getFieldValue,
57
56
  getFieldsValue: formContext.getFieldsValue,
58
57
  getFieldError: formContext.getFieldError,
59
58
  getFieldsError: formContext.getFieldsError,
60
- clearValidates: formContext.resetErrors
59
+ clearValidates: formContext.resetErrors,
60
+ clearFieldValidate: formContext.resetFieldError
61
61
  };
62
62
  });
63
63
  var providedValue = useMemo(function () {
@@ -12,9 +12,9 @@ import { __rest, __awaiter } from 'tslib';
12
12
  import { stringify, parse, isValidField } from './utils/index.js';
13
13
  import React, { useMemo, useReducer, useCallback, useRef } from 'react';
14
14
  import { useLatestRef, useLatestCallback } from '@hi-ui/use-latest';
15
- import { isArray, isObjectLike, isFunction } from '@hi-ui/type-assertion';
15
+ import { isArray, isFunction, isObjectLike } from '@hi-ui/type-assertion';
16
16
  import { callAllFuncs } from '@hi-ui/func-utils';
17
- import { getNested, setNested } from '@hi-ui/object-utils';
17
+ import { getNested, setNested, merge, clone } from '@hi-ui/object-utils';
18
18
  import { stopEvent } from '@hi-ui/dom-utils';
19
19
  var EMPTY_RULES = {};
20
20
  var EMPTY_ERRORS = {};
@@ -282,7 +282,6 @@ var useForm = function useForm(_a) {
282
282
  */
283
283
 
284
284
  var setFieldValue = useCallback(function (field, value, shouldValidate) {
285
- // @ts-ignore
286
285
  formDispatch({
287
286
  type: 'SET_FIELD_VALUE',
288
287
  payload: {
@@ -291,20 +290,20 @@ var useForm = function useForm(_a) {
291
290
  }
292
291
  }); // touched 给外部控制展示,而不是当做参数暴露
293
292
 
294
- var shouldValidateField = // shouldValidate ?? (validateAfterTouched ? getNested(formState.touched, field) : true)
295
- validateAfterTouched ? getNested(formState.touched, field) && shouldValidate : shouldValidate;
293
+ var shouldValidateField = validateAfterTouched ? getNested(formState.touched, field) && shouldValidate : shouldValidate;
296
294
 
297
295
  if (shouldValidateField) {
298
296
  validateField(field, value);
299
297
  }
300
298
  }, [validateField, validateAfterTouched, formState]);
301
- var setFieldsValue = useCallback(function (fields, shouldValidate) {
302
- Object.entries(fields).forEach(function (_ref) {
303
- var fieldName = _ref[0],
304
- value = _ref[1];
305
- setFieldValue(fieldName, value, shouldValidate);
299
+ var setFieldsValue = useCallback(function (fieldsState) {
300
+ formDispatch({
301
+ type: 'SET_VALUES',
302
+ payload: function payload(prevState) {
303
+ return isFunction(fieldsState) ? fieldsState(prevState) : merge(clone(prevState), fieldsState);
304
+ }
306
305
  });
307
- }, [setFieldValue]);
306
+ }, []);
308
307
  var normalizeValueFromChange = useCallback(function (eventOrValue, valuePropName) {
309
308
  var nextValue = eventOrValue;
310
309
 
@@ -512,11 +511,13 @@ var useForm = function useForm(_a) {
512
511
  }, [resetForm]);
513
512
  var resetErrors = useCallback(function () {
514
513
  formDispatch({
515
- // TODO: reset errorMsg
516
514
  type: 'SET_ERRORS',
517
515
  payload: {}
518
516
  });
519
517
  }, []);
518
+ var resetFieldError = useCallback(function (field) {
519
+ setFieldError(field, '');
520
+ }, [setFieldError]);
520
521
  var setFormState = React.useCallback(function (stateOrFunc) {
521
522
  // @ts-ignore
522
523
  formDispatch({
@@ -582,14 +583,14 @@ var useForm = function useForm(_a) {
582
583
  submitForm: submitForm,
583
584
  resetForm: resetForm,
584
585
  resetErrors: resetErrors,
586
+ resetFieldError: resetFieldError,
585
587
  validateFieldState: validateFieldState,
586
588
  validateValue: validateField,
587
589
  getFieldsValue: getFieldsValue,
588
590
  setFieldsValue: setFieldsValue,
589
591
  getFieldsError: getFieldsError
590
592
  });
591
- }; // TODO: field 支持数组
592
-
593
+ };
593
594
 
594
595
  function formReducer(state, action) {
595
596
  switch (action.type) {
@@ -598,8 +599,9 @@ function formReducer(state, action) {
598
599
  return Object.assign(Object.assign({}, state), nextState);
599
600
 
600
601
  case 'SET_VALUES':
602
+ var nextValues = isFunction(action.payload) ? action.payload(state.values) : action.payload;
601
603
  return Object.assign(Object.assign({}, state), {
602
- values: action.payload
604
+ values: nextValues
603
605
  });
604
606
 
605
607
  case 'SET_ERRORS':
@@ -41,19 +41,19 @@ export declare type FormAction<T> = {
41
41
  } | {
42
42
  type: 'SET_FIELD_VALUE';
43
43
  payload: {
44
- field: React.ReactText[];
44
+ field: FormFieldPath;
45
45
  value?: any;
46
46
  };
47
47
  } | {
48
48
  type: 'SET_FIELD_TOUCHED';
49
49
  payload: {
50
- field: React.ReactText[];
50
+ field: FormFieldPath;
51
51
  value?: boolean;
52
52
  };
53
53
  } | {
54
54
  type: 'SET_FIELD_ERROR';
55
55
  payload: {
56
- field: React.ReactText[];
56
+ field: FormFieldPath;
57
57
  value?: string;
58
58
  };
59
59
  } | {
@@ -85,41 +85,38 @@ export interface FormHelpers<T = any> {
85
85
  */
86
86
  validate: () => Promise<T>;
87
87
  /**
88
- * 重置整个表单的验证,对应 Form.Reset中的 API
88
+ * 重置整个表单的验证,对应 Form.Reset中的 API,如果不知道 nextState,则重置为 initialValues
89
89
  */
90
- reset: (fields?: FormFieldPath, toDefault?: boolean) => Promise<T>;
90
+ reset: (nextState?: Partial<FormState<any>>) => Promise<T>;
91
91
  /**
92
92
  * 对指定表单字段进行校验
93
93
  */
94
- validateField: (fields?: FormFieldPath) => Promise<T>;
94
+ validateField: (fields: FormFieldPath) => Promise<T>;
95
95
  /**
96
- * 对指定表单字段进行校验
97
- */
98
- validateFields: (fields?: FormFieldPath) => Promise<T>;
99
- /**
100
- * 设置表单的值,在异步获取的数据回显的时候,使用该方法
96
+ * 设置某个表单字段的值,在更新表单数据的的时候,使用该方法
101
97
  */
102
98
  setFieldValue: (field: FormFieldPath, value: any) => void;
103
99
  /**
104
100
  * 设置多个表单的值,在异步获取的数据回显的时候,使用该方法
101
+ * TODO
105
102
  */
106
- setFieldsValue: (field: Record<string, any>) => void;
103
+ setFieldsValue: (field: Record<string, any> | ((prevValues: Record<string, any>) => Record<string, any>)) => void;
107
104
  /**
108
- * 获取一个字段名对应的 Values 返回为数组形式, 不传入 fields;默认返回全部信息, 不会触发表单校验
105
+ * 获取一个字段名对应的 Value,返回为对应 Field
109
106
  */
110
107
  getFieldValue: (field: FormFieldPath) => any;
111
108
  /**
112
- * 获取所有字段名对应的 Values 返回为数组形式, 不传入 fields;默认返回全部信息, 不会触发表单校验
109
+ * 获取所有字段名对应的 Values 返回为字典映射数据结构
113
110
  */
114
111
  getFieldsValue: () => any;
115
112
  /**
116
- * 获取一组字段名对应的错误信息,返回为数组形式, 不传入 fields;默认返回全部信息
113
+ * 获取一组字段名对应的 errorMessage,返回为对应 field 错误信息
117
114
  */
118
115
  getFieldError: (field: FormFieldPath) => any;
119
116
  /**
120
- * 获取所有字段名对应的错误信息,返回为数组形式, 不传入 fields;默认返回全部信息
117
+ * 获取所有字段名对应的错误信息,返回为字典映射数据结构
121
118
  */
122
- getFieldsError: () => any;
119
+ getFieldsError: () => FormErrors<any>;
123
120
  /**
124
121
  * 移除所有表单项的校验结果
125
122
  */
@@ -127,7 +124,7 @@ export interface FormHelpers<T = any> {
127
124
  /**
128
125
  * 移除表单项的校验结果,传入待移除的表单项的 field 属性组成的数组
129
126
  */
130
- clearFieldsValidates: (fields: FormFieldPath) => void;
127
+ clearFieldValidate: (fields: FormFieldPath) => void;
131
128
  }
132
129
  export declare type FormFieldPath = (string | number)[] | string | number;
133
130
  export declare type FormErrorMessage = string;
@@ -9,21 +9,22 @@ export declare const useForm: <Values = Record<string, any>>({ initialValues, in
9
9
  getFieldError: (fieldName: FormFieldPath) => any;
10
10
  getFieldRules: (fieldName: FormFieldPath) => any;
11
11
  getRootProps: () => {
12
- onSubmit: (evt?: React.FormEvent<HTMLFormElement> | undefined) => Promise<unknown>;
12
+ onSubmit: (evt?: React.FormEvent<HTMLFormElement> | undefined) => Promise<any>;
13
13
  onReset: (evt?: React.FormEvent<HTMLFormElement> | undefined) => void;
14
14
  };
15
15
  getFieldProps: (props?: any) => any;
16
16
  registerField: (keyOrKeys: FormFieldPath, value: FormFieldCollection<Values>) => void;
17
17
  unregisterField: (keyOrKeys: FormFieldPath) => void;
18
- submitForm: () => Promise<unknown>;
18
+ submitForm: () => Promise<any>;
19
19
  resetForm: (nextState?: Partial<FormState<any>> | undefined) => Promise<void>;
20
20
  resetErrors: () => void;
21
+ resetFieldError: (field: FormFieldPath) => void;
21
22
  validateFieldState: (field: FormFieldPath) => Promise<{} | undefined>;
22
23
  validateValue: (field: FormFieldPath, value: unknown) => Promise<{} | undefined>;
23
24
  getFieldsValue: () => any;
24
- setFieldsValue: (fields: Record<string, any>, shouldValidate?: boolean | undefined) => void;
25
+ setFieldsValue: (fieldsState: Record<string, any> | Function) => void;
25
26
  getFieldsError: () => any;
26
- values: unknown;
27
+ values: any;
27
28
  errors: FormErrors<unknown>;
28
29
  touched: import("./types").FormTouched<unknown>;
29
30
  validating: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hi-ui/form",
3
- "version": "4.0.0-beta.24",
3
+ "version": "4.0.0-beta.25",
4
4
  "description": "A sub-package for @hi-ui/hiui.",
5
5
  "keywords": [],
6
6
  "author": "HIUI <mi-hiui@xiaomi.com>",
@@ -49,9 +49,9 @@
49
49
  "@hi-ui/core-css": "^4.0.0-beta.5",
50
50
  "@hi-ui/dom-utils": "^4.0.0-beta.5",
51
51
  "@hi-ui/env": "^4.0.0-beta.0",
52
- "@hi-ui/func-utils": "^4.0.0-beta.10",
53
- "@hi-ui/locale-context": "^4.0.0-beta.16",
54
- "@hi-ui/object-utils": "^4.0.0-beta.9",
52
+ "@hi-ui/func-utils": "^4.0.0-beta.11",
53
+ "@hi-ui/locale-context": "^4.0.0-beta.17",
54
+ "@hi-ui/object-utils": "^4.0.0-beta.10",
55
55
  "@hi-ui/type-assertion": "^4.0.0-beta.4",
56
56
  "@hi-ui/use-latest": "^4.0.0-beta.4",
57
57
  "async-validator": "^4.0.7"
@@ -65,5 +65,5 @@
65
65
  "react": "^17.0.1",
66
66
  "react-dom": "^17.0.1"
67
67
  },
68
- "gitHead": "53ae46d5c39ae7c8ed67049ca2ecbc156c891aef"
68
+ "gitHead": "674c0457e222285057bb82be5dc1ce6bf081b96c"
69
69
  }