@hi-ui/form 4.0.0-beta.26 → 4.0.0-beta.29

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.
@@ -56,8 +56,7 @@ var FormList = function FormList(_ref) {
56
56
 
57
57
  var updateFormList = React__default["default"].useCallback(function (stateFunc, alterTouched, alterErrors) {
58
58
  setFormState(function (prev) {
59
- var values = objectUtils.setNested(prev.values, name, stateFunc(objectUtils.getNested(prev.values, name))); // console.log(values, stateFunc(getNested(prev.values, name)))
60
-
59
+ var values = objectUtils.setNested(prev.values, name, stateFunc(objectUtils.getNested(prev.values, name)));
61
60
  var updateErrors = typeof alterErrors === 'function' ? alterErrors : stateFunc;
62
61
  var updateTouched = typeof alterTouched === 'function' ? alterTouched : stateFunc;
63
62
  var fieldError = alterErrors ? updateErrors(objectUtils.getNested(prev.errors, name)) : undefined;
@@ -71,8 +71,10 @@ var FormSubmit = /*#__PURE__*/React.forwardRef(function (_a, ref) {
71
71
  evt.preventDefault();
72
72
  evt.stopPropagation();
73
73
  submitForm().then(function (result) {
74
+ // @ts-ignore
74
75
  _onClick === null || _onClick === void 0 ? void 0 : _onClick(result, null);
75
76
  })["catch"](function (error) {
77
+ // @ts-ignore
76
78
  _onClick === null || _onClick === void 0 ? void 0 : _onClick(null, error);
77
79
  });
78
80
  }
@@ -14,7 +14,7 @@ Object.defineProperty(exports, '__esModule', {
14
14
  });
15
15
  var css_248z = ".hi-v4-form {max-width: 100%;position: relative; }.hi-v4-form fieldset {margin: 0;padding: 0;border: 0; }.hi-v4-form fieldset + fieldset {margin-top: 16px; }.hi-v4-form legend {margin: 0 0 16px;color: var(--hi-v4-color-gray-800, #1f2937); }.hi-v4-form--placement-horizontal {display: -webkit-box;display: -ms-flexbox;display: flex;-ms-flex-wrap: wrap;flex-wrap: wrap; }.hi-v4-form--placement-horizontal .hi-v4-form-label__indent {min-height: var(--hi-v4-height-8, 32px); }.hi-v4-form-label {display: -webkit-box;display: -ms-flexbox;display: flex;font-size: var(--hi-v4-text-size-md, 0.875rem);-webkit-box-sizing: border-box;box-sizing: border-box;margin-right: 0;margin-bottom: var(--hi-v4-spacing-12, 24px); }.hi-v4-form-label__text, .hi-v4-form-label__indent {-ms-flex-negative: 0;flex-shrink: 0;-webkit-box-sizing: border-box;box-sizing: border-box;vertical-align: top;color: var(--hi-v4-color-gray-800, #1f2937);overflow-wrap: break-word; }.hi-v4-form-label__text {padding-right: var(--hi-v4-spacing-6, 12px);min-height: var(--hi-v4-height-8, 32px);line-height: var(--hi-v4-height-8, 32px); }.hi-v4-form-label--placement-left .hi-v4-form-label__text {text-align: left; }.hi-v4-form-label--placement-right .hi-v4-form-label__text {text-align: right; }.hi-v4-form-label--placement-top .hi-v4-form-label__text {height: 22px;min-height: 22px;line-height: 22px;padding-right: 0;margin-bottom: var(--hi-v4-spacing-4, 8px); }.hi-v4-form-label--required .hi-v4-form-label__text::before {margin-right: var(--hi-v4-spacing-2, 4px);content: '*';color: var(--hi-v4-color-danger-500, var(--hi-v4-color-red-500, #ff5959)); }.hi-v4-form-label--placement-top {text-align: left;-webkit-box-orient: vertical;-webkit-box-direction: normal;-ms-flex-direction: column;flex-direction: column;margin-bottom: var(--hi-v4-spacing-12, 24px);padding-right: 0; }.hi-v4-form-label__content {-webkit-box-flex: 1;-ms-flex: 1 1;flex: 1 1;position: relative;vertical-align: top;min-height: var(--hi-v4-height-8, 32px);-ms-flex-negative: 0;flex-shrink: 0; }.hi-v4-form-label__control {min-width: 1px; }.hi-v4-form-message {display: block;font-size: var(--hi-v4-text-size-md, 0.875rem);min-height: 24px;padding: 2px 0;-webkit-box-sizing: border-box;box-sizing: border-box;line-height: 20px;-webkit-transform: translateY(-10%);transform: translateY(-10%);opacity: 0;color: var(--hi-v4-color-danger-500, var(--hi-v4-color-red-500, #ff5959));-webkit-transition: all 0.3s;transition: all 0.3s; }.hi-v4-form-message--show {opacity: 1;-webkit-transform: translateY(0);transform: translateY(0); }.hi-v4-form-item {position: relative; }.hi-v4-form-item__message {position: absolute; }";
16
16
 
17
- var __styleInject__ = require('style-inject/dist/style-inject.es.js')["default"];
17
+ var __styleInject__ = require('inject-head-style')["default"];
18
18
 
19
19
  __styleInject__(css_248z);
20
20
 
@@ -327,7 +327,7 @@ var useForm = function useForm(_a) {
327
327
  formDispatch({
328
328
  type: 'SET_VALUES',
329
329
  payload: function payload(prevState) {
330
- return typeAssertion.isFunction(fieldsState) ? fieldsState(prevState) : objectUtils.merge(objectUtils.clone(prevState), fieldsState);
330
+ return typeAssertion.isFunction(fieldsState) ? fieldsState(prevState) : index.mergeValues(prevState, fieldsState);
331
331
  }
332
332
  });
333
333
  }, []);
@@ -15,6 +15,8 @@ Object.defineProperty(exports, '__esModule', {
15
15
 
16
16
  var typeAssertion = require('@hi-ui/type-assertion');
17
17
 
18
+ var objectUtils = require('@hi-ui/object-utils');
19
+
18
20
  var stringify = function stringify(field) {
19
21
  return JSON.stringify(field);
20
22
  };
@@ -31,6 +33,21 @@ var isValidField = function isValidField(field) {
31
33
  return true;
32
34
  };
33
35
 
36
+ var mergeValues = function mergeValues(source, override) {
37
+ if (!override) return source;
38
+ if (!source) return objectUtils.clone(override);
39
+ var target = objectUtils.clone(source);
40
+
41
+ for (var key in override) {
42
+ if (objectUtils.hasOwnProp(override, key)) {
43
+ target[key] = override[key];
44
+ }
45
+ }
46
+
47
+ return target;
48
+ };
49
+
34
50
  exports.isValidField = isValidField;
51
+ exports.mergeValues = mergeValues;
35
52
  exports.parse = parse;
36
53
  exports.stringify = stringify;
@@ -35,8 +35,7 @@ var FormList = function FormList(_ref) {
35
35
 
36
36
  var updateFormList = React.useCallback(function (stateFunc, alterTouched, alterErrors) {
37
37
  setFormState(function (prev) {
38
- var values = setNested(prev.values, name, stateFunc(getNested(prev.values, name))); // console.log(values, stateFunc(getNested(prev.values, name)))
39
-
38
+ var values = setNested(prev.values, name, stateFunc(getNested(prev.values, name)));
40
39
  var updateErrors = typeof alterErrors === 'function' ? alterErrors : stateFunc;
41
40
  var updateTouched = typeof alterTouched === 'function' ? alterTouched : stateFunc;
42
41
  var fieldError = alterErrors ? updateErrors(getNested(prev.errors, name)) : undefined;
@@ -47,8 +47,10 @@ var FormSubmit = /*#__PURE__*/forwardRef(function (_a, ref) {
47
47
  evt.preventDefault();
48
48
  evt.stopPropagation();
49
49
  submitForm().then(function (result) {
50
+ // @ts-ignore
50
51
  _onClick === null || _onClick === void 0 ? void 0 : _onClick(result, null);
51
52
  })["catch"](function (error) {
53
+ // @ts-ignore
52
54
  _onClick === null || _onClick === void 0 ? void 0 : _onClick(null, error);
53
55
  });
54
56
  }
@@ -7,7 +7,7 @@
7
7
  * This source code is licensed under the MIT license found in the
8
8
  * LICENSE file in the root directory of this source tree.
9
9
  */
10
- import __styleInject__ from 'style-inject/dist/style-inject.es.js';
10
+ import __styleInject__ from 'inject-head-style';
11
11
  var css_248z = ".hi-v4-form {max-width: 100%;position: relative; }.hi-v4-form fieldset {margin: 0;padding: 0;border: 0; }.hi-v4-form fieldset + fieldset {margin-top: 16px; }.hi-v4-form legend {margin: 0 0 16px;color: var(--hi-v4-color-gray-800, #1f2937); }.hi-v4-form--placement-horizontal {display: -webkit-box;display: -ms-flexbox;display: flex;-ms-flex-wrap: wrap;flex-wrap: wrap; }.hi-v4-form--placement-horizontal .hi-v4-form-label__indent {min-height: var(--hi-v4-height-8, 32px); }.hi-v4-form-label {display: -webkit-box;display: -ms-flexbox;display: flex;font-size: var(--hi-v4-text-size-md, 0.875rem);-webkit-box-sizing: border-box;box-sizing: border-box;margin-right: 0;margin-bottom: var(--hi-v4-spacing-12, 24px); }.hi-v4-form-label__text, .hi-v4-form-label__indent {-ms-flex-negative: 0;flex-shrink: 0;-webkit-box-sizing: border-box;box-sizing: border-box;vertical-align: top;color: var(--hi-v4-color-gray-800, #1f2937);overflow-wrap: break-word; }.hi-v4-form-label__text {padding-right: var(--hi-v4-spacing-6, 12px);min-height: var(--hi-v4-height-8, 32px);line-height: var(--hi-v4-height-8, 32px); }.hi-v4-form-label--placement-left .hi-v4-form-label__text {text-align: left; }.hi-v4-form-label--placement-right .hi-v4-form-label__text {text-align: right; }.hi-v4-form-label--placement-top .hi-v4-form-label__text {height: 22px;min-height: 22px;line-height: 22px;padding-right: 0;margin-bottom: var(--hi-v4-spacing-4, 8px); }.hi-v4-form-label--required .hi-v4-form-label__text::before {margin-right: var(--hi-v4-spacing-2, 4px);content: '*';color: var(--hi-v4-color-danger-500, var(--hi-v4-color-red-500, #ff5959)); }.hi-v4-form-label--placement-top {text-align: left;-webkit-box-orient: vertical;-webkit-box-direction: normal;-ms-flex-direction: column;flex-direction: column;margin-bottom: var(--hi-v4-spacing-12, 24px);padding-right: 0; }.hi-v4-form-label__content {-webkit-box-flex: 1;-ms-flex: 1 1;flex: 1 1;position: relative;vertical-align: top;min-height: var(--hi-v4-height-8, 32px);-ms-flex-negative: 0;flex-shrink: 0; }.hi-v4-form-label__control {min-width: 1px; }.hi-v4-form-message {display: block;font-size: var(--hi-v4-text-size-md, 0.875rem);min-height: 24px;padding: 2px 0;-webkit-box-sizing: border-box;box-sizing: border-box;line-height: 20px;-webkit-transform: translateY(-10%);transform: translateY(-10%);opacity: 0;color: var(--hi-v4-color-danger-500, var(--hi-v4-color-red-500, #ff5959));-webkit-transition: all 0.3s;transition: all 0.3s; }.hi-v4-form-message--show {opacity: 1;-webkit-transform: translateY(0);transform: translateY(0); }.hi-v4-form-item {position: relative; }.hi-v4-form-item__message {position: absolute; }";
12
12
 
13
13
  __styleInject__(css_248z);
@@ -9,12 +9,12 @@
9
9
  */
10
10
  import _regeneratorRuntime from '@babel/runtime/regenerator';
11
11
  import { __rest, __awaiter } from 'tslib';
12
- import { stringify, parse, isValidField } from './utils/index.js';
12
+ import { stringify, parse, mergeValues, 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
15
  import { isArray, isFunction, isObjectLike } from '@hi-ui/type-assertion';
16
16
  import { callAllFuncs } from '@hi-ui/func-utils';
17
- import { getNested, setNested, merge, clone } from '@hi-ui/object-utils';
17
+ import { getNested, setNested } from '@hi-ui/object-utils';
18
18
  import { stopEvent } from '@hi-ui/dom-utils';
19
19
  var EMPTY_RULES = {};
20
20
  var EMPTY_ERRORS = {};
@@ -300,7 +300,7 @@ var useForm = function useForm(_a) {
300
300
  formDispatch({
301
301
  type: 'SET_VALUES',
302
302
  payload: function payload(prevState) {
303
- return isFunction(fieldsState) ? fieldsState(prevState) : merge(clone(prevState), fieldsState);
303
+ return isFunction(fieldsState) ? fieldsState(prevState) : mergeValues(prevState, fieldsState);
304
304
  }
305
305
  });
306
306
  }, []);
@@ -8,6 +8,7 @@
8
8
  * LICENSE file in the root directory of this source tree.
9
9
  */
10
10
  import { isNullish, isArray } from '@hi-ui/type-assertion';
11
+ import { clone, hasOwnProp } from '@hi-ui/object-utils';
11
12
 
12
13
  var stringify = function stringify(field) {
13
14
  return JSON.stringify(field);
@@ -25,4 +26,18 @@ var isValidField = function isValidField(field) {
25
26
  return true;
26
27
  };
27
28
 
28
- export { isValidField, parse, stringify };
29
+ var mergeValues = function mergeValues(source, override) {
30
+ if (!override) return source;
31
+ if (!source) return clone(override);
32
+ var target = clone(source);
33
+
34
+ for (var key in override) {
35
+ if (hasOwnProp(override, key)) {
36
+ target[key] = override[key];
37
+ }
38
+ }
39
+
40
+ return target;
41
+ };
42
+
43
+ export { isValidField, mergeValues, parse, stringify };
@@ -1,6 +1,5 @@
1
1
  import React from 'react';
2
2
  import { UseFormFieldProps } from './use-form-field';
3
- import { FormFieldRenderFunc } from './types';
4
3
  /**
5
4
  * TODO: What is FormField
6
5
  */
@@ -9,9 +8,9 @@ export interface FormFieldProps extends UseFormFieldProps {
9
8
  /**
10
9
  * 表单控件
11
10
  */
12
- children?: React.ReactElement | FormFieldRenderFunc;
11
+ children?: React.ReactNode;
13
12
  /**
14
13
  * 支持表单控件 render 渲染
15
14
  */
16
- render?: FormFieldRenderFunc;
15
+ render?: (props: Record<string, any>) => React.ReactNode;
17
16
  }
@@ -7,11 +7,11 @@ import { FormLabelProps } from './FormLabel';
7
7
  export declare const FormItem: React.FC<FormItemProps>;
8
8
  export interface FormItemProps extends UseFormFieldProps, FormLabelProps {
9
9
  /**
10
- * 表单控件
10
+ * 表单控件或其渲染函数
11
11
  */
12
- children?: React.ReactElement;
12
+ children?: React.ReactNode;
13
13
  /**
14
14
  * 支持表单控件 render 渲染
15
15
  */
16
- render?: (props: Record<string, any>) => React.ReactElement;
16
+ render?: (props: Record<string, any>) => any;
17
17
  }
@@ -10,7 +10,7 @@ export interface FormLabelProps extends HiBaseHTMLProps<'div'> {
10
10
  */
11
11
  labelPlacement?: 'right' | 'left' | 'top';
12
12
  /**
13
- * 标记是否必填
13
+ * 是否显示必填信号。这里不做校验处理,若需校验请使用 rules 进行设置
14
14
  */
15
15
  required?: boolean;
16
16
  /**
@@ -1,16 +1,16 @@
1
1
  import React from 'react';
2
- import { FormListChildrenAction } from './types';
2
+ import { FormFieldPath, FormListChildrenAction } from './types';
3
3
  /**
4
4
  * TODO: What is FormList
5
5
  */
6
6
  export declare const FormList: React.FC<FormListProps>;
7
7
  export interface FormListProps {
8
8
  /**
9
- * 表单控件
9
+ * 表单控件渲染函数
10
10
  */
11
- children?: (fields: any[], action: FormListChildrenAction) => React.ReactElement | null;
11
+ children?: (fields: any[], action: FormListChildrenAction) => React.ReactElement;
12
12
  /**
13
13
  * 列表名称
14
14
  */
15
- name: string | string[];
15
+ name: FormFieldPath;
16
16
  }
@@ -5,5 +5,8 @@ import { ButtonProps } from '@hi-ui/button';
5
5
  */
6
6
  export declare const FormReset: React.ForwardRefExoticComponent<FormResetProps & React.RefAttributes<HTMLButtonElement | null>>;
7
7
  export interface FormResetProps extends ButtonProps {
8
+ /**
9
+ * 点击重置后触发
10
+ */
8
11
  onClick?: () => void;
9
12
  }
@@ -5,5 +5,8 @@ import { ButtonProps } from '@hi-ui/button';
5
5
  */
6
6
  export declare const FormSubmit: React.ForwardRefExoticComponent<FormSubmitProps & React.RefAttributes<HTMLAnchorElement | HTMLButtonElement | null>>;
7
7
  export interface FormSubmitProps extends Omit<ButtonProps, 'onClick'> {
8
- onClick?: (value: any, error: any) => void;
8
+ /**
9
+ * 点击提交后触发
10
+ */
11
+ onClick?: () => void;
9
12
  }
@@ -1,5 +1,4 @@
1
1
  import { RuleItem } from 'async-validator';
2
- import React from 'react';
3
2
  export interface FormState<T> {
4
3
  /**
5
4
  * 字段及值的映射存储
@@ -129,7 +128,6 @@ export interface FormHelpers<T = any> {
129
128
  export declare type FormFieldPath = (string | number)[] | string | number;
130
129
  export declare type FormErrorMessage = string;
131
130
  export declare type FormRules = Record<string, FormRuleModel[]>;
132
- export declare type FormFieldRenderFunc = (props: Record<string, any>) => React.ReactElement;
133
131
  export interface FormListChildrenAction {
134
132
  /**
135
133
  * 在尾部追加一个 FormItems
@@ -32,7 +32,7 @@ export declare const useForm: <Values = Record<string, any>>({ initialValues, in
32
32
  };
33
33
  export interface UseFormProps<T = Record<string, any>> {
34
34
  /**
35
- * 初始化表单值
35
+ * 初始化表单值,只在初始化以及重置时生效;该值是不受控的,和表单中的 defaultValue 的作用类似
36
36
  */
37
37
  initialValues: T;
38
38
  /**
@@ -2,3 +2,4 @@ import { FormFieldPath } from './../types';
2
2
  export declare const stringify: (field: FormFieldPath) => string;
3
3
  export declare const parse: (fieldStr: string) => FormFieldPath;
4
4
  export declare const isValidField: (field: FormFieldPath | undefined) => field is FormFieldPath;
5
+ export declare const mergeValues: <T extends Object, E extends T>(source: T, override: E | null | undefined) => T;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hi-ui/form",
3
- "version": "4.0.0-beta.26",
3
+ "version": "4.0.0-beta.29",
4
4
  "description": "A sub-package for @hi-ui/hiui.",
5
5
  "keywords": [],
6
6
  "author": "HIUI <mi-hiui@xiaomi.com>",
@@ -43,17 +43,17 @@
43
43
  "url": "https://github.com/XiaoMi/hiui/issues"
44
44
  },
45
45
  "dependencies": {
46
- "@hi-ui/array-utils": "^4.0.0-beta.7",
47
- "@hi-ui/button": "^4.0.0-beta.12",
46
+ "@hi-ui/array-utils": "^4.0.0-beta.8",
47
+ "@hi-ui/button": "^4.0.0-beta.14",
48
48
  "@hi-ui/classname": "^4.0.0-beta.0",
49
49
  "@hi-ui/core-css": "^4.0.0-beta.5",
50
- "@hi-ui/dom-utils": "^4.0.0-beta.5",
51
- "@hi-ui/env": "^4.0.0-beta.0",
52
- "@hi-ui/func-utils": "^4.0.0-beta.12",
53
- "@hi-ui/locale-context": "^4.0.0-beta.18",
54
- "@hi-ui/object-utils": "^4.0.0-beta.11",
55
- "@hi-ui/type-assertion": "^4.0.0-beta.4",
56
- "@hi-ui/use-latest": "^4.0.0-beta.4",
50
+ "@hi-ui/dom-utils": "^4.0.0-beta.6",
51
+ "@hi-ui/env": "^4.0.0-beta.1",
52
+ "@hi-ui/func-utils": "^4.0.0-beta.13",
53
+ "@hi-ui/locale-context": "^4.0.0-beta.19",
54
+ "@hi-ui/object-utils": "^4.0.0-beta.12",
55
+ "@hi-ui/type-assertion": "^4.0.0-beta.5",
56
+ "@hi-ui/use-latest": "^4.0.0-beta.5",
57
57
  "async-validator": "^4.0.7"
58
58
  },
59
59
  "peerDependencies": {
@@ -61,9 +61,9 @@
61
61
  "react-dom": ">=16.8.6"
62
62
  },
63
63
  "devDependencies": {
64
- "@hi-ui/hi-build": "^4.0.0-beta.4",
64
+ "@hi-ui/hi-build": "^4.0.0-beta.5",
65
65
  "react": "^17.0.1",
66
66
  "react-dom": "^17.0.1"
67
67
  },
68
- "gitHead": "7f47840510e6dfce429eeed1591c154e321cd14f"
68
+ "gitHead": "9956956831546dfe051b56cf8330912a772fa86f"
69
69
  }