Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(autocomplete): hideOnOptionSelect #5665

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api-generator/components/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ const AutoCompleteProps = [
default: 'false',
description: 'When enabled, the focused option is selected.'
},
{
name: 'hideOnOptionSelect',
type: 'boolean',
default: 'true',
description: 'Whether to hide the overlay when an option is selected.'
},
{
name: 'searchLocale',
type: 'string',
Expand Down
5 changes: 5 additions & 0 deletions components/lib/autocomplete/AutoComplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@ export interface AutoCompleteProps {
* @defaultValue false
*/
selectOnFocus?: boolean | undefined;
/**
* Whether to hide the overlay when an option is selected.
* @defaultValue true
*/
hideOnOptionSelect?: boolean | undefined;
/**
* When enabled, the focus is placed on the hovered option.
* @defaultValue true
Expand Down
49 changes: 49 additions & 0 deletions components/lib/autocomplete/AutoComplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { mount } from '@vue/test-utils';
import PrimeVue from 'primevue/config';
import { nextTick } from 'vue';
import AutoComplete from './AutoComplete.vue';
import { describe } from 'vitest';

describe('AutoComplete.vue', () => {
let wrapper;
Expand Down Expand Up @@ -100,4 +101,52 @@ describe('AutoComplete.vue', () => {
});
});
});

describe('hideOnOptionSelect', () => {
it('should hide overlay when hideOnOptionSelect is true', async () => {
await wrapper.setProps({
hideOnOptionSelect: true
});

const input = await wrapper.find('input');

await input.setValue('b');

// @NOTE: Without this delay, the test fails. vm.$nextTick() doesn't help.
await new Promise((resolve) => setTimeout(resolve, 300));

await wrapper.setProps({
suggestions: [{ name: 'Bahrain', code: 'BH' }]
});

await wrapper.find('.p-autocomplete-item').trigger('click');

await new Promise((resolve) => setTimeout(resolve, 5));

expect(wrapper.find('.p-autocomplete-panel').exists()).toBe(false);
});

it('should keep overlay open when hideOnOptionSelect is false', async () => {
await wrapper.setProps({
hideOnOptionSelect: false
});

const input = await wrapper.find('input');

await input.setValue('b');

// @NOTE: Without this delay, the test fails. vm.$nextTick() doesn't help.
await new Promise((resolve) => setTimeout(resolve, 300));

await wrapper.setProps({
suggestions: [{ name: 'Bahrain', code: 'BH' }]
});

await wrapper.find('.p-autocomplete-item').trigger('click');

await new Promise((resolve) => setTimeout(resolve, 5));

expect(wrapper.find('.p-autocomplete-panel').exists()).toBe(true);
});
});
});
8 changes: 6 additions & 2 deletions components/lib/autocomplete/AutoComplete.vue
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,9 @@ export default {

this.$emit('item-select', { originalEvent: event, value: option });

isHide && this.hide(true);
if (this.hideOnOptionSelect) {
isHide && this.hide(true);
}
},
onOptionMouseMove(event, index) {
if (this.focusOnHover) {
Expand Down Expand Up @@ -623,7 +625,9 @@ export default {
this.onOptionSelect(event, this.visibleOptions[this.focusedOptionIndex]);
}

this.hide();
if (this.hideOnOptionSelect) {
this.hide();
}
}
},
onEscapeKey(event) {
Expand Down
4 changes: 4 additions & 0 deletions components/lib/autocomplete/BaseAutoComplete.vue
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ export default {
type: Boolean,
default: false
},
hideOnOptionSelect: {
type: Boolean,
default: true
},
focusOnHover: {
type: Boolean,
default: true
Expand Down
8 changes: 8 additions & 0 deletions doc/common/apidoc/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -4184,6 +4184,14 @@
"default": "false",
"description": "When enabled, the focused option is selected."
},
{
"name": "hideOnOptionSelect",
"optional": true,
"readonly": false,
"type": "boolean",
"default": "true",
"description": "Whether to hide the overlay when an option is selected."
},
{
"name": "focusOnHover",
"optional": true,
Expand Down