Skip to content

Commit 011c839

Browse files
authored
fix: stop measure when pressing enter with empty result (#2662)
1 parent 4331a57 commit 011c839

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

components/mentions/__tests__/index.test.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { mount } from '@vue/test-utils';
22
import Vue from 'vue';
33
import Mentions from '..';
44
import focusTest from '../../../tests/shared/focusTest';
5+
import KeyCode from '../../_util/KeyCode';
56

6-
const { getMentions } = Mentions;
7+
const { getMentions, Option } = Mentions;
78

89
function $$(className) {
910
return document.body.querySelectorAll(className);
@@ -86,5 +87,28 @@ describe('Mentions', () => {
8687
});
8788
});
8889

90+
it('notExist', async () => {
91+
const wrapper = mount({
92+
render() {
93+
return (
94+
<Mentions>
95+
<Option value="bamboo">Bamboo</Option>
96+
<Option value="light">Light</Option>
97+
<Option value="cat">Cat</Option>
98+
</Mentions>
99+
);
100+
},
101+
});
102+
103+
triggerInput(wrapper, '@notExist');
104+
jest.runAllTimers();
105+
106+
wrapper.find('textarea').element.keyCode = KeyCode.ENTER;
107+
wrapper.find('textarea').trigger('keydown');
108+
jest.runAllTimers();
109+
110+
expect(wrapper.find('textarea').element.value).toBe('@notExist');
111+
});
112+
89113
focusTest(Mentions);
90114
});

components/vc-mentions/src/Mentions.jsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,14 @@ const Mentions = {
9797
this.stopMeasure();
9898
} else if (which === KeyCode.ENTER) {
9999
// Measure hit
100-
const option = this.getOptions()[activeIndex];
101-
this.selectOption(option);
102100
event.preventDefault();
101+
const options = this.getOptions();
102+
if (!options.length) {
103+
this.stopMeasure();
104+
return;
105+
}
106+
const option = options[activeIndex];
107+
this.selectOption(option);
103108
}
104109
},
105110
/**

0 commit comments

Comments
 (0)