Skip to content
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

fix(Select): fix option title #3058

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 11 additions & 2 deletions src/select/_example/base.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { Select } from 'tdesign-react';
import { Select, Tooltip } from 'tdesign-react';

export default function SingleSelect() {
const [value, setValue] = useState('');
Expand All @@ -18,7 +18,16 @@ export default function SingleSelect() {
{ label: '大数据', value: '2' },
{ label: '区块链', value: '3' },
{ label: '物联网', value: '4', disabled: true },
{ label: '人工智能', value: '5', content: <span>人工智能(新)</span> },
{
label: '人工智能',
value: '5',
content: (
<Tooltip content="人工智能">
<span>人工智能(新)</span>
</Tooltip>
),
title: null,
},
]}
/>
);
Expand Down
15 changes: 12 additions & 3 deletions src/select/base/Option.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from 'react';
import React, { useEffect, useMemo } from 'react';
import classNames from 'classnames';
import isNumber from 'lodash/isNumber';
import isString from 'lodash/isString';
Expand Down Expand Up @@ -64,6 +64,15 @@ const Option: React.FC<SelectOptionProps> = (props) => {
const label = propLabel || value;
const disabled = propDisabled || (multiple && Array.isArray(selectedValue) && max && selectedValue.length >= max);

const titleContent = useMemo(() => {
// 外部设置 props,说明希望受控
const controlledTitle = Reflect.has(props, 'title');
if (controlledTitle) return propTitle;
if (typeof label === 'string') return label;
return null;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [propTitle, label]);

const { classPrefix } = useConfig();

// 使用斜八角动画
Expand Down Expand Up @@ -121,7 +130,7 @@ const Option: React.FC<SelectOptionProps> = (props) => {
[`${classPrefix}-is-disabled`]: disabled,
[`${classPrefix}-is-checked`]: selected,
})}
title={propTitle || (label as string)}
title={titleContent}
>
<input
type="checkbox"
Expand All @@ -138,7 +147,7 @@ const Option: React.FC<SelectOptionProps> = (props) => {
</label>
);
}
return <span title={propTitle || (label as string)}>{children || content || label}</span>;
return <span title={titleContent}>{children || content || label}</span>;
};

return (
Expand Down
Loading