ant-design/components/form/FormItemInput.tsx
二货机器人 03ea767cad
fix: Form validate message shaking (#36575)
* refactor: move component out

* refactor: clean up code

* chore: stable it

* chore: back of container

* chore: adjust leave motion

* test: update snapshot

* chore: update motion logic

* fix: list start motion

* test: update snapshot

* test: update snapshot

* test: test for marginBottom
2022-07-19 16:01:31 +08:00

111 lines
3.3 KiB
TypeScript

import classNames from 'classnames';
import * as React from 'react';
import type { ColProps } from '../grid/col';
import Col from '../grid/col';
import { FormContext, FormItemPrefixContext } from './context';
import ErrorList from './ErrorList';
import type { ValidateStatus } from './FormItem';
interface FormItemInputMiscProps {
prefixCls: string;
children: React.ReactNode;
errors: React.ReactNode[];
warnings: React.ReactNode[];
marginBottom?: number | null;
onErrorVisibleChanged?: (visible: boolean) => void;
/** @private Internal Usage, do not use in any of your production. */
_internalItemRender?: {
mark: string;
render: (
props: FormItemInputProps & FormItemInputMiscProps,
domList: {
input: JSX.Element;
errorList: JSX.Element | null;
extra: JSX.Element | null;
},
) => React.ReactNode;
};
}
export interface FormItemInputProps {
wrapperCol?: ColProps;
extra?: React.ReactNode;
status?: ValidateStatus;
help?: React.ReactNode;
}
const FormItemInput: React.FC<FormItemInputProps & FormItemInputMiscProps> = props => {
const {
prefixCls,
status,
wrapperCol,
children,
errors,
warnings,
_internalItemRender: formItemRender,
extra,
help,
marginBottom,
onErrorVisibleChanged,
} = props;
const baseClassName = `${prefixCls}-item`;
const formContext = React.useContext(FormContext);
const mergedWrapperCol: ColProps = wrapperCol || formContext.wrapperCol || {};
const className = classNames(`${baseClassName}-control`, mergedWrapperCol.className);
// Pass to sub FormItem should not with col info
const subFormContext = React.useMemo(() => ({ ...formContext }), [formContext]);
delete subFormContext.labelCol;
delete subFormContext.wrapperCol;
const inputDom = (
<div className={`${baseClassName}-control-input`}>
<div className={`${baseClassName}-control-input-content`}>{children}</div>
</div>
);
const formItemContext = React.useMemo(() => ({ prefixCls, status }), [prefixCls, status]);
const errorListDom =
marginBottom !== null || errors.length || warnings.length ? (
<div style={{ display: 'flex', flexWrap: 'nowrap' }}>
<FormItemPrefixContext.Provider value={formItemContext}>
<ErrorList
errors={errors}
warnings={warnings}
help={help}
helpStatus={status}
className={`${baseClassName}-explain-connected`}
onVisibleChanged={onErrorVisibleChanged}
/>
</FormItemPrefixContext.Provider>
{!!marginBottom && <div style={{ width: 0, height: marginBottom }} />}
</div>
) : null;
// 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}
</>
);
return (
<FormContext.Provider value={subFormContext}>
<Col {...mergedWrapperCol} className={className}>
{dom}
</Col>
</FormContext.Provider>
);
};
export default FormItemInput;