Skip to content

Commit 3a7e0e8

Browse files
authored
fix: should stop measure when pressing enter key with empty result (#30)
1 parent b73f140 commit 3a7e0e8

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

src/Mentions.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,14 @@ class Mentions extends React.Component<MentionsProps, MentionsState> {
142142
this.stopMeasure();
143143
} else if (which === KeyCode.ENTER) {
144144
// Measure hit
145-
const option = this.getOptions()[activeIndex];
146-
this.selectOption(option);
147145
event.preventDefault();
146+
const options = this.getOptions();
147+
if (!options.length) {
148+
this.stopMeasure();
149+
return
150+
}
151+
const option = options[activeIndex];
152+
this.selectOption(option);
148153
}
149154
};
150155

tests/Mentions.spec.jsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { mount } from 'enzyme';
22
import React from 'react';
3+
import KeyCode from 'rc-util/lib/KeyCode';
34
import Mentions from '../src';
45
import { simulateInput } from './shared/input';
56

@@ -73,9 +74,7 @@ describe('Mentions', () => {
7374
expect(document.activeElement).toBe(wrapper.find('textarea').instance());
7475

7576
wrapper.instance().blur();
76-
expect(document.activeElement).not.toBe(
77-
wrapper.find('textarea').instance(),
78-
);
77+
expect(document.activeElement).not.toBe(wrapper.find('textarea').instance());
7978
});
8079
});
8180

@@ -129,6 +128,13 @@ describe('Mentions', () => {
129128
simulateInput(wrapper, '@notExist');
130129
expect(wrapper.find('DropdownMenu').props().options.length).toBe(0);
131130
expect(wrapper.find('MenuItem').props().children).toBe(notFoundContent);
131+
// https://github.com/ant-design/ant-design/issues/26097
132+
wrapper.find('textarea').simulate('keyDown', {
133+
which: KeyCode.ENTER,
134+
});
135+
// stop measure
136+
expect(wrapper.find('DropdownMenu').exists()).toBe(false);
137+
expect(wrapper.find('textarea').prop('value')).toBe('@notExist');
132138
});
133139

134140
describe('accessibility', () => {

0 commit comments

Comments
 (0)