UNPKG

4.87 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _typeof from "@babel/runtime/helpers/esm/typeof";
3import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
4import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
5
6var __rest = this && this.__rest || function (s, e) {
7 var t = {};
8
9 for (var p in s) {
10 if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
11 }
12
13 if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
14 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
15 }
16 return t;
17};
18
19import * as React from 'react';
20import { useMemo } from 'react';
21import classNames from 'classnames';
22import FieldForm, { List } from 'rc-field-form';
23import { ConfigContext } from '../config-provider';
24import { FormContext } from './context';
25import useForm from './hooks/useForm';
26import SizeContext, { SizeContextProvider } from '../config-provider/SizeContext';
27
28var InternalForm = function InternalForm(props, ref) {
29 var _classNames;
30
31 var contextSize = React.useContext(SizeContext);
32
33 var _React$useContext = React.useContext(ConfigContext),
34 getPrefixCls = _React$useContext.getPrefixCls,
35 direction = _React$useContext.direction,
36 contextForm = _React$useContext.form;
37
38 var customizePrefixCls = props.prefixCls,
39 _props$className = props.className,
40 className = _props$className === void 0 ? '' : _props$className,
41 _props$size = props.size,
42 size = _props$size === void 0 ? contextSize : _props$size,
43 form = props.form,
44 colon = props.colon,
45 labelAlign = props.labelAlign,
46 labelCol = props.labelCol,
47 wrapperCol = props.wrapperCol,
48 hideRequiredMark = props.hideRequiredMark,
49 _props$layout = props.layout,
50 layout = _props$layout === void 0 ? 'horizontal' : _props$layout,
51 scrollToFirstError = props.scrollToFirstError,
52 requiredMark = props.requiredMark,
53 onFinishFailed = props.onFinishFailed,
54 name = props.name,
55 restFormProps = __rest(props, ["prefixCls", "className", "size", "form", "colon", "labelAlign", "labelCol", "wrapperCol", "hideRequiredMark", "layout", "scrollToFirstError", "requiredMark", "onFinishFailed", "name"]);
56
57 var mergedRequiredMark = useMemo(function () {
58 if (requiredMark !== undefined) {
59 return requiredMark;
60 }
61
62 if (contextForm && contextForm.requiredMark !== undefined) {
63 return contextForm.requiredMark;
64 }
65
66 if (hideRequiredMark) {
67 return false;
68 }
69
70 return true;
71 }, [hideRequiredMark, requiredMark, contextForm]);
72 var prefixCls = getPrefixCls('form', customizePrefixCls);
73 var formClassName = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(layout), true), _defineProperty(_classNames, "".concat(prefixCls, "-hide-required-mark"), mergedRequiredMark === false), _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(size), size), _classNames), className);
74
75 var _useForm = useForm(form),
76 _useForm2 = _slicedToArray(_useForm, 1),
77 wrapForm = _useForm2[0];
78
79 var __INTERNAL__ = wrapForm.__INTERNAL__;
80 __INTERNAL__.name = name;
81 var formContextValue = useMemo(function () {
82 return {
83 name: name,
84 labelAlign: labelAlign,
85 labelCol: labelCol,
86 wrapperCol: wrapperCol,
87 vertical: layout === 'vertical',
88 colon: colon,
89 requiredMark: mergedRequiredMark,
90 itemRef: __INTERNAL__.itemRef
91 };
92 }, [name, labelAlign, labelCol, wrapperCol, layout, colon, mergedRequiredMark]);
93 React.useImperativeHandle(ref, function () {
94 return wrapForm;
95 });
96
97 var onInternalFinishFailed = function onInternalFinishFailed(errorInfo) {
98 onFinishFailed === null || onFinishFailed === void 0 ? void 0 : onFinishFailed(errorInfo);
99 var defaultScrollToFirstError = {
100 block: 'nearest'
101 };
102
103 if (scrollToFirstError && errorInfo.errorFields.length) {
104 if (_typeof(scrollToFirstError) === 'object') {
105 defaultScrollToFirstError = scrollToFirstError;
106 }
107
108 wrapForm.scrollToField(errorInfo.errorFields[0].name, defaultScrollToFirstError);
109 }
110 };
111
112 return /*#__PURE__*/React.createElement(SizeContextProvider, {
113 size: size
114 }, /*#__PURE__*/React.createElement(FormContext.Provider, {
115 value: formContextValue
116 }, /*#__PURE__*/React.createElement(FieldForm, _extends({
117 id: name
118 }, restFormProps, {
119 name: name,
120 onFinishFailed: onInternalFinishFailed,
121 form: wrapForm,
122 className: formClassName
123 }))));
124};
125
126var Form = /*#__PURE__*/React.forwardRef(InternalForm);
127export { useForm, List };
128export default Form;
\No newline at end of file