2019-07-03 20:14:39 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import isEqual from 'lodash/isEqual';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { Field, FormInstance } from 'rc-field-form';
|
2019-12-31 20:21:30 +08:00
|
|
|
import { FieldProps } from 'rc-field-form/lib/Field';
|
2020-02-04 14:43:12 +08:00
|
|
|
import { Meta, NamePath } from 'rc-field-form/lib/interface';
|
2020-06-05 18:06:52 +08:00
|
|
|
import { supportRef } from 'rc-util/lib/ref';
|
2019-11-06 23:30:41 +08:00
|
|
|
import omit from 'omit.js';
|
2019-07-03 20:14:39 +08:00
|
|
|
import Row from '../grid/row';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import { tuple } from '../_util/type';
|
2020-05-14 15:57:04 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2019-07-03 20:14:39 +08:00
|
|
|
import FormItemLabel, { FormItemLabelProps } from './FormItemLabel';
|
|
|
|
import FormItemInput, { FormItemInputProps } from './FormItemInput';
|
|
|
|
import { FormContext, FormItemContext } from './context';
|
2020-06-05 18:06:52 +08:00
|
|
|
import { toArray, getFieldId } from './util';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { cloneElement, isValidElement } from '../_util/reactNode';
|
2020-06-05 18:06:52 +08:00
|
|
|
import useFrameState from './hooks/useFrameState';
|
|
|
|
import useItemRef from './hooks/useItemRef';
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
const ValidateStatuses = tuple('success', 'warning', 'error', 'validating', '');
|
2019-11-13 11:48:20 +08:00
|
|
|
export type ValidateStatus = typeof ValidateStatuses[number];
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2020-03-05 22:05:22 +08:00
|
|
|
type RenderChildren = (form: FormInstance) => React.ReactNode;
|
2019-12-31 20:21:30 +08:00
|
|
|
type RcFieldProps = Omit<FieldProps, 'children'>;
|
2020-03-27 15:08:55 +08:00
|
|
|
type ChildrenType = RenderChildren | React.ReactNode;
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2020-03-08 16:28:33 +08:00
|
|
|
interface MemoInputProps {
|
|
|
|
value: any;
|
|
|
|
update: number;
|
2020-04-13 21:30:55 +08:00
|
|
|
children: React.ReactNode;
|
2020-03-08 16:28:33 +08:00
|
|
|
}
|
|
|
|
|
2020-04-13 21:30:55 +08:00
|
|
|
const MemoInput = React.memo(
|
|
|
|
({ children }: MemoInputProps) => children as JSX.Element,
|
2020-03-08 16:28:33 +08:00
|
|
|
(prev, next) => {
|
|
|
|
return prev.value === next.value && prev.update === next.update;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-04-13 21:30:55 +08:00
|
|
|
export interface FormItemProps extends FormItemLabelProps, FormItemInputProps, RcFieldProps {
|
2019-07-03 20:14:39 +08:00
|
|
|
prefixCls?: string;
|
2019-07-19 14:07:39 +08:00
|
|
|
noStyle?: boolean;
|
2019-07-03 20:14:39 +08:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
className?: string;
|
2020-01-17 11:50:06 +08:00
|
|
|
children: ChildrenType;
|
2019-07-03 20:14:39 +08:00
|
|
|
id?: string;
|
|
|
|
hasFeedback?: boolean;
|
|
|
|
validateStatus?: ValidateStatus;
|
|
|
|
required?: boolean;
|
|
|
|
|
|
|
|
/** Auto passed by List render props. User should not use this. */
|
2020-05-10 10:39:30 +08:00
|
|
|
fieldKey?: React.Key | React.Key[];
|
2019-07-03 20:14:39 +08:00
|
|
|
}
|
|
|
|
|
2020-02-03 17:54:33 +08:00
|
|
|
function hasValidName(name?: NamePath): Boolean {
|
|
|
|
if (name === null) {
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(false, 'Form.Item', '`null` is passed as `name` property');
|
2020-02-03 17:54:33 +08:00
|
|
|
}
|
|
|
|
return !(name === undefined || name === null);
|
|
|
|
}
|
|
|
|
|
2020-01-22 11:33:35 +08:00
|
|
|
function FormItem(props: FormItemProps): React.ReactElement {
|
2019-07-03 20:14:39 +08:00
|
|
|
const {
|
|
|
|
name,
|
|
|
|
fieldKey,
|
2019-07-19 14:07:39 +08:00
|
|
|
noStyle,
|
2019-07-03 20:14:39 +08:00
|
|
|
dependencies,
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
style,
|
|
|
|
className,
|
|
|
|
shouldUpdate,
|
|
|
|
hasFeedback,
|
|
|
|
help,
|
|
|
|
rules,
|
|
|
|
validateStatus,
|
|
|
|
children,
|
|
|
|
required,
|
2020-03-03 21:57:06 +08:00
|
|
|
label,
|
2019-07-03 20:14:39 +08:00
|
|
|
trigger = 'onChange',
|
|
|
|
validateTrigger = 'onChange',
|
2019-11-13 11:48:20 +08:00
|
|
|
...restProps
|
2019-07-03 20:14:39 +08:00
|
|
|
} = props;
|
2020-03-05 16:22:02 +08:00
|
|
|
const destroyRef = React.useRef(false);
|
2019-07-03 20:14:39 +08:00
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
2020-06-05 18:06:52 +08:00
|
|
|
const { name: formName } = React.useContext(FormContext);
|
2019-07-03 20:14:39 +08:00
|
|
|
const { updateItemErrors } = React.useContext(FormItemContext);
|
2020-03-05 16:22:02 +08:00
|
|
|
const [domErrorVisible, innerSetDomErrorVisible] = React.useState(!!help);
|
2020-05-07 23:26:07 +08:00
|
|
|
const prevValidateStatusRef = React.useRef<ValidateStatus | undefined>(validateStatus);
|
2020-03-19 21:43:57 +08:00
|
|
|
const [inlineErrors, setInlineErrors] = useFrameState<Record<string, string[]>>({});
|
2020-03-05 16:22:02 +08:00
|
|
|
|
|
|
|
function setDomErrorVisible(visible: boolean) {
|
|
|
|
if (!destroyRef.current) {
|
|
|
|
innerSetDomErrorVisible(visible);
|
|
|
|
}
|
|
|
|
}
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2020-02-03 17:54:33 +08:00
|
|
|
const hasName = hasValidName(name);
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
// Cache Field NamePath
|
|
|
|
const nameRef = React.useRef<(string | number)[]>([]);
|
|
|
|
|
|
|
|
// Should clean up if Field removed
|
|
|
|
React.useEffect(() => {
|
|
|
|
return () => {
|
2020-03-05 16:22:02 +08:00
|
|
|
destroyRef.current = true;
|
2019-07-03 20:14:39 +08:00
|
|
|
updateItemErrors(nameRef.current.join('__SPLIT__'), []);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('form', customizePrefixCls);
|
|
|
|
|
2020-01-17 11:50:06 +08:00
|
|
|
// ======================== Errors ========================
|
|
|
|
// Collect noStyle Field error to the top FormItem
|
|
|
|
const updateChildItemErrors = noStyle
|
|
|
|
? updateItemErrors
|
|
|
|
: (subName: string, subErrors: string[]) => {
|
|
|
|
if (!isEqual(inlineErrors[subName], subErrors)) {
|
2020-03-19 21:43:57 +08:00
|
|
|
setInlineErrors(prevInlineErrors => ({
|
|
|
|
...prevInlineErrors,
|
|
|
|
[subName]: subErrors,
|
|
|
|
}));
|
2020-01-17 11:50:06 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-05 18:06:52 +08:00
|
|
|
// ===================== Children Ref =====================
|
|
|
|
const getItemRef = useItemRef();
|
|
|
|
|
2020-01-17 11:50:06 +08:00
|
|
|
function renderLayout(
|
2020-03-05 22:05:22 +08:00
|
|
|
baseChildren: React.ReactNode,
|
2020-01-17 11:50:06 +08:00
|
|
|
fieldId?: string,
|
|
|
|
meta?: Meta,
|
|
|
|
isRequired?: boolean,
|
2020-04-13 21:30:55 +08:00
|
|
|
): React.ReactNode {
|
2020-01-17 11:50:06 +08:00
|
|
|
if (noStyle) {
|
|
|
|
return baseChildren;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ======================== Errors ========================
|
|
|
|
let mergedErrors: React.ReactNode[];
|
2020-01-20 16:09:59 +08:00
|
|
|
if (help !== undefined && help !== null) {
|
2020-01-17 11:50:06 +08:00
|
|
|
mergedErrors = toArray(help);
|
|
|
|
} else {
|
|
|
|
mergedErrors = meta ? meta.errors : [];
|
|
|
|
Object.keys(inlineErrors).forEach(subName => {
|
|
|
|
const subErrors = inlineErrors[subName] || [];
|
|
|
|
if (subErrors.length) {
|
|
|
|
mergedErrors = [...mergedErrors, ...subErrors];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// ======================== Status ========================
|
|
|
|
let mergedValidateStatus: ValidateStatus = '';
|
|
|
|
if (validateStatus !== undefined) {
|
|
|
|
mergedValidateStatus = validateStatus;
|
|
|
|
} else if (meta && meta.validating) {
|
|
|
|
mergedValidateStatus = 'validating';
|
|
|
|
} else if (!help && mergedErrors.length) {
|
|
|
|
mergedValidateStatus = 'error';
|
|
|
|
} else if (meta && meta.touched) {
|
|
|
|
mergedValidateStatus = 'success';
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:26:07 +08:00
|
|
|
if (domErrorVisible && help) {
|
|
|
|
prevValidateStatusRef.current = mergedValidateStatus;
|
|
|
|
}
|
|
|
|
|
2020-01-17 11:50:06 +08:00
|
|
|
const itemClassName = {
|
|
|
|
[`${prefixCls}-item`]: true,
|
|
|
|
[`${prefixCls}-item-with-help`]: domErrorVisible || help,
|
|
|
|
[`${className}`]: !!className,
|
|
|
|
|
|
|
|
// Status
|
|
|
|
[`${prefixCls}-item-has-feedback`]: mergedValidateStatus && hasFeedback,
|
|
|
|
[`${prefixCls}-item-has-success`]: mergedValidateStatus === 'success',
|
|
|
|
[`${prefixCls}-item-has-warning`]: mergedValidateStatus === 'warning',
|
|
|
|
[`${prefixCls}-item-has-error`]: mergedValidateStatus === 'error',
|
|
|
|
[`${prefixCls}-item-has-error-leave`]:
|
2020-05-07 23:26:07 +08:00
|
|
|
!help && domErrorVisible && prevValidateStatusRef.current === 'error',
|
2020-01-17 11:50:06 +08:00
|
|
|
[`${prefixCls}-item-is-validating`]: mergedValidateStatus === 'validating',
|
|
|
|
};
|
|
|
|
|
|
|
|
// ======================= Children =======================
|
|
|
|
return (
|
|
|
|
<Row
|
|
|
|
className={classNames(itemClassName)}
|
|
|
|
style={style}
|
|
|
|
key="row"
|
|
|
|
{...omit(restProps, [
|
|
|
|
'colon',
|
|
|
|
'extra',
|
|
|
|
'getValueFromEvent',
|
2020-05-05 16:15:36 +08:00
|
|
|
'getValueProps',
|
2020-01-17 11:50:06 +08:00
|
|
|
'hasFeedback',
|
|
|
|
'help',
|
|
|
|
'htmlFor',
|
|
|
|
'id', // It is deprecated because `htmlFor` is its replacement.
|
2020-05-04 12:13:44 +08:00
|
|
|
'initialValue',
|
2020-05-10 10:39:30 +08:00
|
|
|
'isListField',
|
2020-01-17 11:50:06 +08:00
|
|
|
'label',
|
|
|
|
'labelAlign',
|
|
|
|
'labelCol',
|
|
|
|
'normalize',
|
|
|
|
'required',
|
2020-02-10 21:55:55 +08:00
|
|
|
'validateFirst',
|
2020-01-17 11:50:06 +08:00
|
|
|
'validateStatus',
|
|
|
|
'valuePropName',
|
|
|
|
'wrapperCol',
|
|
|
|
])}
|
|
|
|
>
|
|
|
|
{/* Label */}
|
2020-02-01 20:09:29 +08:00
|
|
|
<FormItemLabel htmlFor={fieldId} required={isRequired} {...props} prefixCls={prefixCls} />
|
2020-01-17 11:50:06 +08:00
|
|
|
{/* Input Group */}
|
|
|
|
<FormItemInput
|
|
|
|
{...props}
|
|
|
|
{...meta}
|
|
|
|
errors={mergedErrors}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
onDomErrorVisibleChange={setDomErrorVisible}
|
|
|
|
validateStatus={mergedValidateStatus}
|
|
|
|
>
|
|
|
|
<FormItemContext.Provider value={{ updateItemErrors: updateChildItemErrors }}>
|
|
|
|
{baseChildren}
|
|
|
|
</FormItemContext.Provider>
|
|
|
|
</FormItemInput>
|
|
|
|
</Row>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const isRenderProps = typeof children === 'function';
|
|
|
|
|
2020-03-08 16:28:33 +08:00
|
|
|
// Record for real component render
|
|
|
|
const updateRef = React.useRef(0);
|
|
|
|
updateRef.current += 1;
|
|
|
|
|
2020-03-10 13:19:35 +08:00
|
|
|
if (!hasName && !isRenderProps && !dependencies) {
|
2020-04-13 21:30:55 +08:00
|
|
|
return renderLayout(children) as JSX.Element;
|
2020-03-10 13:19:35 +08:00
|
|
|
}
|
|
|
|
|
2020-03-03 21:57:06 +08:00
|
|
|
const variables: Record<string, string> = {};
|
|
|
|
if (typeof label === 'string') {
|
|
|
|
variables.label = label;
|
|
|
|
}
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
return (
|
|
|
|
<Field
|
|
|
|
{...props}
|
2020-03-03 21:57:06 +08:00
|
|
|
messageVariables={variables}
|
2019-07-03 20:14:39 +08:00
|
|
|
trigger={trigger}
|
|
|
|
validateTrigger={validateTrigger}
|
|
|
|
onReset={() => {
|
|
|
|
setDomErrorVisible(false);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{(control, meta, context) => {
|
2020-01-17 11:50:06 +08:00
|
|
|
const { errors } = meta;
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2020-01-17 11:50:06 +08:00
|
|
|
const mergedName = toArray(name).length && meta ? meta.name : [];
|
|
|
|
const fieldId = getFieldId(mergedName, formName);
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2019-07-19 14:07:39 +08:00
|
|
|
if (noStyle) {
|
2019-07-03 20:14:39 +08:00
|
|
|
nameRef.current = [...mergedName];
|
|
|
|
if (fieldKey) {
|
2020-05-10 10:39:30 +08:00
|
|
|
const fieldKeys = Array.isArray(fieldKey) ? fieldKey : [fieldKey];
|
2020-06-09 18:10:43 +08:00
|
|
|
nameRef.current = [...mergedName.slice(0, -1), ...fieldKeys];
|
2019-07-03 20:14:39 +08:00
|
|
|
}
|
|
|
|
updateItemErrors(nameRef.current.join('__SPLIT__'), errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
const isRequired =
|
|
|
|
required !== undefined
|
|
|
|
? required
|
2019-12-12 12:05:59 +08:00
|
|
|
: !!(
|
|
|
|
rules &&
|
|
|
|
rules.some(rule => {
|
|
|
|
if (rule && typeof rule === 'object' && rule.required) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (typeof rule === 'function') {
|
|
|
|
const ruleEntity = rule(context);
|
|
|
|
return ruleEntity && ruleEntity.required;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
);
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
// ======================= Children =======================
|
|
|
|
const mergedControl: typeof control = {
|
|
|
|
...control,
|
2019-07-04 15:00:11 +08:00
|
|
|
id: fieldId,
|
2019-07-03 20:14:39 +08:00
|
|
|
};
|
|
|
|
|
2020-03-05 22:05:22 +08:00
|
|
|
let childNode: React.ReactNode = null;
|
2020-02-03 17:54:33 +08:00
|
|
|
if (Array.isArray(children) && hasName) {
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(false, 'Form.Item', '`children` is array of render props cannot have `name`.');
|
2020-01-01 20:07:10 +08:00
|
|
|
childNode = children;
|
2020-02-03 17:54:33 +08:00
|
|
|
} else if (isRenderProps && (!shouldUpdate || hasName)) {
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(
|
2020-01-07 19:17:51 +08:00
|
|
|
!!shouldUpdate,
|
|
|
|
'Form.Item',
|
|
|
|
'`children` of render props only work with `shouldUpdate`.',
|
|
|
|
);
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(
|
2020-02-03 17:54:33 +08:00
|
|
|
!hasName,
|
2020-01-07 19:17:51 +08:00
|
|
|
'Form.Item',
|
|
|
|
"Do not use `name` with `children` of render props since it's not a field.",
|
|
|
|
);
|
2020-02-03 17:54:33 +08:00
|
|
|
} else if (dependencies && !isRenderProps && !hasName) {
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(
|
2020-01-17 11:50:06 +08:00
|
|
|
false,
|
|
|
|
'Form.Item',
|
|
|
|
'Must set `name` or use render props when `dependencies` is set.',
|
|
|
|
);
|
2020-05-14 20:54:49 +08:00
|
|
|
} else if (isValidElement(children)) {
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(
|
2020-04-13 21:30:55 +08:00
|
|
|
children.props.defaultValue === undefined,
|
2020-03-24 22:23:40 +08:00
|
|
|
'Form.Item',
|
|
|
|
'`defaultValue` will not work on controlled Field. You should use `initialValues` of Form instead.',
|
|
|
|
);
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
const childProps = { ...children.props, ...mergedControl };
|
|
|
|
|
2020-06-05 18:06:52 +08:00
|
|
|
if (supportRef(children)) {
|
|
|
|
childProps.ref = getItemRef(mergedName, children);
|
|
|
|
}
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
// We should keep user origin event handler
|
2020-03-08 00:54:45 +08:00
|
|
|
const triggers = new Set<string>([...toArray(trigger), ...toArray(validateTrigger)]);
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
triggers.forEach(eventName => {
|
2020-02-18 15:38:00 +08:00
|
|
|
childProps[eventName] = (...args: any[]) => {
|
|
|
|
mergedControl[eventName]?.(...args);
|
|
|
|
children.props[eventName]?.(...args);
|
|
|
|
};
|
2019-07-03 20:14:39 +08:00
|
|
|
});
|
|
|
|
|
2020-03-08 16:28:33 +08:00
|
|
|
childNode = (
|
|
|
|
<MemoInput
|
|
|
|
value={mergedControl[props.valuePropName || 'value']}
|
|
|
|
update={updateRef.current}
|
|
|
|
>
|
2020-05-14 20:54:49 +08:00
|
|
|
{cloneElement(children, childProps)}
|
2020-03-08 16:28:33 +08:00
|
|
|
</MemoInput>
|
|
|
|
);
|
2020-02-03 17:54:33 +08:00
|
|
|
} else if (isRenderProps && shouldUpdate && !hasName) {
|
2020-01-17 11:50:06 +08:00
|
|
|
childNode = (children as RenderChildren)(context);
|
2019-12-11 16:08:59 +08:00
|
|
|
} else {
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(
|
2019-12-11 16:08:59 +08:00
|
|
|
!mergedName.length,
|
|
|
|
'Form.Item',
|
|
|
|
'`name` is only used for validate React element. If you are using Form.Item as layout display, please remove `name` instead.',
|
|
|
|
);
|
2020-04-13 21:30:55 +08:00
|
|
|
childNode = children;
|
2019-07-03 20:14:39 +08:00
|
|
|
}
|
|
|
|
|
2020-01-17 11:50:06 +08:00
|
|
|
return renderLayout(childNode, fieldId, meta, isRequired);
|
2019-07-03 20:14:39 +08:00
|
|
|
}}
|
|
|
|
</Field>
|
|
|
|
);
|
2020-01-17 11:50:06 +08:00
|
|
|
}
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
export default FormItem;
|