Skip to content

Commit

Permalink
fix(AutoComplete): improve UX onEnter
Browse files Browse the repository at this point in the history
  • Loading branch information
lukicenturi authored and kelsos committed Jul 15, 2024
1 parent 97973fe commit f1461e8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
23 changes: 21 additions & 2 deletions src/components/forms/auto-complete/RuiAutoComplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,18 @@ describe('autocomplete', () => {
await wrapper.find('[data-id=activator]').trigger('click');
await vi.delay();
await nextTick();
expect(document.body.querySelector('div[role=menu]')).toBeTruthy();

// Close Menu Select
await wrapper.find('[data-id=activator]').trigger('keydown.esc');
await vi.delay();
await nextTick();
expect(document.body.querySelector('div[role=menu]')).toBeFalsy();

// Open Menu Select by Enter
await wrapper.find('[data-id=activator]').trigger('keydown.enter');
await vi.delay();
await nextTick();
expect(document.body.querySelector('div[role=menu]')).toBeTruthy();

const selectedIndex = 4;
Expand All @@ -100,6 +111,15 @@ describe('autocomplete', () => {
buttonToSelect?.click();
expect(wrapper.emitted().input!.at(-1)![0]).toBe(selectedIndex.toString());

await wrapper.setProps({
value: selectedIndex.toString(),
});

await vi.delay();
expect(document.body.querySelector('div[role=menu]')).toBeFalsy();

// Shouldn't open menu select because the value has been set
await wrapper.find('[data-id=activator]').trigger('keydown.enter');
await vi.delay();
expect(document.body.querySelector('div[role=menu]')).toBeFalsy();

Expand Down Expand Up @@ -137,11 +157,10 @@ describe('autocomplete', () => {

await wrapper.setProps({
options: newOptions,
value: newSelectedIndexToString,
});
await nextTick();

expect(wrapper.emitted().input!.at(-1)![0]).toEqual(null);

// Even if the options changed, the search value should not be touch as long as the focus still there, so the UX is not breaking
expect((wrapper.find('input').element as HTMLInputElement).value).toBe('Greece');

Expand Down
2 changes: 1 addition & 1 deletion src/components/forms/auto-complete/RuiAutoComplete.vue
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ function onEnter(event: KeyboardEvent) {
set(isOpen, false);
event.preventDefault();
}
else if (!get(isOpen)) {
else if (!get(isOpen) && get(value).length === 0) {
set(isOpen, true);
event.preventDefault();
}
Expand Down

0 comments on commit f1461e8

Please sign in to comment.