Skip to content

Commit a61a0f4

Browse files
authored
fix: can't select when option has same value (#33)
* fix: can't select when option has same value * chore: add test
1 parent 12a4fd4 commit a61a0f4

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

src/DropdownMenu.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ class DropdownMenu extends React.Component<DropdownMenuProps, {}> {
2727
return (
2828
<Menu
2929
prefixCls={`${prefixCls}-menu`}
30-
activeKey={activeOption.value}
30+
activeKey={activeOption.key}
3131
onSelect={({ key }) => {
32-
const option = options.find(({ value }) => value === key);
32+
const option = options.find(({ key: optionKey }) => optionKey === key);
3333
selectOption(option);
3434
}}
3535
onFocus={onFocus}
3636
onBlur={onBlur}
3737
>
3838
{options.map((option, index) => {
39-
const { value, disabled, children, className, style } = option;
39+
const { key, disabled, children, className, style } = option;
4040
return (
4141
<MenuItem
42-
key={value}
42+
key={key}
4343
disabled={disabled}
4444
className={className}
4545
style={style}

src/Mentions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ class Mentions extends React.Component<MentionsProps, MentionsState> {
298298
const targetMeasureText = measureText || this.state.measureText || '';
299299
const { children, filterOption } = this.props;
300300
const list = toArray(children)
301-
.map(({ props }: { props: OptionProps }) => props)
301+
.map(({ props, key }: { props: OptionProps, key: string }) => ({ ...props, key: key || props.value }))
302302
.filter((option: OptionProps) => {
303303
/** Return all result if `filterOption` is false. */
304304
if (filterOption === false) {

src/Option.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22

33
export interface OptionProps {
44
value?: string;
5+
key?: string;
56
disabled?: boolean;
67
children?: React.ReactNode;
78
className?: string;

tests/FullProcess.spec.jsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ describe('Full Process', () => {
115115
expect(onPressEnter).toHaveBeenCalled();
116116
});
117117

118+
it('should support same value', () => {
119+
const wrapper = mount(
120+
<Mentions>
121+
<Option value="bamboo">Bamboo</Option>
122+
<Option value="bamboo" key="same_bamboo">
123+
Bamboo
124+
</Option>
125+
<Option value="light">Light</Option>
126+
<Option value="cat">Cat</Option>
127+
</Mentions>,
128+
);
129+
130+
simulateInput(wrapper, '@');
131+
expect(wrapper.find('.rc-mentions-dropdown-menu-item-active').length).toBe(1);
132+
});
133+
118134
it('ESC', () => {
119135
const wrapper = createMentions();
120136

0 commit comments

Comments
 (0)