2019-07-03 20:14:39 +08:00
|
|
|
import classNames from 'classnames';
|
2022-06-22 14:57:09 +08:00
|
|
|
import * as React from 'react';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { ColProps } from '../grid/col';
|
|
|
|
import Col from '../grid/col';
|
2020-09-11 21:27:51 +08:00
|
|
|
import { FormContext, FormItemPrefixContext } from './context';
|
|
|
|
import ErrorList from './ErrorList';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { ValidateStatus } from './FormItem';
|
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[];
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
_internalItemRender: formItemRender,
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 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>
|
|
|
|
</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;
|