2019-07-03 20:14:39 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import classNames from 'classnames';
|
2020-03-02 12:09:38 +08:00
|
|
|
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
|
|
|
|
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
|
|
|
|
import CheckCircleFilled from '@ant-design/icons/CheckCircleFilled';
|
|
|
|
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
import Col, { ColProps } from '../grid/col';
|
|
|
|
import { ValidateStatus } from './FormItem';
|
2020-09-11 21:27:51 +08:00
|
|
|
import { FormContext, FormItemPrefixContext } from './context';
|
|
|
|
import ErrorList from './ErrorList';
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
interface FormItemInputMiscProps {
|
|
|
|
prefixCls: string;
|
|
|
|
children: React.ReactNode;
|
|
|
|
errors: React.ReactNode[];
|
2021-06-04 14:44:41 +08:00
|
|
|
warnings: React.ReactNode[];
|
2019-07-03 20:14:39 +08:00
|
|
|
hasFeedback?: boolean;
|
|
|
|
validateStatus?: ValidateStatus;
|
2021-02-23 10:45:11 +08:00
|
|
|
/** @private Internal Usage, do not use in any of your production. */
|
2020-11-20 21:43:43 +08:00
|
|
|
_internalItemRender?: {
|
|
|
|
mark: string;
|
|
|
|
render: (
|
|
|
|
props: FormItemInputProps & FormItemInputMiscProps,
|
|
|
|
domList: {
|
|
|
|
input: JSX.Element;
|
|
|
|
errorList: JSX.Element;
|
|
|
|
extra: JSX.Element | null;
|
|
|
|
},
|
|
|
|
) => React.ReactNode;
|
|
|
|
};
|
2019-07-03 20:14:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface FormItemInputProps {
|
|
|
|
wrapperCol?: ColProps;
|
|
|
|
extra?: React.ReactNode;
|
2020-09-11 21:27:51 +08:00
|
|
|
status?: ValidateStatus;
|
2021-06-04 14:44:41 +08:00
|
|
|
help?: React.ReactNode;
|
2019-07-03 20:14:39 +08:00
|
|
|
}
|
|
|
|
|
2019-08-13 14:07:17 +08:00
|
|
|
const iconMap: { [key: string]: any } = {
|
|
|
|
success: CheckCircleFilled,
|
|
|
|
warning: ExclamationCircleFilled,
|
|
|
|
error: CloseCircleFilled,
|
2019-11-28 12:34:33 +08:00
|
|
|
validating: LoadingOutlined,
|
2019-11-20 17:31:11 +08:00
|
|
|
};
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2020-11-20 21:43:43 +08:00
|
|
|
const FormItemInput: React.FC<FormItemInputProps & FormItemInputMiscProps> = props => {
|
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
status,
|
|
|
|
wrapperCol,
|
|
|
|
children,
|
|
|
|
errors,
|
2021-06-04 14:44:41 +08:00
|
|
|
warnings,
|
2020-11-20 21:43:43 +08:00
|
|
|
hasFeedback,
|
|
|
|
_internalItemRender: formItemRender,
|
|
|
|
validateStatus,
|
|
|
|
extra,
|
2021-06-04 14:44:41 +08:00
|
|
|
help,
|
2020-11-20 21:43:43 +08:00
|
|
|
} = props;
|
2019-07-03 20:14:39 +08:00
|
|
|
const baseClassName = `${prefixCls}-item`;
|
|
|
|
|
|
|
|
const formContext = React.useContext(FormContext);
|
|
|
|
|
|
|
|
const mergedWrapperCol: ColProps = wrapperCol || formContext.wrapperCol || {};
|
|
|
|
|
|
|
|
const className = classNames(`${baseClassName}-control`, mergedWrapperCol.className);
|
|
|
|
|
|
|
|
// Should provides additional icon if `hasFeedback`
|
2019-11-20 17:31:11 +08:00
|
|
|
const IconNode = validateStatus && iconMap[validateStatus];
|
2019-07-03 20:14:39 +08:00
|
|
|
const icon =
|
2019-11-20 17:31:11 +08:00
|
|
|
hasFeedback && IconNode ? (
|
2019-07-03 20:14:39 +08:00
|
|
|
<span className={`${baseClassName}-children-icon`}>
|
2019-11-20 17:31:11 +08:00
|
|
|
<IconNode />
|
2019-07-03 20:14:39 +08:00
|
|
|
</span>
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
// Pass to sub FormItem should not with col info
|
2021-11-26 12:18:21 +08:00
|
|
|
const subFormContext = React.useMemo(() => ({ ...formContext }), [formContext]);
|
2019-07-03 20:14:39 +08:00
|
|
|
delete subFormContext.labelCol;
|
|
|
|
delete subFormContext.wrapperCol;
|
|
|
|
|
2020-11-20 21:43:43 +08:00
|
|
|
const inputDom = (
|
|
|
|
<div className={`${baseClassName}-control-input`}>
|
|
|
|
<div className={`${baseClassName}-control-input-content`}>{children}</div>
|
|
|
|
{icon}
|
|
|
|
</div>
|
|
|
|
);
|
2021-11-26 12:18:21 +08:00
|
|
|
const formItemContext = React.useMemo(() => ({ prefixCls, status }), [prefixCls, status]);
|
2020-11-20 21:43:43 +08:00
|
|
|
const errorListDom = (
|
2021-11-26 12:18:21 +08:00
|
|
|
<FormItemPrefixContext.Provider value={formItemContext}>
|
2021-06-07 14:32:07 +08:00
|
|
|
<ErrorList
|
|
|
|
errors={errors}
|
|
|
|
warnings={warnings}
|
|
|
|
help={help}
|
|
|
|
helpStatus={status}
|
|
|
|
className={`${baseClassName}-explain-connected`}
|
|
|
|
/>
|
2020-11-20 21:43:43 +08:00
|
|
|
</FormItemPrefixContext.Provider>
|
|
|
|
);
|
|
|
|
|
|
|
|
// If extra = 0, && will goes wrong
|
|
|
|
// 0&&error -> 0
|
|
|
|
const extraDom = extra ? <div className={`${baseClassName}-extra`}>{extra}</div> : null;
|
|
|
|
|
|
|
|
const dom =
|
|
|
|
formItemRender && formItemRender.mark === 'pro_table_render' && formItemRender.render ? (
|
|
|
|
formItemRender.render(props, { input: inputDom, errorList: errorListDom, extra: extraDom })
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
{inputDom}
|
|
|
|
{errorListDom}
|
|
|
|
{extraDom}
|
|
|
|
</>
|
|
|
|
);
|
2019-07-03 20:14:39 +08:00
|
|
|
return (
|
|
|
|
<FormContext.Provider value={subFormContext}>
|
|
|
|
<Col {...mergedWrapperCol} className={className}>
|
2020-11-20 21:43:43 +08:00
|
|
|
{dom}
|
2019-07-03 20:14:39 +08:00
|
|
|
</Col>
|
|
|
|
</FormContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FormItemInput;
|