Skip to content

feat(Form): FormItem add tooltip attributes #7014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion components/form/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { FormItemInputContext, useProvideFormItemContext } from './FormItemConte
import useDebounce from './utils/useDebounce';
import classNames from '../_util/classNames';
import useStyle from './style';
import type { TooltipProps } from '../tooltip';

const ValidateStatuses = tuple('success', 'warning', 'error', 'validating', '');
export type ValidateStatus = (typeof ValidateStatuses)[number];
Expand Down Expand Up @@ -126,6 +127,7 @@ export const formItemProps = () => ({
messageVariables: { type: Object as PropType<Record<string, string>> },
hidden: Boolean,
noStyle: Boolean,
tooltip: [String, Object] as PropType<string | TooltipProps>,
});

export type FormItemProps = Partial<ExtractPropTypes<ReturnType<typeof formItemProps>>>;
Expand Down Expand Up @@ -155,6 +157,8 @@ export default defineComponent({
label: any;
extra: any;
default: any;
icon: any;
tooltip: any;
}>,
setup(props, { slots, attrs, expose }) {
warning(props.prop === undefined, `\`prop\` is deprecated. Please use \`name\` instead.`);
Expand Down Expand Up @@ -505,7 +509,8 @@ export default defineComponent({
requiredMark={formContext.requiredMark.value}
prefixCls={prefixCls.value}
onClick={onLabelClick}
label={props.label ?? slots.label?.()}
label={props.label}
v-slots={{ icon: slots.icon, label: slots.label, tooltip: slots.tooltip }}
/>
{/* Input Group */}
<FormItemInput
Expand Down
43 changes: 37 additions & 6 deletions components/form/FormItemLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ import defaultLocale from '../locale/en_US';
import classNames from '../_util/classNames';
import type { VueNode } from '../_util/type';
import type { FunctionalComponent, HTMLAttributes } from 'vue';
import Tooltip from '../tooltip';
import type { TooltipProps } from '../tooltip';
import { QuestionCircleOutlined } from '@ant-design/icons-vue';

function toTooltipProps(tooltip) {
if (!tooltip) {
return null;
}

if (typeof tooltip === 'object') {
return tooltip as TooltipProps;
}

return {
title: tooltip,
};
}

export interface FormItemLabelProps {
colon?: boolean;
Expand All @@ -19,6 +36,8 @@ export interface FormItemLabelProps {
required?: boolean;
prefixCls: string;
onClick: Function;
tooltip: string;
tooltip: string | TooltipProps;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是不是重复了

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1

}

const FormItemLabel: FunctionalComponent<FormItemLabelProps> = (props, { slots, emit, attrs }) => {
Expand Down Expand Up @@ -59,12 +78,24 @@ const FormItemLabel: FunctionalComponent<FormItemLabelProps> = (props, { slots,
labelChildren = (label as string).replace(/[:|:]\s*$/, '');
}

labelChildren = (
<>
{labelChildren}
{slots.tooltip?.({ class: `${prefixCls}-item-tooltip` })}
</>
);
// Tooltip
if (props.tooltip || slots.tooltip) {
const tooltipProps = toTooltipProps(props.tooltip);
const tooltipNode = (
<span class={`${prefixCls}-item-tooltip`}>
<Tooltip {...tooltipProps}>
{slots.icon ? slots.icon?.() : <QuestionCircleOutlined />}
</Tooltip>
</span>
);

labelChildren = (
<>
{labelChildren}
{slots.tooltip ? slots.tooltip?.({ className: `${prefixCls}-item-tooltip` }) : tooltipNode}
</>
);
}

// Add required mark if optional
if (requiredMark === 'optional' && !required) {
Expand Down
2 changes: 2 additions & 0 deletions components/form/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ A form consists of one or more form fields whose type includes input, textarea,
| hasFeedback | Used with `validateStatus`, this option specifies the validation status icon. Recommended to be used only with `Input`. | boolean | false | |
| help | The prompt message. If not provided, the prompt message will be generated by the validation rule. | string\|slot | | |
| htmlFor | Set sub label `htmlFor`. | string | | |
| icon | Custom tooltip display icon, Need to be used in conjunction with tooltip attributes. | slot | | 4.0.4 |
| label | Label text | string\|slot | | |
| labelAlign | text align of label | 'left' \| 'right' | 'right' | |
| labelCol | The layout of label. You can set `span` `offset` to something like `{span: 3, offset: 12}` or `sm: {span: 3, offset: 12}` same as with `<Col>` | [object](/components/grid/#col) | | |
| name | a key of `model`. In the use of validate and resetFields method, the attribute is required | [NamePath](#namepath) | | 2.0.0 |
| required | Whether provided or not, it will be generated by the validation rule. | boolean | false | |
| rules | validation rules of form | object \| array | | |
| tooltip | Config tooltip info | string \| object \| slot | | 4.0.4 |
| validateFirst | Whether stop validate on first rule of error for this field. | boolean | false | |
| validateStatus | The validation status. If not provided, it will be generated by validation rule. options: 'success' 'warning' 'error' 'validating' | string | | |
| validateTrigger | When to validate the value of children node | string \| string\[] | `change` | |
Expand Down
2 changes: 2 additions & 0 deletions components/form/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*ylFATY6w-ygAAA
| hasFeedback | 配合 validateStatus 属性使用,展示校验状态图标,建议只配合 Input 组件使用 | boolean | false | |
| help | 提示信息,如不设置,则会根据校验规则自动生成 | string\|slot | | |
| htmlFor | 设置子元素 label `htmlFor` 属性 | string | | |
| icon | 自定义 tooltip 展示的图标,需要配合 tooltip 属性使用 | slot | | 4.0.4 |
| label | label 标签的文本 | string\|slot | | |
| labelAlign | 标签文本对齐方式 | 'left' \| 'right' | 'right' | |
| labelCol | label 标签布局,同 `<Col>` 组件,设置 `span` `offset` 值,如 `{span: 3, offset: 12}` 或 `sm: {span: 3, offset: 12}` | [object](/components/grid-cn/#col) | | |
| name | 表单域 model 字段,在使用 validate、resetFields 方法的情况下,该属性是必填的 | [NamePath](#namepath) | | |
| required | 是否必填,如不设置,则会根据校验规则自动生成 | boolean | false | |
| rules | 表单验证规则 | object \| array | | |
| tooltip | 配置提示信息 | string \| object \| slot | | 4.0.4 |
| validateFirst | 当某一规则校验不通过时,是否停止剩下的规则的校验。 | boolean | false | 2.0.0 |
| validateStatus | 校验状态,如不设置,则会根据校验规则自动生成,可选:'success' 'warning' 'error' 'validating' | string | | |
| validateTrigger | 设置字段校验的时机 | string \| string\[] | `change` | 2.0.0 |
Expand Down