Skip to content

Commit 821976b

Browse files
authored
fix: prevent extra props from being passed to selected item display (#1189)
1 parent aab0e1a commit 821976b

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/SelectInput/Content/SingleContent.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
3131

3232
// Extract option props, excluding label and value, and handle className/style merging
3333
const optionProps = React.useMemo(() => {
34-
let restProps: React.HTMLAttributes<HTMLDivElement> = {
34+
const restProps: React.HTMLAttributes<HTMLDivElement> = {
3535
className: `${prefixCls}-content-value`,
3636
style: {
3737
visibility: mergedSearchValue ? 'hidden' : 'visible',
@@ -41,15 +41,12 @@ const SingleContent = React.forwardRef<HTMLInputElement, SharedContentProps>(
4141
if (displayValue && selectContext?.flattenOptions) {
4242
const option = selectContext.flattenOptions.find((opt) => opt.value === displayValue.value);
4343
if (option?.data) {
44-
const { label, value, className, style, key, ...rest } = option.data;
45-
46-
restProps = {
47-
...restProps,
48-
...rest,
44+
const { className, style } = option.data;
45+
Object.assign(restProps, {
4946
title: getTitle(option.data),
5047
className: clsx(restProps.className, className),
5148
style: { ...restProps.style, ...style },
52-
};
49+
});
5350
}
5451
}
5552

tests/Select.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,4 +2853,32 @@ describe('Select.Basic', () => {
28532853
expect(select).toHaveClass('rc-select-focused');
28542854
});
28552855
});
2856+
2857+
it('should not pass extra props to the selected item display', () => {
2858+
const { container } = render(
2859+
<Select
2860+
value="1"
2861+
open
2862+
options={[
2863+
{
2864+
value: '1',
2865+
label: 'One',
2866+
className: 'my-option-class',
2867+
style: { color: 'red' },
2868+
title: 'my-title',
2869+
['xxx' as any]: 'should-not-be-here',
2870+
},
2871+
]}
2872+
/>,
2873+
);
2874+
2875+
const selectedItem = container.querySelector('.rc-select-content-value');
2876+
2877+
expect(selectedItem).toBeTruthy();
2878+
expect(selectedItem).toHaveClass('my-option-class');
2879+
expect(selectedItem).toHaveStyle({ color: 'red' });
2880+
expect(selectedItem).toHaveAttribute('title', 'my-title');
2881+
2882+
expect(selectedItem).not.toHaveAttribute('xxx');
2883+
});
28562884
});

0 commit comments

Comments
 (0)