2019-07-03 20:14:39 +08:00
|
|
|
import classNames from 'classnames';
|
2022-04-14 20:46:57 +08:00
|
|
|
import FieldForm, { List, useWatch } from 'rc-field-form';
|
2022-04-06 22:14:16 +08:00
|
|
|
import type { FormProps as RcFormProps } from 'rc-field-form/lib/Form';
|
2023-01-03 09:56:47 +08:00
|
|
|
import type { InternalNamePath, ValidateErrorEntity } from 'rc-field-form/lib/interface';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { useMemo } from 'react';
|
2022-04-06 22:14:16 +08:00
|
|
|
import type { Options } from 'scroll-into-view-if-needed';
|
2020-10-24 14:27:49 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-06-22 14:57:09 +08:00
|
|
|
import DisabledContext, { DisabledContextProvider } from '../config-provider/DisabledContext';
|
|
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
2023-05-12 14:53:47 +08:00
|
|
|
import { SizeContextProvider } from '../config-provider/SizeContext';
|
|
|
|
import useSize from '../config-provider/hooks/useSize';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { ColProps } from '../grid/col';
|
2022-04-06 22:14:16 +08:00
|
|
|
import type { FormContextProps } from './context';
|
|
|
|
import { FormContext } from './context';
|
2022-11-09 12:28:04 +08:00
|
|
|
import useForm, { type FormInstance } from './hooks/useForm';
|
2023-05-09 19:09:21 +08:00
|
|
|
import useFormWarning from './hooks/useFormWarning';
|
2023-05-12 14:53:47 +08:00
|
|
|
import type { FormLabelAlign } from './interface';
|
2022-03-30 14:13:36 +08:00
|
|
|
import useStyle from './style';
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2020-08-21 12:58:14 +08:00
|
|
|
export type RequiredMark = boolean | 'optional';
|
2019-07-03 20:14:39 +08:00
|
|
|
export type FormLayout = 'horizontal' | 'inline' | 'vertical';
|
|
|
|
|
2020-07-31 16:53:39 +08:00
|
|
|
export interface FormProps<Values = any> extends Omit<RcFormProps<Values>, 'form'> {
|
2019-07-03 20:14:39 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
colon?: boolean;
|
|
|
|
name?: string;
|
|
|
|
layout?: FormLayout;
|
|
|
|
labelAlign?: FormLabelAlign;
|
2021-11-26 18:15:59 +08:00
|
|
|
labelWrap?: boolean;
|
2019-07-03 20:14:39 +08:00
|
|
|
labelCol?: ColProps;
|
|
|
|
wrapperCol?: ColProps;
|
2020-07-31 16:53:39 +08:00
|
|
|
form?: FormInstance<Values>;
|
2020-01-03 13:38:16 +08:00
|
|
|
size?: SizeType;
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled?: boolean;
|
2020-12-11 23:25:21 +08:00
|
|
|
scrollToFirstError?: Options | boolean;
|
2020-08-21 12:58:14 +08:00
|
|
|
requiredMark?: RequiredMark;
|
|
|
|
/** @deprecated Will warning in future branch. Pls use `requiredMark` instead. */
|
|
|
|
hideRequiredMark?: boolean;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2019-07-03 20:14:39 +08:00
|
|
|
}
|
|
|
|
|
2021-06-03 09:36:04 +08:00
|
|
|
const InternalForm: React.ForwardRefRenderFunction<FormInstance, FormProps> = (props, ref) => {
|
2022-04-29 20:48:10 +08:00
|
|
|
const contextDisabled = React.useContext(DisabledContext);
|
2022-04-06 22:14:16 +08:00
|
|
|
const { getPrefixCls, direction, form: contextForm } = React.useContext(ConfigContext);
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
const {
|
2020-06-05 18:06:52 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2023-01-20 11:03:50 +08:00
|
|
|
className,
|
|
|
|
rootClassName,
|
2023-05-12 14:53:47 +08:00
|
|
|
size,
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled = contextDisabled,
|
2019-07-04 15:00:11 +08:00
|
|
|
form,
|
2019-07-03 20:14:39 +08:00
|
|
|
colon,
|
|
|
|
labelAlign,
|
2021-11-26 18:15:59 +08:00
|
|
|
labelWrap,
|
2019-07-03 20:14:39 +08:00
|
|
|
labelCol,
|
|
|
|
wrapperCol,
|
|
|
|
hideRequiredMark,
|
|
|
|
layout = 'horizontal',
|
2020-02-19 17:38:46 +08:00
|
|
|
scrollToFirstError,
|
2020-08-21 12:58:14 +08:00
|
|
|
requiredMark,
|
2020-02-19 17:38:46 +08:00
|
|
|
onFinishFailed,
|
2020-11-18 15:29:26 +08:00
|
|
|
name,
|
2020-06-05 18:06:52 +08:00
|
|
|
...restFormProps
|
2019-07-03 20:14:39 +08:00
|
|
|
} = props;
|
2020-06-05 18:06:52 +08:00
|
|
|
|
2023-05-12 14:53:47 +08:00
|
|
|
const mergedSize = useSize(size);
|
|
|
|
|
2023-05-09 19:09:21 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
|
|
useFormWarning(props);
|
|
|
|
}
|
|
|
|
|
2020-08-21 12:58:14 +08:00
|
|
|
const mergedRequiredMark = useMemo(() => {
|
|
|
|
if (requiredMark !== undefined) {
|
|
|
|
return requiredMark;
|
|
|
|
}
|
|
|
|
|
2020-10-24 14:27:49 +08:00
|
|
|
if (contextForm && contextForm.requiredMark !== undefined) {
|
|
|
|
return contextForm.requiredMark;
|
|
|
|
}
|
|
|
|
|
2020-08-21 12:58:14 +08:00
|
|
|
if (hideRequiredMark) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-10-24 14:27:49 +08:00
|
|
|
}, [hideRequiredMark, requiredMark, contextForm]);
|
2020-08-21 12:58:14 +08:00
|
|
|
|
2021-11-11 17:51:33 +08:00
|
|
|
const mergedColon = colon ?? contextForm?.colon;
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
const prefixCls = getPrefixCls('form', customizePrefixCls);
|
|
|
|
|
2022-03-30 14:13:36 +08:00
|
|
|
// Style
|
2022-04-06 22:14:16 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2022-03-30 14:13:36 +08:00
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
const formClassName = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${layout}`]: true,
|
2020-08-21 12:58:14 +08:00
|
|
|
[`${prefixCls}-hide-required-mark`]: mergedRequiredMark === false,
|
2020-01-02 19:10:16 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2023-05-12 14:53:47 +08:00
|
|
|
[`${prefixCls}-${mergedSize}`]: mergedSize,
|
2019-07-03 20:14:39 +08:00
|
|
|
},
|
2022-03-30 14:13:36 +08:00
|
|
|
hashId,
|
2019-07-03 20:14:39 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2019-07-03 20:14:39 +08:00
|
|
|
);
|
|
|
|
|
2019-07-04 15:00:11 +08:00
|
|
|
const [wrapForm] = useForm(form);
|
2020-06-05 18:06:52 +08:00
|
|
|
const { __INTERNAL__ } = wrapForm;
|
|
|
|
__INTERNAL__.name = name;
|
2019-07-04 15:00:11 +08:00
|
|
|
|
2020-08-21 12:58:14 +08:00
|
|
|
const formContextValue = useMemo<FormContextProps>(
|
2020-03-08 17:41:45 +08:00
|
|
|
() => ({
|
|
|
|
name,
|
|
|
|
labelAlign,
|
|
|
|
labelCol,
|
2021-11-26 18:15:59 +08:00
|
|
|
labelWrap,
|
2020-03-08 17:41:45 +08:00
|
|
|
wrapperCol,
|
|
|
|
vertical: layout === 'vertical',
|
2021-11-11 17:51:33 +08:00
|
|
|
colon: mergedColon,
|
2020-08-21 12:58:14 +08:00
|
|
|
requiredMark: mergedRequiredMark,
|
2020-06-05 18:06:52 +08:00
|
|
|
itemRef: __INTERNAL__.itemRef,
|
2022-04-15 15:51:09 +08:00
|
|
|
form: wrapForm,
|
2020-03-08 17:41:45 +08:00
|
|
|
}),
|
2022-04-15 15:51:09 +08:00
|
|
|
[name, labelAlign, labelCol, wrapperCol, layout, mergedColon, mergedRequiredMark, wrapForm],
|
2020-03-08 17:41:45 +08:00
|
|
|
);
|
|
|
|
|
2019-07-04 15:00:11 +08:00
|
|
|
React.useImperativeHandle(ref, () => wrapForm);
|
|
|
|
|
2023-01-03 09:56:47 +08:00
|
|
|
const scrollToField = (options: boolean | Options, fieldName: InternalNamePath) => {
|
2023-01-20 11:03:50 +08:00
|
|
|
if (options) {
|
2023-01-03 09:56:47 +08:00
|
|
|
let defaultScrollToFirstError: Options = { block: 'nearest' };
|
|
|
|
if (typeof options === 'object') {
|
|
|
|
defaultScrollToFirstError = options;
|
|
|
|
}
|
|
|
|
wrapForm.scrollToField(fieldName, defaultScrollToFirstError);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-19 17:38:46 +08:00
|
|
|
const onInternalFinishFailed = (errorInfo: ValidateErrorEntity) => {
|
2021-02-19 18:26:53 +08:00
|
|
|
onFinishFailed?.(errorInfo);
|
2023-01-03 09:56:47 +08:00
|
|
|
if (errorInfo.errorFields.length) {
|
|
|
|
const fieldName = errorInfo.errorFields[0].name;
|
|
|
|
if (scrollToFirstError !== undefined) {
|
2023-01-20 11:03:50 +08:00
|
|
|
scrollToField(scrollToFirstError, fieldName);
|
2023-01-03 09:56:47 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-02-19 17:38:46 +08:00
|
|
|
|
2023-01-03 09:56:47 +08:00
|
|
|
if (contextForm && contextForm.scrollToFirstError !== undefined) {
|
2023-01-20 11:03:50 +08:00
|
|
|
scrollToField(contextForm.scrollToFirstError, fieldName);
|
2020-12-11 14:35:46 +08:00
|
|
|
}
|
2020-02-19 17:38:46 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-30 14:13:36 +08:00
|
|
|
return wrapSSR(
|
2022-04-29 20:48:10 +08:00
|
|
|
<DisabledContextProvider disabled={disabled}>
|
2023-05-12 14:53:47 +08:00
|
|
|
<SizeContextProvider size={mergedSize}>
|
2022-04-29 20:48:10 +08:00
|
|
|
<FormContext.Provider value={formContextValue}>
|
|
|
|
<FieldForm
|
|
|
|
id={name}
|
|
|
|
{...restFormProps}
|
|
|
|
name={name}
|
|
|
|
onFinishFailed={onInternalFinishFailed}
|
|
|
|
form={wrapForm}
|
|
|
|
className={formClassName}
|
|
|
|
/>
|
|
|
|
</FormContext.Provider>
|
|
|
|
</SizeContextProvider>
|
2022-05-06 18:46:16 +08:00
|
|
|
</DisabledContextProvider>,
|
2019-07-03 20:14:39 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-07-31 16:53:39 +08:00
|
|
|
const Form = React.forwardRef<FormInstance, FormProps>(InternalForm) as <Values = any>(
|
|
|
|
props: React.PropsWithChildren<FormProps<Values>> & { ref?: React.Ref<FormInstance<Values>> },
|
|
|
|
) => React.ReactElement;
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
export { useForm, List, type FormInstance, useWatch };
|
2015-10-09 13:53:04 +08:00
|
|
|
|
2019-06-23 12:49:28 +08:00
|
|
|
export default Form;
|