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[];
|
|
|
|
hasFeedback?: boolean;
|
|
|
|
validateStatus?: ValidateStatus;
|
|
|
|
onDomErrorVisibleChange: (visible: boolean) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface FormItemInputProps {
|
|
|
|
wrapperCol?: ColProps;
|
|
|
|
help?: React.ReactNode;
|
|
|
|
extra?: React.ReactNode;
|
2020-09-11 21:27:51 +08:00
|
|
|
status?: ValidateStatus;
|
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
|
|
|
|
|
|
|
const FormItemInput: React.FC<FormItemInputProps & FormItemInputMiscProps> = ({
|
|
|
|
prefixCls,
|
2020-09-11 21:27:51 +08:00
|
|
|
status,
|
2019-07-03 20:14:39 +08:00
|
|
|
wrapperCol,
|
|
|
|
children,
|
2020-01-14 14:45:22 +08:00
|
|
|
help,
|
2019-07-03 20:14:39 +08:00
|
|
|
errors,
|
|
|
|
onDomErrorVisibleChange,
|
|
|
|
hasFeedback,
|
|
|
|
validateStatus,
|
|
|
|
extra,
|
|
|
|
}) => {
|
|
|
|
const baseClassName = `${prefixCls}-item`;
|
|
|
|
|
|
|
|
const formContext = React.useContext(FormContext);
|
|
|
|
|
|
|
|
const mergedWrapperCol: ColProps = wrapperCol || formContext.wrapperCol || {};
|
|
|
|
|
|
|
|
const className = classNames(`${baseClassName}-control`, mergedWrapperCol.className);
|
|
|
|
|
2020-04-08 18:47:59 +08:00
|
|
|
React.useEffect(
|
|
|
|
() => () => {
|
|
|
|
onDomErrorVisibleChange(false);
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
// 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
|
|
|
|
const subFormContext = { ...formContext };
|
|
|
|
delete subFormContext.labelCol;
|
|
|
|
delete subFormContext.wrapperCol;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormContext.Provider value={subFormContext}>
|
|
|
|
<Col {...mergedWrapperCol} className={className}>
|
|
|
|
<div className={`${baseClassName}-control-input`}>
|
2020-02-19 22:04:25 +08:00
|
|
|
<div className={`${baseClassName}-control-input-content`}>{children}</div>
|
2019-07-03 20:14:39 +08:00
|
|
|
{icon}
|
|
|
|
</div>
|
2020-09-11 21:27:51 +08:00
|
|
|
<FormItemPrefixContext.Provider value={{ prefixCls, status }}>
|
|
|
|
<ErrorList
|
|
|
|
errors={errors}
|
|
|
|
help={help}
|
|
|
|
onDomErrorVisibleChange={onDomErrorVisibleChange}
|
|
|
|
/>
|
|
|
|
</FormItemPrefixContext.Provider>
|
2019-07-03 20:14:39 +08:00
|
|
|
{extra && <div className={`${baseClassName}-extra`}>{extra}</div>}
|
|
|
|
</Col>
|
|
|
|
</FormContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FormItemInput;
|