1 | "use client";
|
2 |
|
3 | var __rest = this && this.__rest || function (s, e) {
|
4 | var t = {};
|
5 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
|
6 | if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
7 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | import * as React from 'react';
|
12 | import { useMemo } from 'react';
|
13 | import classNames from 'classnames';
|
14 | import FieldForm, { List, useWatch } from 'rc-field-form';
|
15 | import { ConfigContext } from '../config-provider';
|
16 | import DisabledContext, { DisabledContextProvider } from '../config-provider/DisabledContext';
|
17 | import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
|
18 | import useSize from '../config-provider/hooks/useSize';
|
19 | import SizeContext from '../config-provider/SizeContext';
|
20 | import { FormContext, FormProvider, VariantContext } from './context';
|
21 | import useForm from './hooks/useForm';
|
22 | import useFormWarning from './hooks/useFormWarning';
|
23 | import useStyle from './style';
|
24 | import ValidateMessagesContext from './validateMessagesContext';
|
25 | const InternalForm = (props, ref) => {
|
26 | const contextDisabled = React.useContext(DisabledContext);
|
27 | const {
|
28 | getPrefixCls,
|
29 | direction,
|
30 | form: contextForm
|
31 | } = React.useContext(ConfigContext);
|
32 | const {
|
33 | prefixCls: customizePrefixCls,
|
34 | className,
|
35 | rootClassName,
|
36 | size,
|
37 | disabled = contextDisabled,
|
38 | form,
|
39 | colon,
|
40 | labelAlign,
|
41 | labelWrap,
|
42 | labelCol,
|
43 | wrapperCol,
|
44 | hideRequiredMark,
|
45 | layout = 'horizontal',
|
46 | scrollToFirstError,
|
47 | requiredMark,
|
48 | onFinishFailed,
|
49 | name,
|
50 | style,
|
51 | feedbackIcons,
|
52 | variant
|
53 | } = props,
|
54 | restFormProps = __rest(props, ["prefixCls", "className", "rootClassName", "size", "disabled", "form", "colon", "labelAlign", "labelWrap", "labelCol", "wrapperCol", "hideRequiredMark", "layout", "scrollToFirstError", "requiredMark", "onFinishFailed", "name", "style", "feedbackIcons", "variant"]);
|
55 | const mergedSize = useSize(size);
|
56 | const contextValidateMessages = React.useContext(ValidateMessagesContext);
|
57 | if (process.env.NODE_ENV !== 'production') {
|
58 |
|
59 | useFormWarning(props);
|
60 | }
|
61 | const mergedRequiredMark = useMemo(() => {
|
62 | if (requiredMark !== undefined) {
|
63 | return requiredMark;
|
64 | }
|
65 | if (hideRequiredMark) {
|
66 | return false;
|
67 | }
|
68 | if (contextForm && contextForm.requiredMark !== undefined) {
|
69 | return contextForm.requiredMark;
|
70 | }
|
71 | return true;
|
72 | }, [hideRequiredMark, requiredMark, contextForm]);
|
73 | const mergedColon = colon !== null && colon !== void 0 ? colon : contextForm === null || contextForm === void 0 ? void 0 : contextForm.colon;
|
74 | const prefixCls = getPrefixCls('form', customizePrefixCls);
|
75 |
|
76 | const rootCls = useCSSVarCls(prefixCls);
|
77 | const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
|
78 | const formClassName = classNames(prefixCls, `${prefixCls}-${layout}`, {
|
79 | [`${prefixCls}-hide-required-mark`]: mergedRequiredMark === false,
|
80 | [`${prefixCls}-rtl`]: direction === 'rtl',
|
81 | [`${prefixCls}-${mergedSize}`]: mergedSize
|
82 | }, cssVarCls, rootCls, hashId, contextForm === null || contextForm === void 0 ? void 0 : contextForm.className, className, rootClassName);
|
83 | const [wrapForm] = useForm(form);
|
84 | const {
|
85 | __INTERNAL__
|
86 | } = wrapForm;
|
87 | __INTERNAL__.name = name;
|
88 | const formContextValue = useMemo(() => ({
|
89 | name,
|
90 | labelAlign,
|
91 | labelCol,
|
92 | labelWrap,
|
93 | wrapperCol,
|
94 | vertical: layout === 'vertical',
|
95 | colon: mergedColon,
|
96 | requiredMark: mergedRequiredMark,
|
97 | itemRef: __INTERNAL__.itemRef,
|
98 | form: wrapForm,
|
99 | feedbackIcons
|
100 | }), [name, labelAlign, labelCol, wrapperCol, layout, mergedColon, mergedRequiredMark, wrapForm, feedbackIcons]);
|
101 | React.useImperativeHandle(ref, () => wrapForm);
|
102 | const scrollToField = (options, fieldName) => {
|
103 | if (options) {
|
104 | let defaultScrollToFirstError = {
|
105 | block: 'nearest'
|
106 | };
|
107 | if (typeof options === 'object') {
|
108 | defaultScrollToFirstError = options;
|
109 | }
|
110 | wrapForm.scrollToField(fieldName, defaultScrollToFirstError);
|
111 | }
|
112 | };
|
113 | const onInternalFinishFailed = errorInfo => {
|
114 | onFinishFailed === null || onFinishFailed === void 0 ? void 0 : onFinishFailed(errorInfo);
|
115 | if (errorInfo.errorFields.length) {
|
116 | const fieldName = errorInfo.errorFields[0].name;
|
117 | if (scrollToFirstError !== undefined) {
|
118 | scrollToField(scrollToFirstError, fieldName);
|
119 | return;
|
120 | }
|
121 | if (contextForm && contextForm.scrollToFirstError !== undefined) {
|
122 | scrollToField(contextForm.scrollToFirstError, fieldName);
|
123 | }
|
124 | }
|
125 | };
|
126 | return wrapCSSVar( React.createElement(VariantContext.Provider, {
|
127 | value: variant
|
128 | }, React.createElement(DisabledContextProvider, {
|
129 | disabled: disabled
|
130 | }, React.createElement(SizeContext.Provider, {
|
131 | value: mergedSize
|
132 | }, React.createElement(FormProvider, {
|
133 |
|
134 | validateMessages: contextValidateMessages
|
135 | }, React.createElement(FormContext.Provider, {
|
136 | value: formContextValue
|
137 | }, React.createElement(FieldForm, Object.assign({
|
138 | id: name
|
139 | }, restFormProps, {
|
140 | name: name,
|
141 | onFinishFailed: onInternalFinishFailed,
|
142 | form: wrapForm,
|
143 | style: Object.assign(Object.assign({}, contextForm === null || contextForm === void 0 ? void 0 : contextForm.style), style),
|
144 | className: formClassName
|
145 | }))))))));
|
146 | };
|
147 | const Form = React.forwardRef(InternalForm);
|
148 | if (process.env.NODE_ENV !== 'production') {
|
149 | Form.displayName = 'Form';
|
150 | }
|
151 | export { List, useForm, useWatch };
|
152 | export default Form; |
\ | No newline at end of file |