Skip to content

Commit a635ec0

Browse files
authored
feat: comobobox mode should not has selected item (#738)
close ant-design/ant-design#34975
1 parent dd89dc3 commit a635ec0

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

assets/index.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@
238238

239239
// ------- Active -------
240240
&-active {
241-
background: green;
241+
background: #ddd;
242242
}
243243

244244
// ------ Disabled ------

src/OptionList.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,17 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, OptionListP
3636
_,
3737
ref,
3838
) => {
39-
const { prefixCls, id, open, multiple, searchValue, toggleOpen, notFoundContent, onPopupScroll } =
40-
useBaseProps();
39+
const {
40+
prefixCls,
41+
id,
42+
open,
43+
multiple,
44+
mode,
45+
searchValue,
46+
toggleOpen,
47+
notFoundContent,
48+
onPopupScroll,
49+
} = useBaseProps();
4150
const {
4251
flattenOptions,
4352
onActiveValue,
@@ -108,6 +117,12 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, OptionListP
108117
setActive(defaultActiveFirstOption !== false ? getEnabledActiveIndex(0) : -1);
109118
}, [memoFlattenOptions.length, searchValue]);
110119

120+
// https://github.com/ant-design/ant-design/issues/34975
121+
const isSelected = React.useCallback(
122+
(value: RawValueType) => rawValues.has(value) && mode !== 'combobox',
123+
[mode, [...rawValues].toString()],
124+
);
125+
111126
// Auto scroll to item position in single mode
112127
useEffect(() => {
113128
/**
@@ -246,7 +261,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, OptionListP
246261
key={index}
247262
role={group ? 'presentation' : 'option'}
248263
id={`${id}_list_${index}`}
249-
aria-selected={rawValues.has(value)}
264+
aria-selected={isSelected(value)}
250265
>
251266
{value}
252267
</div>
@@ -293,7 +308,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, OptionListP
293308
const passedProps = omit(otherProps, omitFieldNameList);
294309

295310
// Option
296-
const selected = rawValues.has(value);
311+
const selected = isSelected(value);
297312

298313
const optionPrefixCls = `${itemPrefixCls}-option`;
299314
const optionClassName = classNames(itemPrefixCls, optionPrefixCls, className, {

tests/Combobox.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,4 +507,18 @@ describe('Select.Combobox', () => {
507507

508508
expect(wrapper.find('List').prop('data')).toHaveLength(2);
509509
});
510+
511+
// https://github.com/ant-design/ant-design/issues/34975
512+
it('should not contain selected className in combobox mode', () => {
513+
const onChange = jest.fn();
514+
const wrapper = mount(
515+
<Select mode="combobox" onChange={onChange}>
516+
<Option value="One">One</Option>
517+
<Option value="Two">Two</Option>
518+
</Select>,
519+
);
520+
toggleOpen(wrapper);
521+
selectItem(wrapper);
522+
expect(wrapper.find('.rc-select-item-option-selected').length).toBe(0);
523+
});
510524
});

0 commit comments

Comments
 (0)