2019-07-03 20:14:39 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import omit from 'omit.js';
|
|
|
|
import { FormProvider as RcFormProvider } from 'rc-field-form';
|
|
|
|
import { FormProviderProps as RcFormProviderProps } from 'rc-field-form/lib/FormContext';
|
|
|
|
import { ColProps } from '../grid/col';
|
|
|
|
import { FormLabelAlign } from './interface';
|
2020-08-21 12:58:14 +08:00
|
|
|
import { RequiredMark } from './Form';
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Form Context
|
|
|
|
* Set top form style and pass to Form Item usage.
|
|
|
|
*/
|
|
|
|
export interface FormContextProps {
|
|
|
|
vertical: boolean;
|
|
|
|
name?: string;
|
|
|
|
colon?: boolean;
|
|
|
|
labelAlign?: FormLabelAlign;
|
|
|
|
labelCol?: ColProps;
|
|
|
|
wrapperCol?: ColProps;
|
2020-08-21 12:58:14 +08:00
|
|
|
requiredMark?: RequiredMark;
|
2020-06-05 18:06:52 +08:00
|
|
|
itemRef: (name: (string | number)[]) => (node: React.ReactElement) => void;
|
2019-07-03 20:14:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const FormContext = React.createContext<FormContextProps>({
|
|
|
|
labelAlign: 'right',
|
|
|
|
vertical: false,
|
2020-06-05 18:06:52 +08:00
|
|
|
itemRef: (() => {}) as any,
|
2019-07-03 20:14:39 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Form Item Context
|
2019-07-19 14:07:39 +08:00
|
|
|
* Used for Form noStyle Item error collection
|
2019-07-03 20:14:39 +08:00
|
|
|
*/
|
|
|
|
export interface FormItemContextProps {
|
|
|
|
updateItemErrors: (name: string, errors: string[]) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const FormItemContext = React.createContext<FormItemContextProps>({
|
|
|
|
updateItemErrors: () => {},
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Form Provider
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export interface FormProviderProps extends Omit<RcFormProviderProps, 'validateMessages'> {}
|
|
|
|
|
|
|
|
export const FormProvider: React.FC<FormProviderProps> = props => {
|
|
|
|
const providerProps = omit(props, ['prefixCls']);
|
|
|
|
return <RcFormProvider {...providerProps} />;
|
|
|
|
};
|