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-12-26 15:48:52 +08:00
|
|
|
import useMemo from 'rc-util/lib/hooks/useMemo';
|
2019-07-03 20:14:39 +08:00
|
|
|
import CSSMotion from 'rc-animate/lib/CSSMotion';
|
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';
|
|
|
|
import { FormContext } from './context';
|
|
|
|
import { useCacheErrors } from './util';
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
wrapperCol,
|
|
|
|
children,
|
2020-01-14 14:45:22 +08:00
|
|
|
help,
|
2019-07-03 20:14:39 +08:00
|
|
|
errors,
|
|
|
|
onDomErrorVisibleChange,
|
|
|
|
hasFeedback,
|
|
|
|
validateStatus,
|
|
|
|
extra,
|
|
|
|
}) => {
|
2019-12-26 15:48:52 +08:00
|
|
|
const [, forceUpdate] = React.useState({});
|
|
|
|
|
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);
|
|
|
|
|
2020-01-17 11:50:06 +08:00
|
|
|
const [visible, cacheErrors] = useCacheErrors(
|
|
|
|
errors,
|
|
|
|
changedVisible => {
|
|
|
|
if (changedVisible) {
|
2020-03-03 12:10:32 +08:00
|
|
|
/**
|
|
|
|
* We trigger in sync to avoid dom shaking but this get warning in react 16.13.
|
|
|
|
* So use Promise to keep in micro async to handle this.
|
|
|
|
* https://github.com/ant-design/ant-design/issues/21698#issuecomment-593743485
|
|
|
|
*/
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
onDomErrorVisibleChange(true);
|
|
|
|
});
|
2020-01-17 11:50:06 +08:00
|
|
|
}
|
|
|
|
forceUpdate({});
|
|
|
|
},
|
|
|
|
!!help,
|
|
|
|
);
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2020-04-08 18:47:59 +08:00
|
|
|
React.useEffect(
|
|
|
|
() => () => {
|
|
|
|
onDomErrorVisibleChange(false);
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
2019-12-26 15:48:52 +08:00
|
|
|
const memoErrors = useMemo(
|
|
|
|
() => cacheErrors,
|
|
|
|
visible,
|
|
|
|
(_, nextVisible) => nextVisible,
|
|
|
|
);
|
|
|
|
|
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>
|
|
|
|
<CSSMotion
|
|
|
|
visible={visible}
|
|
|
|
motionName="show-help"
|
|
|
|
onLeaveEnd={() => {
|
|
|
|
onDomErrorVisibleChange(false);
|
|
|
|
}}
|
|
|
|
motionAppear
|
|
|
|
removeOnLeave
|
|
|
|
>
|
|
|
|
{({ className: motionClassName }: { className: string }) => {
|
|
|
|
return (
|
|
|
|
<div className={classNames(`${baseClassName}-explain`, motionClassName)} key="help">
|
2019-12-28 14:53:43 +08:00
|
|
|
{memoErrors.map((error, index) => (
|
|
|
|
// eslint-disable-next-line react/no-array-index-key
|
|
|
|
<div key={index}>{error}</div>
|
|
|
|
))}
|
2019-07-03 20:14:39 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</CSSMotion>
|
|
|
|
{extra && <div className={`${baseClassName}-extra`}>{extra}</div>}
|
|
|
|
</Col>
|
|
|
|
</FormContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FormItemInput;
|