2019-07-03 20:14:39 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import omit from 'omit.js';
|
|
|
|
import classNames from 'classnames';
|
2019-07-04 15:00:11 +08:00
|
|
|
import FieldForm, { List } from 'rc-field-form';
|
2019-07-03 20:14:39 +08:00
|
|
|
import { FormProps as RcFormProps } from 'rc-field-form/lib/Form';
|
2020-02-19 17:38:46 +08:00
|
|
|
import { ValidateErrorEntity } from 'rc-field-form/lib/interface';
|
2019-07-03 20:14:39 +08:00
|
|
|
import { ColProps } from '../grid/col';
|
|
|
|
import { ConfigContext, ConfigConsumerProps } from '../config-provider';
|
|
|
|
import { FormContext } from './context';
|
|
|
|
import { FormLabelAlign } from './interface';
|
2019-07-04 15:00:11 +08:00
|
|
|
import { useForm, FormInstance } from './util';
|
2020-02-19 22:04:25 +08:00
|
|
|
import SizeContext, { SizeType, SizeContextProvider } from '../config-provider/SizeContext';
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
export type FormLayout = 'horizontal' | 'inline' | 'vertical';
|
|
|
|
|
2019-12-06 14:59:16 +08:00
|
|
|
export interface FormProps extends Omit<RcFormProps, 'form'> {
|
2019-07-03 20:14:39 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
hideRequiredMark?: boolean;
|
|
|
|
colon?: boolean;
|
|
|
|
name?: string;
|
|
|
|
layout?: FormLayout;
|
|
|
|
labelAlign?: FormLabelAlign;
|
|
|
|
labelCol?: ColProps;
|
|
|
|
wrapperCol?: ColProps;
|
2019-07-04 15:00:11 +08:00
|
|
|
form?: FormInstance;
|
2020-01-03 13:38:16 +08:00
|
|
|
size?: SizeType;
|
2020-02-19 17:38:46 +08:00
|
|
|
scrollToFirstError?: boolean;
|
2019-07-03 20:14:39 +08:00
|
|
|
}
|
|
|
|
|
2020-02-20 16:36:47 +08:00
|
|
|
const InternalForm: React.ForwardRefRenderFunction<unknown, FormProps> = (props, ref) => {
|
2020-02-19 22:04:25 +08:00
|
|
|
const contextSize = React.useContext(SizeContext);
|
2020-01-02 19:10:16 +08:00
|
|
|
const { getPrefixCls, direction }: ConfigConsumerProps = React.useContext(ConfigContext);
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
const {
|
2019-07-04 15:00:11 +08:00
|
|
|
form,
|
2019-07-03 20:14:39 +08:00
|
|
|
colon,
|
|
|
|
name,
|
|
|
|
labelAlign,
|
|
|
|
labelCol,
|
|
|
|
wrapperCol,
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
hideRequiredMark,
|
|
|
|
className = '',
|
|
|
|
layout = 'horizontal',
|
2020-02-19 22:04:25 +08:00
|
|
|
size = contextSize,
|
2020-02-19 17:38:46 +08:00
|
|
|
scrollToFirstError,
|
|
|
|
onFinishFailed,
|
2019-07-03 20:14:39 +08:00
|
|
|
} = props;
|
|
|
|
const prefixCls = getPrefixCls('form', customizePrefixCls);
|
|
|
|
|
|
|
|
const formClassName = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${layout}`]: true,
|
|
|
|
[`${prefixCls}-hide-required-mark`]: hideRequiredMark,
|
2020-01-02 19:10:16 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2020-02-19 22:04:25 +08:00
|
|
|
[`${prefixCls}-${size}`]: size,
|
2019-07-03 20:14:39 +08:00
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
|
|
|
|
|
|
|
const formProps = omit(props, [
|
|
|
|
'prefixCls',
|
|
|
|
'className',
|
|
|
|
'layout',
|
|
|
|
'hideRequiredMark',
|
|
|
|
'wrapperCol',
|
|
|
|
'labelAlign',
|
|
|
|
'labelCol',
|
|
|
|
'colon',
|
2020-02-19 17:38:46 +08:00
|
|
|
'scrollToFirstError',
|
2019-07-03 20:14:39 +08:00
|
|
|
]);
|
|
|
|
|
2019-07-04 15:00:11 +08:00
|
|
|
const [wrapForm] = useForm(form);
|
|
|
|
wrapForm.__INTERNAL__.name = name;
|
|
|
|
|
2020-03-08 17:41:45 +08:00
|
|
|
const formContextValue = React.useMemo(
|
|
|
|
() => ({
|
|
|
|
name,
|
|
|
|
labelAlign,
|
|
|
|
labelCol,
|
|
|
|
wrapperCol,
|
|
|
|
vertical: layout === 'vertical',
|
|
|
|
colon,
|
|
|
|
}),
|
|
|
|
[name, labelAlign, labelCol, wrapperCol, layout, colon],
|
|
|
|
);
|
|
|
|
|
2019-07-04 15:00:11 +08:00
|
|
|
React.useImperativeHandle(ref, () => wrapForm);
|
|
|
|
|
2020-02-19 17:38:46 +08:00
|
|
|
const onInternalFinishFailed = (errorInfo: ValidateErrorEntity) => {
|
|
|
|
if (onFinishFailed) {
|
|
|
|
onFinishFailed(errorInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scrollToFirstError && errorInfo.errorFields.length) {
|
|
|
|
wrapForm.scrollToField(errorInfo.errorFields[0].name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
return (
|
2020-01-03 13:38:16 +08:00
|
|
|
<SizeContextProvider size={size}>
|
|
|
|
<FormContext.Provider
|
2020-03-08 17:41:45 +08:00
|
|
|
value={formContextValue}
|
2020-01-03 13:38:16 +08:00
|
|
|
>
|
2020-02-19 17:38:46 +08:00
|
|
|
<FieldForm
|
|
|
|
id={name}
|
|
|
|
{...formProps}
|
|
|
|
onFinishFailed={onInternalFinishFailed}
|
|
|
|
form={wrapForm}
|
|
|
|
className={formClassName}
|
|
|
|
/>
|
2020-01-03 13:38:16 +08:00
|
|
|
</FormContext.Provider>
|
|
|
|
</SizeContextProvider>
|
2019-07-03 20:14:39 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-07-17 16:53:45 +08:00
|
|
|
const Form = React.forwardRef<FormInstance, FormProps>(InternalForm);
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
export { useForm, List, FormInstance };
|
2015-10-09 13:53:04 +08:00
|
|
|
|
2019-06-23 12:49:28 +08:00
|
|
|
export default Form;
|