Skip to content

Commit 65dbf00

Browse files
RylanBotuyarn
andauthored
fix(Select): ensure keys props with content/children work correctly (#3829)
* fix(Select): ensure keys props with content/children work correctly * fix(Select): enhance rendering logic for custom keys * chore: centralize keys logic --------- Co-authored-by: wū yāng <uyarnchen@gmail.com>
1 parent 29b3d73 commit 65dbf00

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

packages/components/select/base/Option.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React, { useEffect, useMemo } from 'react';
22
import classNames from 'classnames';
3-
import { isNumber, isString, get } from 'lodash-es';
3+
import { get, isNumber, isString } from 'lodash-es';
44

55
import useConfig from '../../hooks/useConfig';
66
import useDomRefCallback from '../../hooks/useDomRefCallback';
77
import useRipple from '../../hooks/useRipple';
8-
import { StyledProps } from '../../common';
9-
import { SelectValue, TdOptionProps, TdSelectProps, SelectKeysType, SelectOption } from '../type';
8+
9+
import type { StyledProps } from '../../common';
10+
import type { SelectKeysType, SelectOption, SelectValue, TdOptionProps, TdSelectProps } from '../type';
1011

1112
/**
1213
* Option 组件属性
@@ -119,7 +120,8 @@ const Option: React.FC<SelectOptionProps> = (props) => {
119120
}
120121
};
121122

122-
const renderItem = (children: React.ReactNode) => {
123+
const renderItem = () => {
124+
const displayContent = children || content || label;
123125
if (multiple) {
124126
return (
125127
<label
@@ -141,11 +143,11 @@ const Option: React.FC<SelectOptionProps> = (props) => {
141143
}}
142144
/>
143145
<span className={classNames(`${classPrefix}-checkbox__input`)}></span>
144-
<span className={classNames(`${classPrefix}-checkbox__label`)}>{children || content || label}</span>
146+
<span className={classNames(`${classPrefix}-checkbox__label`)}>{displayContent}</span>
145147
</label>
146148
);
147149
}
148-
return <span title={titleContent}>{children || content || label}</span>;
150+
return <span title={titleContent}>{displayContent}</span>;
149151
};
150152

151153
return (
@@ -161,7 +163,7 @@ const Option: React.FC<SelectOptionProps> = (props) => {
161163
ref={setRefCurrent}
162164
style={style}
163165
>
164-
{renderItem(children)}
166+
{renderItem()}
165167
</li>
166168
);
167169
};

packages/components/select/base/PopupContent.tsx

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
import React, { Children, isValidElement, cloneElement, useRef, CSSProperties, useMemo } from 'react';
1+
import React, { Children, cloneElement, isValidElement, useMemo, useRef } from 'react';
2+
23
import classNames from 'classnames';
34
import { isEqual } from 'lodash-es';
5+
6+
import useConfig from '../../hooks/useConfig';
47
import { useLocaleReceiver } from '../../locale/LocalReceiver';
8+
import usePanelVirtualScroll from '../hooks/usePanelVirtualScroll';
59
import { getSelectValueArr } from '../util/helper';
6-
import {
7-
TdSelectProps,
8-
SelectValue,
9-
TdOptionProps,
10-
SelectValueChangeTrigger,
10+
import Option, { type SelectOptionProps } from './Option';
11+
import OptionGroup from './OptionGroup';
12+
13+
import type {
1114
SelectOption,
1215
SelectOptionGroup,
16+
SelectValue,
17+
SelectValueChangeTrigger,
18+
TdOptionProps,
19+
TdSelectProps,
1320
} from '../type';
14-
import useConfig from '../../hooks/useConfig';
15-
import usePanelVirtualScroll from '../hooks/usePanelVirtualScroll';
16-
import Option, { SelectOptionProps } from './Option';
17-
import OptionGroup from './OptionGroup';
1821

1922
interface SelectPopupProps
2023
extends Pick<
@@ -171,7 +174,10 @@ const PopupContent = React.forwardRef<HTMLDivElement, SelectPopupProps>((props,
171174
);
172175
}
173176

174-
const { value: optionValue, label, disabled, content, children, ...restData } = item as TdOptionProps;
177+
const { value: optionValue, label, disabled, children, ...restData } = item as TdOptionProps;
178+
// 当 keys 属性配置 content 作为 value 或 label 时,确保 restData 中也包含它, 不参与渲染计算
179+
const { content } = item as TdOptionProps;
180+
const shouldOmitContent = Object.values(keys || {}).includes('content');
175181
return (
176182
<Option
177183
key={index}
@@ -186,7 +192,6 @@ const PopupContent = React.forwardRef<HTMLDivElement, SelectPopupProps>((props,
186192
disabled={disabled}
187193
restData={restData}
188194
keys={keys}
189-
content={content}
190195
onCheckAllChange={onCheckAllChange}
191196
onRowMounted={handleRowMounted}
192197
{...(isVirtual
@@ -197,6 +202,7 @@ const PopupContent = React.forwardRef<HTMLDivElement, SelectPopupProps>((props,
197202
}
198203
: {})}
199204
{...restData}
205+
content={shouldOmitContent ? null : content}
200206
>
201207
{children}
202208
</Option>
@@ -211,7 +217,7 @@ const PopupContent = React.forwardRef<HTMLDivElement, SelectPopupProps>((props,
211217
const isEmpty =
212218
(Array.isArray(childrenWithProps) && !childrenWithProps.length) || (propsOptions && propsOptions.length === 0);
213219

214-
const renderPanel = (renderedOptions: SelectOption[], extraStyle?: CSSProperties) => (
220+
const renderPanel = (renderedOptions: SelectOption[], extraStyle?: React.CSSProperties) => (
215221
<div
216222
ref={ref}
217223
className={classNames(`${classPrefix}-select__dropdown-inner`, {

0 commit comments

Comments
 (0)