@hi-ui/form 4.0.0-alpha.1
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/LICENSE +21 -0
- package/README.md +11 -0
- package/lib/cjs/Form.js +106 -0
- package/lib/cjs/Form.js.map +1 -0
- package/lib/cjs/FormField.js +44 -0
- package/lib/cjs/FormField.js.map +1 -0
- package/lib/cjs/FormItem.js +61 -0
- package/lib/cjs/FormItem.js.map +1 -0
- package/lib/cjs/FormLabel.js +135 -0
- package/lib/cjs/FormLabel.js.map +1 -0
- package/lib/cjs/FormList.js +166 -0
- package/lib/cjs/FormList.js.map +1 -0
- package/lib/cjs/FormMessage.js +72 -0
- package/lib/cjs/FormMessage.js.map +1 -0
- package/lib/cjs/context.js +33 -0
- package/lib/cjs/context.js.map +1 -0
- package/lib/cjs/index.js +29 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/styles/index.scss.js +22 -0
- package/lib/cjs/styles/index.scss.js.map +1 -0
- package/lib/cjs/use-form-field.js +114 -0
- package/lib/cjs/use-form-field.js.map +1 -0
- package/lib/cjs/use-form.js +544 -0
- package/lib/cjs/use-form.js.map +1 -0
- package/lib/esm/Form.js +83 -0
- package/lib/esm/Form.js.map +1 -0
- package/lib/esm/FormField.js +35 -0
- package/lib/esm/FormField.js.map +1 -0
- package/lib/esm/FormItem.js +39 -0
- package/lib/esm/FormItem.js.map +1 -0
- package/lib/esm/FormLabel.js +113 -0
- package/lib/esm/FormLabel.js.map +1 -0
- package/lib/esm/FormList.js +145 -0
- package/lib/esm/FormList.js.map +1 -0
- package/lib/esm/FormMessage.js +51 -0
- package/lib/esm/FormMessage.js.map +1 -0
- package/lib/esm/context.js +25 -0
- package/lib/esm/context.js.map +1 -0
- package/lib/esm/index.js +14 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/styles/index.scss.js +17 -0
- package/lib/esm/styles/index.scss.js.map +1 -0
- package/lib/esm/use-form-field.js +94 -0
- package/lib/esm/use-form-field.js.map +1 -0
- package/lib/esm/use-form.js +519 -0
- package/lib/esm/use-form.js.map +1 -0
- package/lib/types/Form.d.ts +35 -0
- package/lib/types/FormField.d.ts +12 -0
- package/lib/types/FormItem.d.ts +13 -0
- package/lib/types/FormLabel.d.ts +28 -0
- package/lib/types/FormList.d.ts +15 -0
- package/lib/types/FormMessage.d.ts +27 -0
- package/lib/types/FormReset.d.ts +8 -0
- package/lib/types/FormSubmit.d.ts +8 -0
- package/lib/types/context.d.ts +55 -0
- package/lib/types/index.d.ts +6 -0
- package/lib/types/types.d.ts +113 -0
- package/lib/types/use-form-field.d.ts +37 -0
- package/lib/types/use-form.d.ts +78 -0
- package/lib/types/utils/index.d.ts +1 -0
- package/package.json +68 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/form
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/form#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HIUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
import { isValidElement, cloneElement } from 'react';
|
|
11
|
+
import { __DEV__ } from '@hi-ui/env';
|
|
12
|
+
import { useFormField } from './use-form-field.js';
|
|
13
|
+
/**
|
|
14
|
+
* TODO: What is FormField
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
var FormField = function FormField(props) {
|
|
18
|
+
var _a;
|
|
19
|
+
|
|
20
|
+
var fieldProps = useFormField(props);
|
|
21
|
+
|
|
22
|
+
if (! /*#__PURE__*/isValidElement(props.children)) {
|
|
23
|
+
console.warn('FormField must pass a valid element as children.');
|
|
24
|
+
return (_a = props.children) !== null && _a !== void 0 ? _a : null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return /*#__PURE__*/cloneElement(props.children, Object.assign({}, fieldProps));
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
if (__DEV__) {
|
|
31
|
+
FormField.displayName = 'FormField';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { FormField };
|
|
35
|
+
//# sourceMappingURL=FormField.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormField.js","sources":["../../src/FormField.tsx"],"sourcesContent":[null],"names":["FormField","props","fieldProps","useFormField","isValidElement","children","console","warn","cloneElement","__DEV__","displayName"],"mappings":";;;;;;;;;;;;AAIA;;;;IAGaA,SAAS,GAA6B,SAAtCA,SAAsC,CAACC,KAAD;;;MAC3CC,UAAU,GAAGC,YAAY,CAACF,KAAD;;MAE3B,eAACG,cAAc,CAACH,KAAK,CAACI,QAAP,GAAkB;AACnCC,IAAAA,OAAO,CAACC,IAARD,CAAa,kDAAbA;WACO,MAAAL,KAAK,CAACI,QAAN,UAAA,iBAAA,KAAA,GAAkB;;;sBAGpBG,YAAY,CAACP,KAAK,CAACI,QAAP,oBAAsBH,WAAtB;;;AAUrB,IAAIO,OAAJ,EAAa;AACXT,EAAAA,SAAS,CAACU,WAAVV,GAAwB,WAAxBA;;;"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/form
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/form#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HIUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
import { __rest } from 'tslib';
|
|
11
|
+
import React from 'react';
|
|
12
|
+
import { __DEV__ } from '@hi-ui/env';
|
|
13
|
+
import { FormLabel } from './FormLabel.js';
|
|
14
|
+
import { FormMessage } from './FormMessage.js';
|
|
15
|
+
import { FormField } from './FormField.js';
|
|
16
|
+
/**
|
|
17
|
+
* TODO: What is FormItem
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
var FormItem = function FormItem(_a) {
|
|
21
|
+
var field = _a.field,
|
|
22
|
+
children = _a.children,
|
|
23
|
+
valueType = _a.valueType,
|
|
24
|
+
rest = __rest(_a, ["field", "children", "valueType"]);
|
|
25
|
+
|
|
26
|
+
return /*#__PURE__*/React.createElement(FormLabel, Object.assign({}, rest), /*#__PURE__*/React.createElement(FormField, {
|
|
27
|
+
field: field,
|
|
28
|
+
valueType: valueType
|
|
29
|
+
}, children), /*#__PURE__*/React.createElement(FormMessage, {
|
|
30
|
+
field: field
|
|
31
|
+
}));
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
if (__DEV__) {
|
|
35
|
+
FormItem.displayName = 'FormItem';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { FormItem };
|
|
39
|
+
//# sourceMappingURL=FormItem.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormItem.js","sources":["../../src/FormItem.tsx"],"sourcesContent":[null],"names":["FormItem","_a","field","children","valueType","rest","React","FormLabel","FormField","FormMessage","__DEV__","displayName"],"mappings":";;;;;;;;;;;;;;;AAOA;;;;IAGaA,QAAQ,GAA4B,SAApCA,QAAoC,CAACC,EAAD;MAAGC,WAAAA;MAAOC,cAAAA;MAAUC,eAAAA;MAAcC,kBAAjC,QAAA,YAAA,aAAA;;sBAE9CC,mBAAAA,CAACC,SAADD,oBAAeD,KAAfC,eACEA,mBAAAA,CAACE,SAADF;AAAWJ,IAAAA,KAAK,EAAEA;AAAOE,IAAAA,SAAS,EAAEA;GAApCE,EACGH,QADHG,CADFA,eAKEA,mBAAAA,CAACG,WAADH;AAAaJ,IAAAA,KAAK,EAAEA;GAApBI,CALFA;;;AAiBJ,IAAII,OAAJ,EAAa;AACXV,EAAAA,QAAQ,CAACW,WAATX,GAAuB,UAAvBA;;;"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/form
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/form#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HIUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
import { __rest } from 'tslib';
|
|
11
|
+
import React, { forwardRef, useMemo } from 'react';
|
|
12
|
+
import { getPrefixCls, cx } from '@hi-ui/classname';
|
|
13
|
+
import { __DEV__ } from '@hi-ui/env';
|
|
14
|
+
import { useFormContext } from './context.js';
|
|
15
|
+
import { isNumeric } from '@hi-ui/type-assertion';
|
|
16
|
+
var _role = 'form-label';
|
|
17
|
+
|
|
18
|
+
var _prefix = getPrefixCls(_role);
|
|
19
|
+
/**
|
|
20
|
+
* TODO: What is FormLabel
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
var FormLabel = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
25
|
+
var _useFormContext = useFormContext(),
|
|
26
|
+
labelWidthContext = _useFormContext.labelWidth,
|
|
27
|
+
labelPlacement = _useFormContext.labelPlacement,
|
|
28
|
+
colonContext = _useFormContext.colon,
|
|
29
|
+
contentPositionContext = _useFormContext.contentPosition;
|
|
30
|
+
|
|
31
|
+
var _props$prefixCls = props.prefixCls,
|
|
32
|
+
prefixCls = _props$prefixCls === void 0 ? _prefix : _props$prefixCls,
|
|
33
|
+
_props$role = props.role,
|
|
34
|
+
role = _props$role === void 0 ? _role : _props$role,
|
|
35
|
+
className = props.className,
|
|
36
|
+
styleProp = props.style,
|
|
37
|
+
children = props.children,
|
|
38
|
+
label = props.label,
|
|
39
|
+
_props$required = props.required,
|
|
40
|
+
required = _props$required === void 0 ? false : _props$required,
|
|
41
|
+
_props$labelWidth = props.labelWidth,
|
|
42
|
+
labelWidthProp = _props$labelWidth === void 0 ? labelWidthContext : _props$labelWidth,
|
|
43
|
+
_props$colon = props.colon,
|
|
44
|
+
colon = _props$colon === void 0 ? colonContext : _props$colon,
|
|
45
|
+
_props$contentPositio = props.contentPosition,
|
|
46
|
+
contentPosition = _props$contentPositio === void 0 ? contentPositionContext : _props$contentPositio,
|
|
47
|
+
rest = __rest(props, ["prefixCls", "role", "className", "style", "children", "label", "field", "required", "labelWidth", "colon", "contentPosition"]);
|
|
48
|
+
|
|
49
|
+
var _useMemo = useMemo(function () {
|
|
50
|
+
if (labelPlacement === 'top') return {
|
|
51
|
+
labelWidth: '100%',
|
|
52
|
+
controlWidth: '100%'
|
|
53
|
+
};
|
|
54
|
+
var labelWidth = isNumeric(labelWidthProp) ? Number(labelWidthProp) : labelWidthProp;
|
|
55
|
+
return {
|
|
56
|
+
labelWidth: labelWidth,
|
|
57
|
+
controlWidth: "calc(100% - " + labelWidth + "px)"
|
|
58
|
+
};
|
|
59
|
+
}, [labelPlacement, labelWidthProp]),
|
|
60
|
+
labelWidth = _useMemo.labelWidth,
|
|
61
|
+
controlWidth = _useMemo.controlWidth; // 指定子元素位置
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
var contentPositionMemo = useMemo(function () {
|
|
65
|
+
switch (contentPosition) {
|
|
66
|
+
case 'top':
|
|
67
|
+
return 'flex-start';
|
|
68
|
+
|
|
69
|
+
case 'bottom':
|
|
70
|
+
return 'flex-end';
|
|
71
|
+
|
|
72
|
+
default:
|
|
73
|
+
return 'center';
|
|
74
|
+
}
|
|
75
|
+
}, [contentPosition]);
|
|
76
|
+
var colonMemo = useMemo(function () {
|
|
77
|
+
return colon ? ':' : null;
|
|
78
|
+
}, [colon]);
|
|
79
|
+
var cls = cx(prefixCls, className, required && prefixCls + "--required", labelPlacement && prefixCls + "--placement-" + labelPlacement // error && `${prefixCls}--error`,
|
|
80
|
+
// validating && `${prefixCls}--validating`
|
|
81
|
+
);
|
|
82
|
+
var style = Object.assign(Object.assign({}, styleProp), {
|
|
83
|
+
alignItems: contentPositionMemo
|
|
84
|
+
});
|
|
85
|
+
return /*#__PURE__*/React.createElement("div", Object.assign({
|
|
86
|
+
ref: ref,
|
|
87
|
+
role: role,
|
|
88
|
+
className: cls,
|
|
89
|
+
style: style
|
|
90
|
+
}, rest), label ? /*#__PURE__*/React.createElement("label", {
|
|
91
|
+
className: prefixCls + "__text",
|
|
92
|
+
style: {
|
|
93
|
+
width: labelWidth
|
|
94
|
+
}
|
|
95
|
+
}, label, colonMemo) : /*#__PURE__*/React.createElement("span", {
|
|
96
|
+
className: prefixCls + "__indent",
|
|
97
|
+
style: {
|
|
98
|
+
width: labelWidth
|
|
99
|
+
}
|
|
100
|
+
}), /*#__PURE__*/React.createElement("div", {
|
|
101
|
+
className: prefixCls + "__control",
|
|
102
|
+
style: {
|
|
103
|
+
width: controlWidth
|
|
104
|
+
}
|
|
105
|
+
}, children));
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
if (__DEV__) {
|
|
109
|
+
FormLabel.displayName = 'FormLabel';
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export { FormLabel };
|
|
113
|
+
//# sourceMappingURL=FormLabel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormLabel.js","sources":["../../src/FormLabel.tsx"],"sourcesContent":[null],"names":["_role","_prefix","getPrefixCls","FormLabel","forwardRef","props","ref","useFormContext","labelWidthContext","labelWidth","labelPlacement","colonContext","colon","contentPositionContext","contentPosition","prefixCls","role","className","styleProp","style","children","label","required","labelWidthProp","rest","useMemo","controlWidth","isNumeric","Number","contentPositionMemo","colonMemo","cls","cx","alignItems","React","width","__DEV__","displayName"],"mappings":";;;;;;;;;;;;;;;AAOA,IAAMA,KAAK,GAAG,YAAd;;AACA,IAAMC,OAAO,GAAGC,YAAY,CAACF,KAAD,CAA5B;AAEA;;;;;IAGaG,SAAS,gBAAGC,UAAU,CAAwC,UAACC,KAAD,EAAQC,GAAR;wBAUrEC,cAAc;MAPJC,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,EAAE,CACZjB,SADY,EAEZE,SAFY,EAGZK,QAAQ,IAAOP,SAAP,eAHI,EAIZL,cAAc,IAAOK,SAAP,iBAAOA,GAAwBL,cAJjC;;AAAA;MASRS,KAAK,mCAAQD;AAAWe,IAAAA,UAAU,EAAEJ;;sBAGxCK,mBAAAA,MAAAA;AAAK5B,IAAAA,GAAG,EAAEA;AAAKU,IAAAA,IAAI,EAAEA;AAAMC,IAAAA,SAAS,EAAEc;AAAKZ,IAAAA,KAAK,EAAEA;KAAWK,KAA7DU,EACGb,KAAK,gBACJa,mBAAAA,QAAAA;AAAOjB,IAAAA,SAAS,EAAKF,SAAL;AAAwBI,IAAAA,KAAK,EAAE;AAAEgB,MAAAA,KAAK,EAAE1B;AAAT;GAA/CyB,EACGb,KADHa,EAEGJ,SAFHI,CADI,gBAMJA,mBAAAA,OAAAA;AAAMjB,IAAAA,SAAS,EAAKF,SAAL;AAA0BI,IAAAA,KAAK,EAAE;AAAEgB,MAAAA,KAAK,EAAE1B;AAAT;GAAhDyB,CAPJA,eASEA,mBAAAA,MAAAA;AAAKjB,IAAAA,SAAS,EAAKF,SAAL;AAA2BI,IAAAA,KAAK,EAAE;AAAEgB,MAAAA,KAAK,EAAET;AAAT;GAAhDQ,EACGd,QADHc,CATFA;AAvE+B,CAAA;;AA8GnC,IAAIE,OAAJ,EAAa;AACXjC,EAAAA,SAAS,CAACkC,WAAVlC,GAAwB,WAAxBA;;;"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/form
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/form#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HIUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
import React from 'react';
|
|
11
|
+
import { __DEV__ } from '@hi-ui/env';
|
|
12
|
+
import { useFormContext } from './context.js';
|
|
13
|
+
import { setNested, getNested } from '@hi-ui/func-utils';
|
|
14
|
+
import { isArray, isArrayNonEmpty } from '@hi-ui/type-assertion';
|
|
15
|
+
var fieldListSymbol = Symbol('field-list');
|
|
16
|
+
/**
|
|
17
|
+
* TODO: What is FormList
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
var FormList = function FormList(_ref) {
|
|
21
|
+
var children = _ref.children,
|
|
22
|
+
nameProp = _ref.name;
|
|
23
|
+
|
|
24
|
+
var _useFormContext = useFormContext(),
|
|
25
|
+
values = _useFormContext.values,
|
|
26
|
+
setFormState = _useFormContext.setFormState; // 唯一 id 生成器
|
|
27
|
+
// const listCountRef = React.useRef(0)
|
|
28
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
var name = isArray(nameProp) ? nameProp : [nameProp];
|
|
32
|
+
/**
|
|
33
|
+
* form List 动态字段更新器
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
var updateFormList = React.useCallback(function (stateFunc, alterTouched, alterErrors) {
|
|
37
|
+
setFormState(function (prev) {
|
|
38
|
+
var values = setNested(prev.values, name, stateFunc(getNested(prev.values, name)));
|
|
39
|
+
console.log(values, stateFunc(getNested(prev.values, name)));
|
|
40
|
+
var updateErrors = typeof alterErrors === 'function' ? alterErrors : stateFunc;
|
|
41
|
+
var updateTouched = typeof alterTouched === 'function' ? alterTouched : stateFunc;
|
|
42
|
+
var fieldError = alterErrors ? updateErrors(getNested(prev.errors, name)) : undefined;
|
|
43
|
+
var fieldTouched = alterTouched ? updateTouched(getNested(prev.touched, name)) : undefined;
|
|
44
|
+
|
|
45
|
+
if (!isArrayNonEmpty(fieldError)) {
|
|
46
|
+
fieldError = undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (!isArrayNonEmpty(fieldTouched)) {
|
|
50
|
+
fieldTouched = undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
values: values,
|
|
55
|
+
errors: alterErrors ? setNested(prev.errors, name, fieldError) : prev.errors,
|
|
56
|
+
touched: alterTouched ? setNested(prev.touched, name, fieldTouched) : prev.touched
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
}, [name, setFormState]);
|
|
60
|
+
var add = React.useCallback(function (value) {
|
|
61
|
+
// 维护的 动态 field list
|
|
62
|
+
updateFormList(function (prev) {
|
|
63
|
+
return [].concat(asArray(prev), [value]);
|
|
64
|
+
}, false, false);
|
|
65
|
+
}, [updateFormList]);
|
|
66
|
+
var remove = React.useCallback(function (index) {
|
|
67
|
+
updateFormList(function (prev) {
|
|
68
|
+
return asArray(prev).filter(function (_, idx) {
|
|
69
|
+
return idx !== index;
|
|
70
|
+
});
|
|
71
|
+
}, true, true);
|
|
72
|
+
}, [updateFormList]);
|
|
73
|
+
var insertBefore = React.useCallback(function (index, value) {
|
|
74
|
+
updateFormList(function (prev) {
|
|
75
|
+
return insert(asArray(prev), index, value);
|
|
76
|
+
}, function (prev) {
|
|
77
|
+
return insert(asArray(prev), index, null);
|
|
78
|
+
}, function (prev) {
|
|
79
|
+
return insert(asArray(prev), index, null);
|
|
80
|
+
});
|
|
81
|
+
}, [updateFormList]);
|
|
82
|
+
var swap = React.useCallback(function (aIndex, bIndex) {
|
|
83
|
+
updateFormList(function (prev) {
|
|
84
|
+
var copy = [].concat(asArray(prev));
|
|
85
|
+
var temp = copy[aIndex];
|
|
86
|
+
copy[aIndex] = copy[bIndex];
|
|
87
|
+
copy[bIndex] = temp;
|
|
88
|
+
return copy;
|
|
89
|
+
}, true, true);
|
|
90
|
+
}, [updateFormList]);
|
|
91
|
+
var move = React.useCallback(function (fromIndex, toIndex) {
|
|
92
|
+
updateFormList(function (prev) {
|
|
93
|
+
var copy = [].concat(asArray(prev));
|
|
94
|
+
var temp = copy[fromIndex];
|
|
95
|
+
copy.splice(fromIndex, 1);
|
|
96
|
+
copy.splice(toIndex, 0, temp);
|
|
97
|
+
return copy;
|
|
98
|
+
}, true, true);
|
|
99
|
+
}, [updateFormList]);
|
|
100
|
+
var listFields = React.useMemo(function () {
|
|
101
|
+
var list = getNested(values, name) || []; // @ts-ignore
|
|
102
|
+
|
|
103
|
+
return list.map(function (value, index) {
|
|
104
|
+
return {
|
|
105
|
+
name: '' + index,
|
|
106
|
+
// field: ++listCountRef.current + '',
|
|
107
|
+
symbol: fieldListSymbol,
|
|
108
|
+
value: value
|
|
109
|
+
};
|
|
110
|
+
});
|
|
111
|
+
}, [values, name]);
|
|
112
|
+
|
|
113
|
+
if (typeof children !== 'function') {
|
|
114
|
+
if (__DEV__) {
|
|
115
|
+
console.warn(false, 'FormList only accepts a render function as children.');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return children(listFields, {
|
|
122
|
+
add: add,
|
|
123
|
+
remove: remove,
|
|
124
|
+
swap: swap,
|
|
125
|
+
insertBefore: insertBefore,
|
|
126
|
+
move: move
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
if (__DEV__) {
|
|
131
|
+
FormList.displayName = 'FormList';
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
var insert = function insert(arr, index, value) {
|
|
135
|
+
arr = [].concat(arr);
|
|
136
|
+
arr.splice(index, 0, value);
|
|
137
|
+
return arr;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
var asArray = function asArray(arr) {
|
|
141
|
+
return isArray(arr) ? arr : [];
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export { FormList };
|
|
145
|
+
//# sourceMappingURL=FormList.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormList.js","sources":["../../src/FormList.tsx"],"sourcesContent":[null],"names":["fieldListSymbol","Symbol","FormList","children","nameProp","name","useFormContext","values","setFormState","isArray","updateFormList","React","useCallback","stateFunc","alterTouched","alterErrors","prev","setNested","getNested","console","log","updateErrors","updateTouched","fieldError","errors","undefined","fieldTouched","touched","isArrayNonEmpty","add","value","asArray","remove","index","filter","_","idx","insertBefore","insert","swap","aIndex","bIndex","copy","temp","move","fromIndex","toIndex","splice","listFields","useMemo","list","map","symbol","__DEV__","warn","displayName","arr"],"mappings":";;;;;;;;;;;;;;AAOA,IAAMA,eAAe,GAAGC,MAAM,CAAC,YAAD,CAA9B;AAEA;;;;IAGaC,QAAQ,GAA4B,SAApCA,QAAoC,KAAA;MAAGC,gBAAAA;MAAgBC,gBAANC;;wBAC3BC,cAAc;MAAvCC,MAAR,mBAAQA;MAAQC,YAAhB,mBAAgBA;;;;;MAMVH,IAAI,GAAGI,OAAO,CAACL,QAAD,CAAPK,GAAoBL,QAApBK,GAA+B,CAACL,QAAD;;;;;MAKtCM,cAAc,GAAGC,KAAK,CAACC,WAAND,CACrB,UAACE,SAAD,EAAsBC,YAAtB,EAAwDC,WAAxD;AACEP,IAAAA,YAAY,CAAC,UAACQ,IAAD;UACLT,MAAM,GAAGU,SAAS,CAACD,IAAI,CAACT,MAAN,EAAcF,IAAd,EAAoBQ,SAAS,CAACK,SAAS,CAACF,IAAI,CAACT,MAAN,EAAcF,IAAd,CAAV,CAA7B;AACxBc,MAAAA,OAAO,CAACC,GAARD,CAAYZ,MAAZY,EAAoBN,SAAS,CAACK,SAAS,CAACF,IAAI,CAACT,MAAN,EAAcF,IAAd,CAAV,CAA7Bc;UAEME,YAAY,GAAG,OAAON,WAAP,KAAuB,UAAvB,GAAoCA,WAApC,GAAkDF;UACjES,aAAa,GAAG,OAAOR,YAAP,KAAwB,UAAxB,GAAqCA,YAArC,GAAoDD;UAEtEU,UAAU,GAAGR,WAAW,GAAGM,YAAY,CAACH,SAAS,CAACF,IAAI,CAACQ,MAAN,EAAcnB,IAAd,CAAV,CAAf,GAAgDoB;UACxEC,YAAY,GAAGZ,YAAY,GAAGQ,aAAa,CAACJ,SAAS,CAACF,IAAI,CAACW,O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Copyright (c) HIUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
import { __rest } from 'tslib';
|
|
11
|
+
import React, { forwardRef } from 'react';
|
|
12
|
+
import { getPrefixCls, cx } from '@hi-ui/classname';
|
|
13
|
+
import { __DEV__ } from '@hi-ui/env';
|
|
14
|
+
import { useFormContext } from './context.js';
|
|
15
|
+
var _role = 'form-message';
|
|
16
|
+
|
|
17
|
+
var _prefix = getPrefixCls(_role);
|
|
18
|
+
/**
|
|
19
|
+
* TODO: What is FormMessage
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
var FormMessage = /*#__PURE__*/forwardRef(function (_a, ref) {
|
|
24
|
+
var _a$prefixCls = _a.prefixCls,
|
|
25
|
+
prefixCls = _a$prefixCls === void 0 ? _prefix : _a$prefixCls,
|
|
26
|
+
_a$role = _a.role,
|
|
27
|
+
role = _a$role === void 0 ? _role : _a$role,
|
|
28
|
+
className = _a.className,
|
|
29
|
+
field = _a.field,
|
|
30
|
+
rest = __rest(_a, ["prefixCls", "role", "className", "children", "field"]);
|
|
31
|
+
|
|
32
|
+
var _useFormContext = useFormContext(),
|
|
33
|
+
getFieldError = _useFormContext.getFieldError; // field 支持数组,递归去设置或者获取对象中的属性值
|
|
34
|
+
// 如何变成映射的 key,特殊处理化
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
var message = field ? getFieldError(field) : null;
|
|
38
|
+
var cls = cx(prefixCls, className);
|
|
39
|
+
return message ? /*#__PURE__*/React.createElement("span", Object.assign({
|
|
40
|
+
ref: ref,
|
|
41
|
+
role: role,
|
|
42
|
+
className: cls
|
|
43
|
+
}, rest), message) : null;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
if (__DEV__) {
|
|
47
|
+
FormMessage.displayName = 'FormMessage';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { FormMessage };
|
|
51
|
+
//# sourceMappingURL=FormMessage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormMessage.js","sources":["../../src/FormMessage.tsx"],"sourcesContent":[null],"names":["_role","_prefix","getPrefixCls","FormMessage","forwardRef","_a","ref","prefixCls","role","className","field","rest","useFormContext","getFieldError","message","cls","cx","React","__DEV__","displayName"],"mappings":";;;;;;;;;;;;;;AAKA,IAAMA,KAAK,GAAG,cAAd;;AACA,IAAMC,OAAO,GAAGC,YAAY,CAACF,KAAD,CAA5B;AAEA;;;;;IAGaG,WAAW,gBAAGC,UAAU,CACnC,UAACC,EAAD,EAA6EC,GAA7E;wBAAGC;MAAAA,sCAAYN;mBAASO;MAAAA,4BAAOR;MAAOS,eAAAA;MAAqBC,WAAAA;MAAUC,kBAApE,YAAA,QAAA,aAAA,YAAA,SAAA;;wBAC2BC,cAAc;MAAhCC,aAAR,mBAAQA;;;;MAIFC,OAAO,GAAGJ,KAAK,GAAGG,aAAa,CAACH,KAAD,CAAhB,GAA0B;MACzCK,GAAG,GAAGC,EAAE,CAACT,SAAD,EAAYE,SAAZ;SAEPK,OAAO,gBACZG,mBAAAA,OAAAA;AAAMX,IAAAA,GAAG,EAAEA;AAAKE,IAAAA,IAAI,EAAEA;AAAMC,IAAAA,SAAS,EAAEM;KAASJ,KAAhDM,EAEGH,OAFHG,CADY,GAKV;AAd6B,CAAA;;AAyCrC,IAAIC,OAAJ,EAAa;AACXf,EAAAA,WAAW,CAACgB,WAAZhB,GAA0B,aAA1BA;;;"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/form
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/form#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HIUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
import { useContext, createContext } from 'react';
|
|
11
|
+
var formContext = /*#__PURE__*/createContext(null);
|
|
12
|
+
var FormProvider = formContext.Provider;
|
|
13
|
+
|
|
14
|
+
var useFormContext = function useFormContext() {
|
|
15
|
+
var context = useContext(formContext);
|
|
16
|
+
|
|
17
|
+
if (!context) {
|
|
18
|
+
throw new Error('The FormContext should using in Form.');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return context;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export { FormProvider, useFormContext };
|
|
25
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sources":["../../src/context.ts"],"sourcesContent":[null],"names":["formContext","createContext","FormProvider","Provider","useFormContext","context","useContext","Error"],"mappings":";;;;;;;;;;AAIA,IAAMA,WAAW,gBAAGC,aAAa,CAA0C,IAA1C,CAAjC;IAEaC,YAAY,GAAGF,WAAW,CAACG;;IAE3BC,cAAc,GAAG,SAAjBA,cAAiB;MACtBC,OAAO,GAAGC,UAAU,CAACN,WAAD;;MAEtB,CAACK,SAAS;UACN,IAAIE,KAAJ,CAAU,uCAAV;;;SAGDF;;;"}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/form
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/form#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HIUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
import './styles/index.scss.js';
|
|
11
|
+
export { FORM_REGISTER_TABLE, Form, Form as default } from './Form.js';
|
|
12
|
+
export { FormItem } from './FormItem.js';
|
|
13
|
+
export { FormList } from './FormList.js';
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/form
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/form#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HIUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
var css_248z = "@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n@use 'sass:map';\n.hi-v4-form {\n max-width: 100%;\n position: relative; }\n.hi-v4-form fieldset {\n margin: 0;\n padding: 0;\n border: 0; }\n.hi-v4-form fieldset + fieldset {\n margin-top: 16px; }\n.hi-v4-form legend {\n margin: 0 0 16px;\n color: #1f2937;\n color: var(--hi-v4-color-gray-800, #1f2937); }\n.hi-v4-form--inline {\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n -ms-flex-wrap: wrap;\n flex-wrap: wrap; }\n.hi-v4-form--inline .hi-v4-form-label__indent {\n min-height: 32px; }\n.hi-v4-form-label {\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n font-size: 0.875rem;\n font-size: var(--hi-v4-text-size-normal, 0.875rem);\n margin-right: 8px;\n margin-right: var(--hi-v4-spacing-4, 8px);\n margin-bottom: 24px; }\n.hi-v4-form-label__text, .hi-v4-form-label__indent {\n -ms-flex-negative: 0;\n flex-shrink: 0;\n -webkit-box-sizing: border-box;\n box-sizing: border-box;\n vertical-align: top;\n color: #1f2937;\n color: var(--hi-v4-color-gray-800, #1f2937);\n overflow-wrap: break-word; }\n.hi-v4-form-label__text {\n margin-top: 5px;\n min-height: 22px;\n padding-right: 8px;\n padding-right: var(--hi-v4-spacing-4, 8px);\n line-height: 22px; }\n.hi-v4-form-label--placement-left .hi-v4-form-label__text {\n text-align: left; }\n.hi-v4-form-label--placement-right .hi-v4-form-label__text {\n text-align: right; }\n.hi-v4-form-label--placement-top .hi-v4-form-label__text {\n height: 22px;\n padding-right: 8px;\n margin-bottom: 8px; }\n.hi-v4-form-label--required .hi-v4-form-label__text::after {\n margin-right: 4px;\n margin-right: var(--hi-v4-spacing-2, 4px);\n content: '*';\n color: #237ffa;\n color: var(--hi-v4-color-brandblue-500, #237ffa); }\n.hi-v4-form-label .hi-v4-form-label--placement-top {\n padding-right: 0;\n text-align: left;\n -webkit-box-orient: vertical;\n -webkit-box-direction: normal;\n -ms-flex-direction: column;\n flex-direction: column;\n margin-bottom: 20px; }\n.hi-v4-form-label__content {\n -webkit-box-flex: 1;\n -ms-flex: 1 1;\n flex: 1 1;\n position: relative;\n vertical-align: top;\n min-height: 32px;\n -ms-flex-negative: 0;\n flex-shrink: 0; }\n.hi-v4-form-message {\n font-size: 0.875rem;\n font-size: var(--hi-v4-text-size-normal, 0.875rem);\n min-height: 24px;\n padding: 2px 0;\n -webkit-box-sizing: border-box;\n box-sizing: border-box;\n line-height: 20px;\n -webkit-transition: all 0.3s;\n transition: all 0.3s;\n -webkit-transform: translateY(-10%);\n transform: translateY(-10%);\n color: #ff5959;\n color: var(--hi-v4-color-red-500, #ff5959); }\n";
|
|
11
|
+
|
|
12
|
+
var __styleInject__ = require('style-inject/dist/style-inject.es.js')["default"];
|
|
13
|
+
|
|
14
|
+
__styleInject__(css_248z);
|
|
15
|
+
|
|
16
|
+
export default css_248z;
|
|
17
|
+
//# sourceMappingURL=index.scss.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.scss.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/** @LICENSE
|
|
2
|
+
* @hi-ui/form
|
|
3
|
+
* https://github.com/XiaoMi/hiui/tree/master/packages/ui/form#readme
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) HIUI <mi-hiui@xiaomi.com>.
|
|
6
|
+
*
|
|
7
|
+
* This source code is licensed under the MIT license found in the
|
|
8
|
+
* LICENSE file in the root directory of this source tree.
|
|
9
|
+
*/
|
|
10
|
+
import { useMemo, useCallback, useEffect } from 'react';
|
|
11
|
+
import { useFormContext } from './context.js';
|
|
12
|
+
import { isArrayNonEmpty } from '@hi-ui/type-assertion';
|
|
13
|
+
import Validater from 'async-validator';
|
|
14
|
+
import { toArray } from '@hi-ui/func-utils';
|
|
15
|
+
|
|
16
|
+
var useFormField = function useFormField(props) {
|
|
17
|
+
var field = props.field,
|
|
18
|
+
rulesProp = props.rules,
|
|
19
|
+
valueType = props.valueType;
|
|
20
|
+
|
|
21
|
+
var _useFormContext = useFormContext(),
|
|
22
|
+
getFieldRules = _useFormContext.getFieldRules,
|
|
23
|
+
getFieldProps = _useFormContext.getFieldProps,
|
|
24
|
+
registerField = _useFormContext.registerField,
|
|
25
|
+
unregisterField = _useFormContext.unregisterField;
|
|
26
|
+
/**
|
|
27
|
+
* 处理校验规则,item 优先级大于 form
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
var fieldRules = useMemo(function () {
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
var rules = toArray(rulesProp !== null && rulesProp !== void 0 ? rulesProp : getFieldRules(field)); // switch(valueType) {
|
|
34
|
+
// case: 'string':
|
|
35
|
+
// const scheme = yup.string()
|
|
36
|
+
// rules.reduce((scheme, { name, strategy, validator, message, transform }) => {
|
|
37
|
+
// if (typeof name === 'string' && name in scheme) {
|
|
38
|
+
// if (isFunction(transform)) {
|
|
39
|
+
// scheme = scheme.transform(transform)
|
|
40
|
+
// }
|
|
41
|
+
// // @ts-ignore
|
|
42
|
+
// return scheme[name](message)
|
|
43
|
+
// }
|
|
44
|
+
// }, scheme)
|
|
45
|
+
// case: 'number':
|
|
46
|
+
// case: 'boolean':
|
|
47
|
+
// case: 'array':
|
|
48
|
+
// case: 'object':
|
|
49
|
+
// case: 'date':
|
|
50
|
+
// }
|
|
51
|
+
|
|
52
|
+
return rules.map(function (rule) {
|
|
53
|
+
return Object.assign({
|
|
54
|
+
type: valueType
|
|
55
|
+
}, rule);
|
|
56
|
+
});
|
|
57
|
+
}, [rulesProp, field, getFieldRules, valueType]); // 当前 field 的唯一校验器
|
|
58
|
+
|
|
59
|
+
var fieldValidate = useCallback(function (value) {
|
|
60
|
+
var _Validater, _validater$validate;
|
|
61
|
+
|
|
62
|
+
if (!isArrayNonEmpty(fieldRules)) {
|
|
63
|
+
return Promise.resolve(null);
|
|
64
|
+
} // TODO: rules 处理成 Async Validate 的指定结构
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
console.log('fieldRules', fieldRules, 'value:', value);
|
|
68
|
+
var validater = new Validater((_Validater = {}, _Validater[field] = fieldRules, _Validater)); // @ts-ignore
|
|
69
|
+
|
|
70
|
+
return validater.validate((_validater$validate = {}, _validater$validate[field] = value, _validater$validate), {
|
|
71
|
+
firstFields: true
|
|
72
|
+
}, console.log);
|
|
73
|
+
}, [fieldRules, field]); // 注入当前 field 及其验证规则到 Form
|
|
74
|
+
|
|
75
|
+
useEffect(function () {
|
|
76
|
+
if (field) {
|
|
77
|
+
// @ts-ignore
|
|
78
|
+
registerField(field, {
|
|
79
|
+
validate: fieldValidate
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return function () {
|
|
84
|
+
if (field) {
|
|
85
|
+
// @ts-ignore
|
|
86
|
+
unregisterField(field);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}, [registerField, unregisterField, field, fieldValidate]);
|
|
90
|
+
return getFieldProps(props);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export { useFormField };
|
|
94
|
+
//# sourceMappingURL=use-form-field.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-form-field.js","sources":["../../src/use-form-field.ts"],"sourcesContent":[null],"names":["useFormField","props","field","rulesProp","rules","valueType","useFormContext","getFieldRules","getFieldProps","registerField","unregisterField","fieldRules","useMemo","toArray","map","rule","type","fieldValidate","useCallback","value","isArrayNonEmpty","Promise","resolve","console","log","validater","Validater","validate","firstFields","useEffect"],"mappings":";;;;;;;;;;;;;;;IAQaA,YAAY,GAAG,SAAfA,YAAe,CAAeC,KAAf;MAClBC,KAAR,GAA+CD,KAA/C,CAAQC;MAAcC,SAAtB,GAA+CF,KAA/C,CAAeG;MAAkBC,SAAjC,GAA+CJ,KAA/C,CAAiCI;;wBAW7BC,cAAc;MAJhBC,aALF,mBAKEA;MACAC,aANF,mBAMEA;MACAC,aAPF,mBAOEA;MACAC,eARF,mBAQEA;;;;;;MAMIC,UAAU,GAAGC,OAAO,CAAC;;QAEnBR,KAAK,GAAGS,OAAO,CAACV,SAAS,SAATA,IAAAA,SAAS,WAATA,GAAAA,SAAAA,GAAaI,aAAa,CAACL,KAAD,CAA3B;;;;;;;;;;;;;;;;;;;WAqBdE,KAAK,CAACU,GAANV,CAAU,UAACW,IAAD;;AAAaC,QAAAA,IAAI,EAAEX;SAAcU;AAA3C,KAAAX;AAvBiB,GAAA,EAwBvB,CAACD,SAAD,EAAYD,KAAZ,EAAmBK,aAAnB,EAAkCF,SAAlC,CAxBuB;;MA2BpBY,aAAa,GAAGC,WAAW,CAC/B,UAACC,KAAD;;;QACM,CAACC,eAAe,CAACT,UAAD,GAAc;aACzBU,OAAO,CAACC,OAARD,CAAgB,IAAhBA;;;;AAITE,IAAAA,OAAO,CAACC,GAARD,CAAY,YAAZA,EAA0BZ,UAA1BY,EAAsC,QAAtCA,EAAgDJ,KAAhDI;QAEME,SAAS,GAAG,IAAIC,SAAJ,8BAEfxB,SAAQS,sBAFO;;WAKXc,SAAS,CAACE,QAAVF,gDAAsBvB,SAAQiB,0BAA9BM,GAAuC;AAAEG,MAAAA,WAAW,EA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