Skip to content

Commit

Permalink
fix(Select): fix option title (#3058)
Browse files Browse the repository at this point in the history
  • Loading branch information
uyarn authored Aug 22, 2024
1 parent 1128644 commit 8e37532
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
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

0 comments on commit 8e37532

Please sign in to comment.