2019-07-03 20:14:39 +08:00
|
|
|
import * as React from 'react';
|
2022-03-25 17:48:12 +08:00
|
|
|
import { ReactNode, useContext, useMemo } from 'react';
|
2019-07-03 20:14:39 +08:00
|
|
|
import classNames from 'classnames';
|
2021-10-29 18:24:50 +08:00
|
|
|
import { Field, FormInstance, FieldContext, ListContext } 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-28 22:41:59 +08:00
|
|
|
import { supportRef } from 'rc-util/lib/ref';
|
2022-03-10 15:21:10 +08:00
|
|
|
import useState from 'rc-util/lib/hooks/useState';
|
2021-01-13 21:00:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2022-03-25 17:48:12 +08:00
|
|
|
import CheckCircleFilled from '@ant-design/icons/CheckCircleFilled';
|
|
|
|
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
|
|
|
|
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
|
|
|
|
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
|
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';
|
2020-09-17 17:11:45 +08:00
|
|
|
import FormItemLabel, { FormItemLabelProps, LabelTooltipType } from './FormItemLabel';
|
2019-07-03 20:14:39 +08:00
|
|
|
import FormItemInput, { FormItemInputProps } from './FormItemInput';
|
2022-02-14 17:09:35 +08:00
|
|
|
import {
|
|
|
|
FormContext,
|
2022-03-24 21:54:20 +08:00
|
|
|
FormItemInputContext,
|
2022-02-14 17:09:35 +08:00
|
|
|
NoStyleItemContext,
|
|
|
|
FormItemStatusContextProps,
|
|
|
|
} from './context';
|
2020-06-28 22:41:59 +08:00
|
|
|
import { toArray, getFieldId } from './util';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { cloneElement, isValidElement } from '../_util/reactNode';
|
2020-06-28 22:41:59 +08:00
|
|
|
import useFrameState from './hooks/useFrameState';
|
2021-06-04 14:44:41 +08:00
|
|
|
import useDebounce from './hooks/useDebounce';
|
2020-06-28 22:41:59 +08:00
|
|
|
import useItemRef from './hooks/useItemRef';
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2021-01-19 11:53:44 +08:00
|
|
|
const NAME_SPLIT = '__SPLIT__';
|
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
interface FieldError {
|
|
|
|
errors: string[];
|
|
|
|
warnings: string[];
|
|
|
|
}
|
|
|
|
|
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-09-17 16:51:19 +08:00
|
|
|
type RenderChildren<Values = any> = (form: FormInstance<Values>) => React.ReactNode;
|
2021-02-27 23:51:35 +08:00
|
|
|
type RcFieldProps<Values = any> = Omit<FieldProps<Values>, 'children'>;
|
2020-09-17 16:51:19 +08:00
|
|
|
type ChildrenType<Values = any> = RenderChildren<Values> | React.ReactNode;
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2020-03-08 16:28:33 +08:00
|
|
|
interface MemoInputProps {
|
|
|
|
value: any;
|
2021-06-04 14:44:41 +08:00
|
|
|
update: any;
|
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-12-09 17:12:32 +08:00
|
|
|
(prev, next) => prev.value === next.value && prev.update === next.update,
|
2020-03-08 16:28:33 +08:00
|
|
|
);
|
|
|
|
|
2020-09-17 16:51:19 +08:00
|
|
|
export interface FormItemProps<Values = any>
|
|
|
|
extends FormItemLabelProps,
|
|
|
|
FormItemInputProps,
|
2021-02-27 23:51:35 +08:00
|
|
|
RcFieldProps<Values> {
|
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-09-17 16:51:19 +08:00
|
|
|
children?: ChildrenType<Values>;
|
2019-07-03 20:14:39 +08:00
|
|
|
id?: string;
|
|
|
|
hasFeedback?: boolean;
|
|
|
|
validateStatus?: ValidateStatus;
|
|
|
|
required?: boolean;
|
2020-06-28 22:41:59 +08:00
|
|
|
hidden?: boolean;
|
2020-08-20 14:42:09 +08:00
|
|
|
initialValue?: any;
|
2020-09-10 21:00:21 +08:00
|
|
|
messageVariables?: Record<string, string>;
|
2020-09-17 17:11:45 +08:00
|
|
|
tooltip?: LabelTooltipType;
|
2021-10-29 18:24:50 +08:00
|
|
|
/** @deprecated No need anymore */
|
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);
|
|
|
|
}
|
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
function genEmptyMeta(): Meta {
|
|
|
|
return {
|
|
|
|
errors: [],
|
|
|
|
warnings: [],
|
|
|
|
touched: false,
|
|
|
|
validating: false,
|
|
|
|
name: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-25 17:48:12 +08:00
|
|
|
const iconMap = {
|
|
|
|
success: CheckCircleFilled,
|
|
|
|
warning: ExclamationCircleFilled,
|
|
|
|
error: CloseCircleFilled,
|
|
|
|
validating: LoadingOutlined,
|
|
|
|
};
|
|
|
|
|
2020-09-17 16:51:19 +08:00
|
|
|
function FormItem<Values = any>(props: FormItemProps<Values>): React.ReactElement {
|
2019-07-03 20:14:39 +08:00
|
|
|
const {
|
|
|
|
name,
|
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,
|
2020-09-10 21:00:21 +08:00
|
|
|
messageVariables,
|
2019-07-03 20:14:39 +08:00
|
|
|
trigger = 'onChange',
|
2020-06-14 14:15:17 +08:00
|
|
|
validateTrigger,
|
2020-06-28 22:41:59 +08:00
|
|
|
hidden,
|
2019-11-13 11:48:20 +08:00
|
|
|
...restProps
|
2019-07-03 20:14:39 +08:00
|
|
|
} = props;
|
2020-09-11 21:27:51 +08:00
|
|
|
const { getPrefixCls } = useContext(ConfigContext);
|
|
|
|
const { name: formName, requiredMark } = useContext(FormContext);
|
2021-06-04 14:44:41 +08:00
|
|
|
const isRenderProps = typeof children === 'function';
|
|
|
|
const notifyParentMetaChange = useContext(NoStyleItemContext);
|
2020-03-05 16:22:02 +08:00
|
|
|
|
2020-09-11 21:27:51 +08:00
|
|
|
const { validateTrigger: contextValidateTrigger } = useContext(FieldContext);
|
2020-06-14 14:15:17 +08:00
|
|
|
const mergedValidateTrigger =
|
|
|
|
validateTrigger !== undefined ? validateTrigger : contextValidateTrigger;
|
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
const hasName = hasValidName(name);
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('form', customizePrefixCls);
|
|
|
|
|
2021-10-29 18:24:50 +08:00
|
|
|
// ========================= MISC =========================
|
|
|
|
// Get `noStyle` required info
|
|
|
|
const listContext = React.useContext(ListContext);
|
|
|
|
const fieldKeyPathRef = React.useRef<React.Key[]>();
|
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
// ======================== Errors ========================
|
|
|
|
// >>>>> Collect sub field errors
|
|
|
|
const [subFieldErrors, setSubFieldErrors] = useFrameState<Record<string, FieldError>>({});
|
|
|
|
|
|
|
|
// >>>>> Current field errors
|
2022-03-10 15:21:10 +08:00
|
|
|
const [meta, setMeta] = useState<Meta>(() => genEmptyMeta());
|
2021-06-04 14:44:41 +08:00
|
|
|
|
|
|
|
const onMetaChange = (nextMeta: Meta & { destroy?: boolean }) => {
|
2021-10-29 18:24:50 +08:00
|
|
|
// This keyInfo is not correct when field is removed
|
|
|
|
// Since origin keyManager no longer keep the origin key anymore
|
|
|
|
// Which means we need cache origin one and reuse when removed
|
|
|
|
const keyInfo = listContext?.getKey(nextMeta.name);
|
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
// Destroy will reset all the meta
|
2022-03-10 15:21:10 +08:00
|
|
|
setMeta(nextMeta.destroy ? genEmptyMeta() : nextMeta, true);
|
2021-06-04 14:44:41 +08:00
|
|
|
|
|
|
|
// Bump to parent since noStyle
|
|
|
|
if (noStyle && notifyParentMetaChange) {
|
|
|
|
let namePath = nextMeta.name;
|
2021-10-29 18:24:50 +08:00
|
|
|
|
|
|
|
if (!nextMeta.destroy) {
|
|
|
|
if (keyInfo !== undefined) {
|
|
|
|
const [fieldKey, restPath] = keyInfo;
|
|
|
|
namePath = [fieldKey, ...restPath];
|
|
|
|
fieldKeyPathRef.current = namePath;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Use origin cache data
|
|
|
|
namePath = fieldKeyPathRef.current || namePath;
|
2021-06-04 14:44:41 +08:00
|
|
|
}
|
|
|
|
notifyParentMetaChange(nextMeta, namePath);
|
2020-03-05 16:22:02 +08:00
|
|
|
}
|
2021-06-04 14:44:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// >>>>> Collect noStyle Field error to the top FormItem
|
|
|
|
const onSubItemMetaChange = (subMeta: Meta & { destroy: boolean }, uniqueKeys: React.Key[]) => {
|
|
|
|
// Only `noStyle` sub item will trigger
|
|
|
|
setSubFieldErrors(prevSubFieldErrors => {
|
|
|
|
const clone = {
|
|
|
|
...prevSubFieldErrors,
|
|
|
|
};
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
// name: ['user', 1] + key: [4] = ['user', 4]
|
|
|
|
const mergedNamePath = [...subMeta.name.slice(0, -1), ...uniqueKeys];
|
|
|
|
const mergedNameKey = mergedNamePath.join(NAME_SPLIT);
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
if (subMeta.destroy) {
|
|
|
|
// Remove
|
|
|
|
delete clone[mergedNameKey];
|
|
|
|
} else {
|
|
|
|
// Update
|
|
|
|
clone[mergedNameKey] = subMeta;
|
|
|
|
}
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
return clone;
|
|
|
|
});
|
|
|
|
};
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
// >>>>> Get merged errors
|
|
|
|
const [mergedErrors, mergedWarnings] = React.useMemo(() => {
|
|
|
|
const errorList: string[] = [...meta.errors];
|
|
|
|
const warningList: string[] = [...meta.warnings];
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
Object.values(subFieldErrors).forEach(subFieldError => {
|
|
|
|
errorList.push(...(subFieldError.errors || []));
|
|
|
|
warningList.push(...(subFieldError.warnings || []));
|
|
|
|
});
|
2021-01-19 11:53:44 +08:00
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
return [errorList, warningList];
|
|
|
|
}, [subFieldErrors, meta.errors, meta.warnings]);
|
|
|
|
|
|
|
|
const debounceErrors = useDebounce(mergedErrors);
|
|
|
|
const debounceWarnings = useDebounce(mergedWarnings);
|
2020-01-17 11:50:06 +08:00
|
|
|
|
2020-06-28 22:41:59 +08:00
|
|
|
// ===================== Children Ref =====================
|
|
|
|
const getItemRef = useItemRef();
|
|
|
|
|
2022-02-14 17:09:35 +08:00
|
|
|
// ======================== Status ========================
|
|
|
|
let mergedValidateStatus: ValidateStatus = '';
|
|
|
|
if (validateStatus !== undefined) {
|
|
|
|
mergedValidateStatus = validateStatus;
|
|
|
|
} else if (meta?.validating) {
|
|
|
|
mergedValidateStatus = 'validating';
|
|
|
|
} else if (debounceErrors.length) {
|
|
|
|
mergedValidateStatus = 'error';
|
|
|
|
} else if (debounceWarnings.length) {
|
|
|
|
mergedValidateStatus = 'warning';
|
|
|
|
} else if (meta?.touched) {
|
|
|
|
mergedValidateStatus = 'success';
|
|
|
|
}
|
|
|
|
|
2022-03-25 17:48:12 +08:00
|
|
|
const formItemStatusContext = useMemo<FormItemStatusContextProps>(() => {
|
|
|
|
let feedbackIcon: ReactNode;
|
|
|
|
if (hasFeedback) {
|
|
|
|
const IconNode = mergedValidateStatus && iconMap[mergedValidateStatus];
|
|
|
|
feedbackIcon = IconNode ? (
|
|
|
|
<span
|
|
|
|
className={classNames(
|
|
|
|
`${prefixCls}-item-feedback-icon`,
|
|
|
|
`${prefixCls}-item-feedback-icon-${mergedValidateStatus}`,
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<IconNode />
|
|
|
|
</span>
|
|
|
|
) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2022-02-14 17:09:35 +08:00
|
|
|
status: mergedValidateStatus,
|
|
|
|
hasFeedback,
|
2022-03-25 17:48:12 +08:00
|
|
|
feedbackIcon,
|
2022-03-24 21:54:20 +08:00
|
|
|
isFormItemInput: true,
|
2022-03-25 17:48:12 +08:00
|
|
|
};
|
|
|
|
}, [mergedValidateStatus, hasFeedback]);
|
2022-02-14 17:09:35 +08:00
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
// ======================== Render ========================
|
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,
|
|
|
|
isRequired?: boolean,
|
2020-04-13 21:30:55 +08:00
|
|
|
): React.ReactNode {
|
2020-08-05 10:08:57 +08:00
|
|
|
if (noStyle && !hidden) {
|
2020-01-17 11:50:06 +08:00
|
|
|
return baseChildren;
|
|
|
|
}
|
|
|
|
|
|
|
|
const itemClassName = {
|
|
|
|
[`${prefixCls}-item`]: true,
|
2022-01-10 15:58:53 +08:00
|
|
|
[`${prefixCls}-item-with-help`]:
|
|
|
|
(help !== undefined && help !== null) || debounceErrors.length || debounceWarnings.length,
|
2020-01-17 11:50:06 +08:00
|
|
|
[`${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-is-validating`]: mergedValidateStatus === 'validating',
|
2020-07-01 14:12:35 +08:00
|
|
|
[`${prefixCls}-item-hidden`]: hidden,
|
2020-01-17 11:50:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// ======================= Children =======================
|
|
|
|
return (
|
|
|
|
<Row
|
|
|
|
className={classNames(itemClassName)}
|
|
|
|
style={style}
|
|
|
|
key="row"
|
|
|
|
{...omit(restProps, [
|
|
|
|
'colon',
|
|
|
|
'extra',
|
2021-11-26 15:49:09 +08:00
|
|
|
'fieldKey',
|
2022-03-07 12:22:33 +08:00
|
|
|
'requiredMark',
|
2020-01-17 11:50:06 +08:00
|
|
|
'getValueFromEvent',
|
2020-05-05 16:15:36 +08:00
|
|
|
'getValueProps',
|
2020-01-17 11:50:06 +08:00
|
|
|
'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
|
|
|
'labelAlign',
|
2021-11-26 18:15:59 +08:00
|
|
|
'labelWrap',
|
2020-01-17 11:50:06 +08:00
|
|
|
'labelCol',
|
|
|
|
'normalize',
|
2020-07-08 23:43:26 +08:00
|
|
|
'preserve',
|
2020-09-17 17:11:45 +08:00
|
|
|
'tooltip',
|
2020-02-10 21:55:55 +08:00
|
|
|
'validateFirst',
|
2020-01-17 11:50:06 +08:00
|
|
|
'valuePropName',
|
|
|
|
'wrapperCol',
|
2021-01-13 21:00:30 +08:00
|
|
|
'_internalItemRender' as any,
|
2020-01-17 11:50:06 +08:00
|
|
|
])}
|
|
|
|
>
|
|
|
|
{/* Label */}
|
2020-08-21 12:58:14 +08:00
|
|
|
<FormItemLabel
|
|
|
|
htmlFor={fieldId}
|
|
|
|
required={isRequired}
|
|
|
|
requiredMark={requiredMark}
|
|
|
|
{...props}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
/>
|
2020-01-17 11:50:06 +08:00
|
|
|
{/* Input Group */}
|
|
|
|
<FormItemInput
|
|
|
|
{...props}
|
|
|
|
{...meta}
|
2021-06-04 14:44:41 +08:00
|
|
|
errors={debounceErrors}
|
|
|
|
warnings={debounceWarnings}
|
2020-01-17 11:50:06 +08:00
|
|
|
prefixCls={prefixCls}
|
2020-09-11 21:27:51 +08:00
|
|
|
status={mergedValidateStatus}
|
2021-06-04 14:44:41 +08:00
|
|
|
help={help}
|
2020-01-17 11:50:06 +08:00
|
|
|
>
|
2021-06-04 14:44:41 +08:00
|
|
|
<NoStyleItemContext.Provider value={onSubItemMetaChange}>
|
2022-03-24 21:54:20 +08:00
|
|
|
<FormItemInputContext.Provider value={formItemStatusContext}>
|
2022-02-14 17:09:35 +08:00
|
|
|
{baseChildren}
|
2022-03-24 21:54:20 +08:00
|
|
|
</FormItemInputContext.Provider>
|
2021-06-04 14:44:41 +08:00
|
|
|
</NoStyleItemContext.Provider>
|
2020-01-17 11:50:06 +08:00
|
|
|
</FormItemInput>
|
|
|
|
</Row>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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-09-10 21:00:21 +08:00
|
|
|
let variables: Record<string, string> = {};
|
2020-03-03 21:57:06 +08:00
|
|
|
if (typeof label === 'string') {
|
|
|
|
variables.label = label;
|
2021-06-30 11:32:34 +08:00
|
|
|
} else if (name) {
|
|
|
|
variables.label = String(name);
|
2020-03-03 21:57:06 +08:00
|
|
|
}
|
2020-09-10 21:00:21 +08:00
|
|
|
if (messageVariables) {
|
|
|
|
variables = { ...variables, ...messageVariables };
|
|
|
|
}
|
2020-03-03 21:57:06 +08:00
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
// >>>>> With Field
|
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}
|
2020-06-14 14:15:17 +08:00
|
|
|
validateTrigger={mergedValidateTrigger}
|
2021-06-04 14:44:41 +08:00
|
|
|
onMetaChange={onMetaChange}
|
2019-07-03 20:14:39 +08:00
|
|
|
>
|
2021-06-04 14:44:41 +08:00
|
|
|
{(control, renderMeta, context) => {
|
|
|
|
const mergedName = toArray(name).length && renderMeta ? renderMeta.name : [];
|
2020-01-17 11:50:06 +08:00
|
|
|
const fieldId = getFieldId(mergedName, formName);
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
const isRequired =
|
|
|
|
required !== undefined
|
|
|
|
? required
|
2019-12-12 12:05:59 +08:00
|
|
|
: !!(
|
|
|
|
rules &&
|
|
|
|
rules.some(rule => {
|
2021-06-04 14:44:41 +08:00
|
|
|
if (rule && typeof rule === 'object' && rule.required && !rule.warningOnly) {
|
2019-12-12 12:05:59 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (typeof rule === 'function') {
|
|
|
|
const ruleEntity = rule(context);
|
2021-06-04 14:44:41 +08:00
|
|
|
return ruleEntity && ruleEntity.required && !ruleEntity.warningOnly;
|
2019-12-12 12:05:59 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
);
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
// ======================= Children =======================
|
|
|
|
const mergedControl: typeof control = {
|
|
|
|
...control,
|
|
|
|
};
|
|
|
|
|
2020-03-05 22:05:22 +08:00
|
|
|
let childNode: React.ReactNode = null;
|
2020-07-03 16:25:29 +08:00
|
|
|
|
|
|
|
devWarning(
|
|
|
|
!(shouldUpdate && dependencies),
|
|
|
|
'Form.Item',
|
|
|
|
"`shouldUpdate` and `dependencies` shouldn't be used together. See https://ant.design/components/form/#dependencies.",
|
|
|
|
);
|
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-07-03 16:25:29 +08:00
|
|
|
} else if (isRenderProps && (!(shouldUpdate || dependencies) || hasName)) {
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(
|
2020-07-03 16:25:29 +08:00
|
|
|
!!(shouldUpdate || dependencies),
|
2020-01-07 19:17:51 +08:00
|
|
|
'Form.Item',
|
2020-07-03 16:25:29 +08:00
|
|
|
'`children` of render props only work with `shouldUpdate` or `dependencies`.',
|
2020-01-07 19:17:51 +08:00
|
|
|
);
|
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-11 22:25:58 +08:00
|
|
|
if (!childProps.id) {
|
|
|
|
childProps.id = fieldId;
|
|
|
|
}
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2020-06-28 22:41:59 +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-06-14 14:15:17 +08:00
|
|
|
const triggers = new Set<string>([
|
|
|
|
...toArray(trigger),
|
|
|
|
...toArray(mergedValidateTrigger),
|
|
|
|
]);
|
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 = (
|
2021-06-04 14:44:41 +08:00
|
|
|
<MemoInput value={mergedControl[props.valuePropName || 'value']} update={children}>
|
2020-05-14 20:54:49 +08:00
|
|
|
{cloneElement(children, childProps)}
|
2020-03-08 16:28:33 +08:00
|
|
|
</MemoInput>
|
|
|
|
);
|
2020-07-03 16:25:29 +08:00
|
|
|
} else if (isRenderProps && (shouldUpdate || dependencies) && !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.',
|
|
|
|
);
|
2022-04-08 22:55:42 +08:00
|
|
|
childNode = children as React.ReactNode;
|
2019-07-03 20:14:39 +08:00
|
|
|
}
|
|
|
|
|
2021-06-04 14:44:41 +08:00
|
|
|
return renderLayout(childNode, fieldId, 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;
|