File size: 7,251 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
import * as React from 'react';
import classNames from 'classnames';
import FieldForm, { List, useWatch } from 'rc-field-form';
import type { FormProps as RcFormProps } from 'rc-field-form/lib/Form';
import type { FormRef, InternalNamePath, ValidateErrorEntity } from 'rc-field-form/lib/interface';
import type { Variant } from '../config-provider';
import { useComponentConfig } from '../config-provider/context';
import DisabledContext, { DisabledContextProvider } from '../config-provider/DisabledContext';
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
import useSize from '../config-provider/hooks/useSize';
import type { SizeType } from '../config-provider/SizeContext';
import SizeContext from '../config-provider/SizeContext';
import type { ColProps } from '../grid/col';
import type { FormContextProps } from './context';
import { FormContext, FormProvider, VariantContext } from './context';
import type { FeedbackIcons } from './FormItem';
import useForm from './hooks/useForm';
import type { FormInstance } from './hooks/useForm';
import useFormWarning from './hooks/useFormWarning';
import type { FormLabelAlign, ScrollFocusOptions } from './interface';
import useStyle from './style';
import ValidateMessagesContext from './validateMessagesContext';
export type RequiredMark =
| boolean
| 'optional'
| ((labelNode: React.ReactNode, info: { required: boolean }) => React.ReactNode);
export type FormLayout = 'horizontal' | 'inline' | 'vertical';
export type FormItemLayout = 'horizontal' | 'vertical';
export type { ScrollFocusOptions };
export interface FormProps<Values = any> extends Omit<RcFormProps<Values>, 'form'> {
prefixCls?: string;
colon?: boolean;
name?: string;
layout?: FormLayout;
labelAlign?: FormLabelAlign;
labelWrap?: boolean;
labelCol?: ColProps;
wrapperCol?: ColProps;
form?: FormInstance<Values>;
feedbackIcons?: FeedbackIcons;
size?: SizeType;
disabled?: boolean;
scrollToFirstError?: ScrollFocusOptions | boolean;
requiredMark?: RequiredMark;
/** @deprecated Will warning in future branch. Pls use `requiredMark` instead. */
hideRequiredMark?: boolean;
rootClassName?: string;
variant?: Variant;
}
const InternalForm: React.ForwardRefRenderFunction<FormRef, FormProps> = (props, ref) => {
const contextDisabled = React.useContext(DisabledContext);
const {
getPrefixCls,
direction,
requiredMark: contextRequiredMark,
colon: contextColon,
scrollToFirstError: contextScrollToFirstError,
className: contextClassName,
style: contextStyle,
} = useComponentConfig('form');
const {
prefixCls: customizePrefixCls,
className,
rootClassName,
size,
disabled = contextDisabled,
form,
colon,
labelAlign,
labelWrap,
labelCol,
wrapperCol,
hideRequiredMark,
layout = 'horizontal',
scrollToFirstError,
requiredMark,
onFinishFailed,
name,
style,
feedbackIcons,
variant,
...restFormProps
} = props;
const mergedSize = useSize(size);
const contextValidateMessages = React.useContext(ValidateMessagesContext);
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
useFormWarning(props);
}
const mergedRequiredMark = React.useMemo(() => {
if (requiredMark !== undefined) {
return requiredMark;
}
if (hideRequiredMark) {
return false;
}
if (contextRequiredMark !== undefined) {
return contextRequiredMark;
}
return true;
}, [hideRequiredMark, requiredMark, contextRequiredMark]);
const mergedColon = colon ?? contextColon;
const prefixCls = getPrefixCls('form', customizePrefixCls);
// Style
const rootCls = useCSSVarCls(prefixCls);
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
const formClassName = classNames(
prefixCls,
`${prefixCls}-${layout}`,
{
[`${prefixCls}-hide-required-mark`]: mergedRequiredMark === false, // todo: remove in next major version
[`${prefixCls}-rtl`]: direction === 'rtl',
[`${prefixCls}-${mergedSize}`]: mergedSize,
},
cssVarCls,
rootCls,
hashId,
contextClassName,
className,
rootClassName,
);
const [wrapForm] = useForm(form);
const { __INTERNAL__ } = wrapForm;
__INTERNAL__.name = name;
const formContextValue = React.useMemo<FormContextProps>(
() => ({
name,
labelAlign,
labelCol,
labelWrap,
wrapperCol,
vertical: layout === 'vertical',
colon: mergedColon,
requiredMark: mergedRequiredMark,
itemRef: __INTERNAL__.itemRef,
form: wrapForm,
feedbackIcons,
}),
[
name,
labelAlign,
labelCol,
wrapperCol,
layout,
mergedColon,
mergedRequiredMark,
wrapForm,
feedbackIcons,
],
);
const nativeElementRef = React.useRef<FormRef>(null);
React.useImperativeHandle(ref, () => ({
...wrapForm,
nativeElement: nativeElementRef.current?.nativeElement,
}));
const scrollToField = (options: ScrollFocusOptions | boolean, fieldName: InternalNamePath) => {
if (options) {
let defaultScrollToFirstError: ScrollFocusOptions = { block: 'nearest' };
if (typeof options === 'object') {
defaultScrollToFirstError = { ...defaultScrollToFirstError, ...options };
}
wrapForm.scrollToField(fieldName, defaultScrollToFirstError);
}
};
const onInternalFinishFailed = (errorInfo: ValidateErrorEntity) => {
onFinishFailed?.(errorInfo);
if (errorInfo.errorFields.length) {
const fieldName = errorInfo.errorFields[0].name;
if (scrollToFirstError !== undefined) {
scrollToField(scrollToFirstError, fieldName);
return;
}
if (contextScrollToFirstError !== undefined) {
scrollToField(contextScrollToFirstError, fieldName);
}
}
};
return wrapCSSVar(
<VariantContext.Provider value={variant}>
<DisabledContextProvider disabled={disabled}>
<SizeContext.Provider value={mergedSize}>
<FormProvider
{...{
// This is not list in API, we pass with spread
validateMessages: contextValidateMessages,
}}
>
<FormContext.Provider value={formContextValue}>
<FieldForm
id={name}
{...restFormProps}
name={name}
onFinishFailed={onInternalFinishFailed}
form={wrapForm}
ref={nativeElementRef}
style={{ ...contextStyle, ...style }}
className={formClassName}
/>
</FormContext.Provider>
</FormProvider>
</SizeContext.Provider>
</DisabledContextProvider>
</VariantContext.Provider>,
);
};
const Form = React.forwardRef<FormRef, FormProps>(InternalForm) as (<Values = any>(
props: React.PropsWithChildren<FormProps<Values>> & React.RefAttributes<FormRef<Values>>,
) => React.ReactElement) &
Pick<React.FC, 'displayName'>;
if (process.env.NODE_ENV !== 'production') {
Form.displayName = 'Form';
}
export { List, useForm, useWatch, type FormInstance };
export default Form;
|