2019-07-03 20:14:39 +08:00
|
|
|
|
import * as React from 'react';
|
|
|
|
|
import classNames from 'classnames';
|
2020-09-17 17:11:45 +08:00
|
|
|
|
import QuestionCircleOutlined from '@ant-design/icons/QuestionCircleOutlined';
|
2019-07-03 20:14:39 +08:00
|
|
|
|
import Col, { ColProps } from '../grid/col';
|
|
|
|
|
import { FormLabelAlign } from './interface';
|
|
|
|
|
import { FormContext, FormContextProps } from './context';
|
2020-08-21 12:58:14 +08:00
|
|
|
|
import { RequiredMark } from './Form';
|
|
|
|
|
import { useLocaleReceiver } from '../locale-provider/LocaleReceiver';
|
|
|
|
|
import defaultLocale from '../locale/default';
|
2020-09-17 17:11:45 +08:00
|
|
|
|
import Tooltip, { TooltipProps } from '../tooltip';
|
|
|
|
|
|
|
|
|
|
export type WrapperTooltipProps = TooltipProps & {
|
|
|
|
|
icon?: React.ReactElement;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type LabelTooltipType = WrapperTooltipProps | React.ReactNode;
|
|
|
|
|
|
|
|
|
|
function toTooltipProps(tooltip: LabelTooltipType): WrapperTooltipProps | null {
|
|
|
|
|
if (!tooltip) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-10 13:32:27 +08:00
|
|
|
|
if (typeof tooltip === 'object' && !React.isValidElement(tooltip)) {
|
2020-09-17 17:11:45 +08:00
|
|
|
|
return tooltip as WrapperTooltipProps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
title: tooltip,
|
|
|
|
|
};
|
|
|
|
|
}
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
|
|
export interface FormItemLabelProps {
|
|
|
|
|
colon?: boolean;
|
2019-07-04 15:00:11 +08:00
|
|
|
|
htmlFor?: string;
|
2019-07-03 20:14:39 +08:00
|
|
|
|
label?: React.ReactNode;
|
|
|
|
|
labelAlign?: FormLabelAlign;
|
|
|
|
|
labelCol?: ColProps;
|
2020-08-21 12:58:14 +08:00
|
|
|
|
requiredMark?: RequiredMark;
|
2020-09-17 17:11:45 +08:00
|
|
|
|
tooltip?: LabelTooltipType;
|
2019-07-03 20:14:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-17 11:50:06 +08:00
|
|
|
|
const FormItemLabel: React.FC<FormItemLabelProps & { required?: boolean; prefixCls: string }> = ({
|
2019-07-03 20:14:39 +08:00
|
|
|
|
prefixCls,
|
|
|
|
|
label,
|
|
|
|
|
htmlFor,
|
|
|
|
|
labelCol,
|
|
|
|
|
labelAlign,
|
|
|
|
|
colon,
|
|
|
|
|
required,
|
2020-08-21 12:58:14 +08:00
|
|
|
|
requiredMark,
|
2020-09-17 17:11:45 +08:00
|
|
|
|
tooltip,
|
2019-07-03 20:14:39 +08:00
|
|
|
|
}) => {
|
2020-08-21 12:58:14 +08:00
|
|
|
|
const [formLocale] = useLocaleReceiver('Form');
|
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
|
if (!label) return null;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<FormContext.Consumer key="label">
|
|
|
|
|
{({
|
|
|
|
|
vertical,
|
|
|
|
|
labelAlign: contextLabelAlign,
|
|
|
|
|
labelCol: contextLabelCol,
|
2021-11-26 18:15:59 +08:00
|
|
|
|
labelWrap,
|
2019-07-03 20:14:39 +08:00
|
|
|
|
colon: contextColon,
|
|
|
|
|
}: FormContextProps) => {
|
|
|
|
|
const mergedLabelCol: ColProps = labelCol || contextLabelCol || {};
|
|
|
|
|
|
|
|
|
|
const mergedLabelAlign: FormLabelAlign | undefined = labelAlign || contextLabelAlign;
|
|
|
|
|
|
|
|
|
|
const labelClsBasic = `${prefixCls}-item-label`;
|
|
|
|
|
const labelColClassName = classNames(
|
|
|
|
|
labelClsBasic,
|
|
|
|
|
mergedLabelAlign === 'left' && `${labelClsBasic}-left`,
|
|
|
|
|
mergedLabelCol.className,
|
2021-11-26 18:15:59 +08:00
|
|
|
|
{
|
|
|
|
|
[`${labelClsBasic}-wrap`]: !!labelWrap,
|
|
|
|
|
},
|
2019-07-03 20:14:39 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let labelChildren = label;
|
|
|
|
|
// Keep label is original where there should have no colon
|
|
|
|
|
const computedColon = colon === true || (contextColon !== false && colon !== false);
|
|
|
|
|
const haveColon = computedColon && !vertical;
|
|
|
|
|
// Remove duplicated user input colon
|
|
|
|
|
if (haveColon && typeof label === 'string' && (label as string).trim() !== '') {
|
2020-04-03 15:02:11 +08:00
|
|
|
|
labelChildren = (label as string).replace(/[:|:]\s*$/, '');
|
2019-07-03 20:14:39 +08:00
|
|
|
|
}
|
2020-09-17 17:11:45 +08:00
|
|
|
|
|
|
|
|
|
// Tooltip
|
|
|
|
|
const tooltipProps = toTooltipProps(tooltip);
|
|
|
|
|
if (tooltipProps) {
|
|
|
|
|
const { icon = <QuestionCircleOutlined />, ...restTooltipProps } = tooltipProps;
|
|
|
|
|
const tooltipNode = (
|
|
|
|
|
<Tooltip {...restTooltipProps}>
|
2021-08-13 17:06:30 +08:00
|
|
|
|
{React.cloneElement(icon, { className: `${prefixCls}-item-tooltip`, title: '' })}
|
2020-09-17 17:11:45 +08:00
|
|
|
|
</Tooltip>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
labelChildren = (
|
|
|
|
|
<>
|
|
|
|
|
{labelChildren}
|
|
|
|
|
{tooltipNode}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
2020-08-21 12:58:14 +08:00
|
|
|
|
// Add required mark if optional
|
|
|
|
|
if (requiredMark === 'optional' && !required) {
|
|
|
|
|
labelChildren = (
|
|
|
|
|
<>
|
|
|
|
|
{labelChildren}
|
2021-08-13 17:06:30 +08:00
|
|
|
|
<span className={`${prefixCls}-item-optional`} title="">
|
2020-08-21 12:58:14 +08:00
|
|
|
|
{formLocale?.optional || defaultLocale.Form?.optional}
|
|
|
|
|
</span>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
|
const labelClassName = classNames({
|
|
|
|
|
[`${prefixCls}-item-required`]: required,
|
2020-08-21 12:58:14 +08:00
|
|
|
|
[`${prefixCls}-item-required-mark-optional`]: requiredMark === 'optional',
|
2019-07-03 20:14:39 +08:00
|
|
|
|
[`${prefixCls}-item-no-colon`]: !computedColon,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Col {...mergedLabelCol} className={labelColClassName}>
|
|
|
|
|
<label
|
|
|
|
|
htmlFor={htmlFor}
|
|
|
|
|
className={labelClassName}
|
|
|
|
|
title={typeof label === 'string' ? label : ''}
|
|
|
|
|
>
|
|
|
|
|
{labelChildren}
|
|
|
|
|
</label>
|
|
|
|
|
</Col>
|
|
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
</FormContext.Consumer>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default FormItemLabel;
|