Skip to content

Commit 398710c

Browse files
feat(Form): FormItem add tooltip attributes (#7014)
* feat(Form): FormItem add tooltip attributes * fix: Delete duplicate types * fix: Remove excess slots and types * Update FormItemLabel.tsx --------- Co-authored-by: undefined <undefined> Co-authored-by: tangjinzhou <415800467@qq.com>
1 parent 858c4ec commit 398710c

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

components/form/FormItem.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export const formItemProps = () => ({
126126
messageVariables: { type: Object as PropType<Record<string, string>> },
127127
hidden: Boolean,
128128
noStyle: Boolean,
129+
tooltip: String,
129130
});
130131

131132
export type FormItemProps = Partial<ExtractPropTypes<ReturnType<typeof formItemProps>>>;
@@ -155,6 +156,7 @@ export default defineComponent({
155156
label: any;
156157
extra: any;
157158
default: any;
159+
tooltip: any;
158160
}>,
159161
setup(props, { slots, attrs, expose }) {
160162
warning(props.prop === undefined, `\`prop\` is deprecated. Please use \`name\` instead.`);
@@ -505,7 +507,8 @@ export default defineComponent({
505507
requiredMark={formContext.requiredMark.value}
506508
prefixCls={prefixCls.value}
507509
onClick={onLabelClick}
508-
label={props.label ?? slots.label?.()}
510+
label={props.label}
511+
v-slots={{ label: slots.label, tooltip: slots.tooltip }}
509512
/>
510513
{/* Input Group */}
511514
<FormItemInput

components/form/FormItemLabel.tsx

+20-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import defaultLocale from '../locale/en_US';
88
import classNames from '../_util/classNames';
99
import type { VueNode } from '../_util/type';
1010
import type { FunctionalComponent, HTMLAttributes } from 'vue';
11+
import Tooltip from '../tooltip';
12+
import QuestionCircleOutlined from '@ant-design/icons-vue/QuestionCircleOutlined';
1113

1214
export interface FormItemLabelProps {
1315
colon?: boolean;
@@ -19,6 +21,7 @@ export interface FormItemLabelProps {
1921
required?: boolean;
2022
prefixCls: string;
2123
onClick: Function;
24+
tooltip: string;
2225
}
2326

2427
const FormItemLabel: FunctionalComponent<FormItemLabelProps> = (props, { slots, emit, attrs }) => {
@@ -59,12 +62,23 @@ const FormItemLabel: FunctionalComponent<FormItemLabelProps> = (props, { slots,
5962
labelChildren = (label as string).replace(/[:|]\s*$/, '');
6063
}
6164

62-
labelChildren = (
63-
<>
64-
{labelChildren}
65-
{slots.tooltip?.({ class: `${prefixCls}-item-tooltip` })}
66-
</>
67-
);
65+
// Tooltip
66+
if (props.tooltip || slots.tooltip) {
67+
const tooltipNode = (
68+
<span class={`${prefixCls}-item-tooltip`}>
69+
<Tooltip title={props.tooltip}>
70+
<QuestionCircleOutlined />
71+
</Tooltip>
72+
</span>
73+
);
74+
75+
labelChildren = (
76+
<>
77+
{labelChildren}
78+
{slots.tooltip ? slots.tooltip?.({ class: `${prefixCls}-item-tooltip` }) : tooltipNode}
79+
</>
80+
);
81+
}
6882

6983
// Add required mark if optional
7084
if (requiredMark === 'optional' && !required) {

components/form/index.en-US.md

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ A form consists of one or more form fields whose type includes input, textarea,
8787
| name | a key of `model`. In the use of validate and resetFields method, the attribute is required | [NamePath](#namepath) | | 2.0.0 |
8888
| required | Whether provided or not, it will be generated by the validation rule. | boolean | false | |
8989
| rules | validation rules of form | object \| array | | |
90+
| tooltip | Config tooltip info | string \| slot | | 4.0.4 |
9091
| validateFirst | Whether stop validate on first rule of error for this field. | boolean | false | |
9192
| validateStatus | The validation status. If not provided, it will be generated by validation rule. options: 'success' 'warning' 'error' 'validating' | string | | |
9293
| validateTrigger | When to validate the value of children node | string \| string\[] | `change` | |

components/form/index.zh-CN.md

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*ylFATY6w-ygAAA
8888
| name | 表单域 model 字段,在使用 validate、resetFields 方法的情况下,该属性是必填的 | [NamePath](#namepath) | | |
8989
| required | 是否必填,如不设置,则会根据校验规则自动生成 | boolean | false | |
9090
| rules | 表单验证规则 | object \| array | | |
91+
| tooltip | 配置提示信息 | string \| slot | | 4.0.4 |
9192
| validateFirst | 当某一规则校验不通过时,是否停止剩下的规则的校验。 | boolean | false | 2.0.0 |
9293
| validateStatus | 校验状态,如不设置,则会根据校验规则自动生成,可选:'success' 'warning' 'error' 'validating' | string | | |
9394
| validateTrigger | 设置字段校验的时机 | string \| string\[] | `change` | 2.0.0 |

0 commit comments

Comments
 (0)