primevue-mirror/components/lib/autocomplete/AutoComplete.vue

986 lines
39 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2024-02-11 08:10:29 +00:00
<div ref="container" :class="cx('root')" :style="sx('root')" @click="onContainerClick" v-bind="ptmi('root')">
2024-03-19 14:14:13 +00:00
<InputText
2022-09-14 11:26:01 +00:00
v-if="!multiple"
ref="focusInput"
:id="inputId"
type="text"
:class="[cx('pcInput'), inputClass]"
2022-09-14 11:26:01 +00:00
:style="inputStyle"
:value="inputValue"
:placeholder="placeholder"
:tabindex="!disabled ? tabindex : -1"
:disabled="disabled"
2024-03-18 10:01:24 +00:00
:invalid="invalid"
:variant="variant"
2022-09-14 11:26:01 +00:00
autocomplete="off"
role="combobox"
:aria-label="ariaLabel"
:aria-labelledby="ariaLabelledby"
aria-haspopup="listbox"
aria-autocomplete="list"
:aria-expanded="overlayVisible"
:aria-controls="panelId"
2022-09-14 11:26:01 +00:00
:aria-activedescendant="focused ? focusedOptionId : undefined"
@focus="onFocus"
@blur="onBlur"
@keydown="onKeyDown"
@input="onInput"
@change="onChange"
2024-03-18 10:01:24 +00:00
:unstyled="unstyled"
:pt="ptm('pcInput')"
2022-09-14 11:26:01 +00:00
/>
<ul
v-if="multiple"
ref="multiContainer"
:class="cx('inputMultiple')"
2022-09-14 11:26:01 +00:00
tabindex="-1"
role="listbox"
aria-orientation="horizontal"
:aria-activedescendant="focused ? focusedMultipleOptionId : undefined"
@focus="onMultipleContainerFocus"
@blur="onMultipleContainerBlur"
@keydown="onMultipleContainerKeyDown"
v-bind="ptm('inputMultiple')"
2022-09-14 11:26:01 +00:00
>
<li
v-for="(option, i) of modelValue"
2024-04-08 12:20:42 +00:00
:key="`${i}_${getOptionLabel(option)}`"
2022-09-14 11:26:01 +00:00
:id="id + '_multiple_option_' + i"
2024-05-15 07:35:36 +00:00
:class="cx('chipItem', { i })"
2022-09-14 11:26:01 +00:00
role="option"
:aria-label="getOptionLabel(option)"
:aria-selected="true"
:aria-setsize="modelValue.length"
:aria-posinset="i + 1"
2024-05-15 07:35:36 +00:00
v-bind="ptm('chipItem')"
2022-09-14 11:26:01 +00:00
>
2024-05-15 07:35:36 +00:00
<slot name="chip" :class="cx('pcChip')" :value="option" :index="i" :removeCallback="(event) => removeOption(event, i)">
<!-- TODO: removetokenicon and removeTokenIcon deprecated since v4.0. Use chipicon slot and chipIcon prop-->
2024-05-15 07:35:36 +00:00
<Chip :class="cx('pcChip')" :label="getOptionLabel(option)" :removeIcon="chipIcon || removeTokenIcon" removable :unstyled="unstyled" @remove="removeOption($event, i)" :pt="ptm('pcChip')">
2024-04-08 12:20:42 +00:00
<template #removeicon>
2024-05-06 10:52:41 +00:00
<slot :name="$slots.chipicon ? 'chipicon' : 'removetokenicon'" :class="cx('chipIcon')" :index="i" :removeCallback="(event) => removeOption(event, i)" />
2024-04-08 12:20:42 +00:00
</template>
</Chip>
</slot>
2022-09-06 12:03:37 +00:00
</li>
<li :class="cx('inputChip')" role="option" v-bind="ptm('inputChip')">
2024-04-08 12:58:38 +00:00
<input
2022-09-14 11:26:01 +00:00
ref="focusInput"
:id="inputId"
2024-04-08 12:58:38 +00:00
type="text"
2022-09-14 11:26:01 +00:00
:style="inputStyle"
:class="inputClass"
:placeholder="placeholder"
:tabindex="!disabled ? tabindex : -1"
:disabled="disabled"
autocomplete="off"
role="combobox"
:aria-label="ariaLabel"
:aria-labelledby="ariaLabelledby"
aria-haspopup="listbox"
aria-autocomplete="list"
:aria-expanded="overlayVisible"
:aria-controls="id + '_list'"
:aria-activedescendant="focused ? focusedOptionId : undefined"
2024-04-08 12:58:38 +00:00
:aria-invalid="invalid || undefined"
2022-09-14 11:26:01 +00:00
@focus="onFocus"
@blur="onBlur"
@keydown="onKeyDown"
@input="onInput"
@change="onChange"
2024-04-08 12:58:38 +00:00
v-bind="ptm('input')"
2022-09-14 11:26:01 +00:00
/>
2022-09-06 12:03:37 +00:00
</li>
</ul>
<slot v-if="searching || loading" :class="cx('loader')" :name="$slots.loader ? 'loader' : 'loadingicon'">
<i v-if="loader || loadingIcon" :class="['pi-spin', cx('loader'), loader, loadingIcon]" aria-hidden="true" v-bind="ptm('loader')" />
<SpinnerIcon v-else :class="cx('loader')" spin aria-hidden="true" v-bind="ptm('loader')" />
</slot>
<slot :name="$slots.dropdown ? 'dropdown' : 'dropdownbutton'" :toggleCallback="(event) => onDropdownClick(event)">
<button
v-if="dropdown"
ref="dropdownButton"
type="button"
:class="[cx('dropdown'), dropdownClass]"
:disabled="disabled"
aria-haspopup="listbox"
:aria-expanded="overlayVisible"
:aria-controls="panelId"
@click="onDropdownClick"
v-bind="ptm('dropdown')"
>
<slot name="dropdownicon" :class="dropdownIcon">
<component :is="dropdownIcon ? 'span' : 'ChevronDownIcon'" :class="dropdownIcon" v-bind="ptm('dropdownIcon')" />
</slot>
2024-03-18 10:01:24 +00:00
</button>
</slot>
<span role="status" aria-live="polite" class="p-hidden-accessible" v-bind="ptm('hiddenSearchResult')" :data-p-hidden-accessible="true">
2022-09-14 11:26:01 +00:00
{{ searchResultMessageText }}
2022-09-06 12:03:37 +00:00
</span>
<Portal :appendTo="appendTo">
2023-08-02 11:59:40 +00:00
<transition name="p-connected-overlay" @enter="onOverlayEnter" @after-enter="onOverlayAfterEnter" @leave="onOverlayLeave" @after-leave="onOverlayAfterLeave" v-bind="ptm('transition')">
2023-05-05 06:40:09 +00:00
<div
v-if="overlayVisible"
:ref="overlayRef"
:id="panelId"
2024-05-10 11:24:01 +00:00
:class="[cx('overlay'), panelClass, overlayClass]"
:style="{ ...panelStyle, ...overlayStyle, 'max-height': virtualScrollerDisabled ? scrollHeight : '' }"
2023-05-05 06:40:09 +00:00
@click="onOverlayClick"
@keydown="onOverlayKeyDown"
2024-05-10 11:24:01 +00:00
v-bind="ptm('overlay')"
2023-05-05 06:40:09 +00:00
>
2022-09-06 12:03:37 +00:00
<slot name="header" :value="modelValue" :suggestions="visibleOptions"></slot>
2023-05-25 14:02:41 +00:00
<VirtualScroller :ref="virtualScrollerRef" v-bind="virtualScrollerOptions" :style="{ height: scrollHeight }" :items="visibleOptions" :tabindex="-1" :disabled="virtualScrollerDisabled" :pt="ptm('virtualScroller')">
2022-09-06 12:03:37 +00:00
<template v-slot:content="{ styleClass, contentRef, items, getItemOptions, contentStyle, itemSize }">
<ul :ref="(el) => listRef(el, contentRef)" :id="id + '_list'" :class="[cx('list'), styleClass]" :style="contentStyle" role="listbox" :aria-label="listAriaLabel" v-bind="ptm('list')">
2022-09-06 12:03:37 +00:00
<template v-for="(option, i) of items" :key="getOptionRenderKey(option, getOptionIndex(i, getItemOptions))">
<li v-if="isOptionGroup(option)" :id="id + '_' + getOptionIndex(i, getItemOptions)" :style="{ height: itemSize ? itemSize + 'px' : undefined }" :class="cx('optionGroup')" role="option" v-bind="ptm('optionGroup')">
<slot name="optiongroup" :option="option.optionGroup" :index="getOptionIndex(i, getItemOptions)">{{ getOptionGroupLabel(option.optionGroup) }}</slot>
2022-09-06 12:03:37 +00:00
</li>
2022-09-14 11:26:01 +00:00
<li
v-else
:id="id + '_' + getOptionIndex(i, getItemOptions)"
v-ripple
:style="{ height: itemSize ? itemSize + 'px' : undefined }"
:class="cx('option', { option, i, getItemOptions })"
2022-09-14 11:26:01 +00:00
role="option"
:aria-label="getOptionLabel(option)"
:aria-selected="isSelected(option)"
:aria-disabled="isOptionDisabled(option)"
:aria-setsize="ariaSetSize"
:aria-posinset="getAriaPosInset(getOptionIndex(i, getItemOptions))"
@click="onOptionSelect($event, option)"
@mousemove="onOptionMouseMove($event, getOptionIndex(i, getItemOptions))"
:data-p-selected="isSelected(option)"
:data-p-focus="focusedOptionIndex === getOptionIndex(i, getItemOptions)"
2023-05-23 12:44:11 +00:00
:data-p-disabled="isOptionDisabled(option)"
v-bind="getPTOptions(option, getItemOptions, i, 'option')"
2022-09-14 11:26:01 +00:00
>
<slot v-if="$slots.option" name="option" :option="option" :index="getOptionIndex(i, getItemOptions)">{{ getOptionLabel(option) }}</slot>
2022-09-06 12:03:37 +00:00
</li>
</template>
<li v-if="!items || (items && items.length === 0)" :class="cx('emptyMessage')" role="option" v-bind="ptm('emptyMessage')">
<slot name="empty">{{ searchResultMessageText }}</slot>
</li>
2022-09-06 12:03:37 +00:00
</ul>
</template>
<template v-if="$slots.loader" v-slot:loader="{ options }">
<slot name="loader" :options="options"></slot>
</template>
</VirtualScroller>
<slot name="footer" :value="modelValue" :suggestions="visibleOptions"></slot>
<span role="status" aria-live="polite" class="p-hidden-accessible" v-bind="ptm('hiddenSelectedMessage')" :data-p-hidden-accessible="true">
2022-12-08 11:04:25 +00:00
{{ selectedMessageText }}
</span>
2022-09-06 12:03:37 +00:00
</div>
</transition>
</Portal>
</div>
</template>
<script>
2024-04-08 12:20:42 +00:00
import Chip from 'primevue/chip';
import ChevronDownIcon from 'primevue/icons/chevrondown';
import SpinnerIcon from 'primevue/icons/spinner';
2024-03-18 10:01:24 +00:00
import InputText from 'primevue/inputtext';
2022-12-08 11:04:25 +00:00
import OverlayEventBus from 'primevue/overlayeventbus';
import Portal from 'primevue/portal';
2022-09-06 12:03:37 +00:00
import Ripple from 'primevue/ripple';
2022-12-08 11:04:25 +00:00
import { ConnectedOverlayScrollHandler, DomHandler, ObjectUtils, UniqueComponentId, ZIndexUtils } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
import VirtualScroller from 'primevue/virtualscroller';
2023-05-23 12:44:11 +00:00
import BaseAutoComplete from './BaseAutoComplete.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'AutoComplete',
2023-05-23 12:44:11 +00:00
extends: BaseAutoComplete,
2024-02-11 08:10:29 +00:00
inheritAttrs: false,
2024-05-10 11:50:11 +00:00
emits: ['update:modelValue', 'change', 'focus', 'blur', 'item-select', 'item-unselect', 'option-select', 'option-unselect', 'dropdown-click', 'clear', 'complete', 'before-show', 'before-hide', 'show', 'hide'],
2022-09-06 12:03:37 +00:00
outsideClickListener: null,
resizeListener: null,
scrollHandler: null,
overlay: null,
virtualScroller: null,
searchTimeout: null,
dirty: false,
data() {
return {
id: this.$attrs.id,
clicked: false,
2022-09-06 12:03:37 +00:00
focused: false,
focusedOptionIndex: -1,
focusedMultipleOptionIndex: -1,
overlayVisible: false,
2024-04-08 12:58:38 +00:00
searching: false
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
watch: {
2024-04-16 09:35:02 +00:00
'$attrs.id': function (newValue) {
this.id = newValue || UniqueComponentId();
},
2022-09-06 12:03:37 +00:00
suggestions() {
if (this.searching) {
this.show();
2022-09-06 12:03:37 +00:00
this.focusedOptionIndex = this.overlayVisible && this.autoOptionFocus ? this.findFirstFocusedOptionIndex() : -1;
this.searching = false;
}
this.autoUpdateModel();
}
},
mounted() {
2024-04-16 09:35:02 +00:00
this.id = this.id || UniqueComponentId();
2022-09-06 12:03:37 +00:00
this.autoUpdateModel();
},
updated() {
if (this.overlayVisible) {
this.alignOverlay();
}
},
beforeUnmount() {
this.unbindOutsideClickListener();
this.unbindResizeListener();
if (this.scrollHandler) {
this.scrollHandler.destroy();
this.scrollHandler = null;
}
if (this.overlay) {
ZIndexUtils.clear(this.overlay);
this.overlay = null;
}
},
methods: {
getOptionIndex(index, fn) {
2022-09-14 11:26:01 +00:00
return this.virtualScrollerDisabled ? index : fn && fn(index)['index'];
2022-09-06 12:03:37 +00:00
},
getOptionLabel(option) {
return this.optionLabel ? ObjectUtils.resolveFieldData(option, this.optionLabel) : option;
2022-09-06 12:03:37 +00:00
},
getOptionValue(option) {
return option; // TODO: The 'optionValue' properties can be added.
},
getOptionRenderKey(option, index) {
return (this.dataKey ? ObjectUtils.resolveFieldData(option, this.dataKey) : this.getOptionLabel(option)) + '_' + index;
},
2023-05-08 07:12:02 +00:00
getPTOptions(option, itemOptions, index, key) {
return this.ptm(key, {
context: {
selected: this.isSelected(option),
focused: this.focusedOptionIndex === this.getOptionIndex(index, itemOptions),
disabled: this.isOptionDisabled(option)
}
});
},
2022-09-06 12:03:37 +00:00
isOptionDisabled(option) {
return this.optionDisabled ? ObjectUtils.resolveFieldData(option, this.optionDisabled) : false;
},
isOptionGroup(option) {
return this.optionGroupLabel && option.optionGroup && option.group;
},
getOptionGroupLabel(optionGroup) {
return ObjectUtils.resolveFieldData(optionGroup, this.optionGroupLabel);
},
getOptionGroupChildren(optionGroup) {
return ObjectUtils.resolveFieldData(optionGroup, this.optionGroupChildren);
},
getAriaPosInset(index) {
2022-09-14 11:26:01 +00:00
return (this.optionGroupLabel ? index - this.visibleOptions.slice(0, index).filter((option) => this.isOptionGroup(option)).length : index) + 1;
2022-09-06 12:03:37 +00:00
},
show(isFocus) {
this.$emit('before-show');
this.dirty = true;
this.overlayVisible = true;
2022-09-14 11:26:01 +00:00
this.focusedOptionIndex = this.focusedOptionIndex !== -1 ? this.focusedOptionIndex : this.autoOptionFocus ? this.findFirstFocusedOptionIndex() : -1;
2024-04-08 12:58:38 +00:00
isFocus && DomHandler.focus(this.multiple ? this.$refs.focusInput : this.$refs.focusInput.$el);
2022-09-06 12:03:37 +00:00
},
hide(isFocus) {
const _hide = () => {
this.$emit('before-hide');
this.dirty = isFocus;
this.overlayVisible = false;
this.clicked = false;
2022-09-06 12:03:37 +00:00
this.focusedOptionIndex = -1;
2024-04-08 12:58:38 +00:00
isFocus && DomHandler.focus(this.multiple ? this.$refs.focusInput : this.$refs.focusInput.$el);
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
setTimeout(() => {
_hide();
}, 0); // For ScreenReaders
2022-09-06 12:03:37 +00:00
},
onFocus(event) {
if (this.disabled) {
// For ScreenReaders
return;
}
2022-09-06 12:03:37 +00:00
if (!this.dirty && this.completeOnFocus) {
this.search(event, event.target.value, 'focus');
}
this.dirty = true;
this.focused = true;
if (this.overlayVisible) {
this.focusedOptionIndex = this.focusedOptionIndex !== -1 ? this.focusedOptionIndex : this.overlayVisible && this.autoOptionFocus ? this.findFirstFocusedOptionIndex() : -1;
this.scrollInView(this.focusedOptionIndex);
}
2022-09-06 12:03:37 +00:00
this.$emit('focus', event);
},
onBlur(event) {
this.dirty = false;
this.focused = false;
this.focusedOptionIndex = -1;
this.$emit('blur', event);
},
onKeyDown(event) {
if (this.disabled) {
event.preventDefault();
return;
}
2022-09-06 12:03:37 +00:00
switch (event.code) {
case 'ArrowDown':
this.onArrowDownKey(event);
break;
case 'ArrowUp':
this.onArrowUpKey(event);
break;
case 'ArrowLeft':
this.onArrowLeftKey(event);
break;
case 'ArrowRight':
this.onArrowRightKey(event);
break;
case 'Home':
this.onHomeKey(event);
break;
case 'End':
this.onEndKey(event);
break;
case 'PageDown':
this.onPageDownKey(event);
break;
case 'PageUp':
this.onPageUpKey(event);
break;
case 'Enter':
case 'NumpadEnter':
2022-09-06 12:03:37 +00:00
this.onEnterKey(event);
break;
case 'Escape':
this.onEscapeKey(event);
break;
case 'Tab':
this.onTabKey(event);
break;
case 'Backspace':
this.onBackspaceKey(event);
break;
case 'ShiftLeft':
case 'ShiftRight':
//NOOP
break;
default:
break;
}
this.clicked = false;
2022-09-06 12:03:37 +00:00
},
onInput(event) {
2024-05-15 10:47:01 +00:00
if (this.typeahead) {
if (this.searchTimeout) {
clearTimeout(this.searchTimeout);
}
2022-09-06 12:03:37 +00:00
2024-05-15 10:47:01 +00:00
let query = event.target.value;
2022-09-14 11:26:01 +00:00
2024-05-15 10:47:01 +00:00
if (!this.multiple) {
this.updateModel(event, query);
}
2022-09-06 12:03:37 +00:00
2024-05-15 10:47:01 +00:00
if (query.length === 0) {
2022-09-06 12:03:37 +00:00
this.hide();
2024-05-15 10:47:01 +00:00
this.$emit('clear');
} else {
if (query.length >= this.minLength) {
this.focusedOptionIndex = -1;
this.searchTimeout = setTimeout(() => {
this.search(event, query, 'input');
}, this.delay);
} else {
this.hide();
}
2022-09-06 12:03:37 +00:00
}
}
},
onChange(event) {
if (this.forceSelection) {
let valid = false;
// when forceSelection is on, prevent called twice onOptionSelect()
if (this.visibleOptions && !this.multiple) {
2024-04-08 12:58:38 +00:00
let value = this.multiple ? this.$refs.focusInput.value : this.$refs.focusInput.$el.value;
const matchedValue = this.visibleOptions.find((option) => this.isOptionMatched(option, value || ''));
2022-09-06 12:03:37 +00:00
if (matchedValue !== undefined) {
valid = true;
!this.isSelected(matchedValue) && this.onOptionSelect(event, matchedValue);
}
}
if (!valid) {
2024-04-08 12:58:38 +00:00
if (this.multiple) this.$refs.focusInput.value = '';
else this.$refs.focusInput.$el.value = '';
2022-09-06 12:03:37 +00:00
this.$emit('clear');
!this.multiple && this.updateModel(event, null);
}
}
},
onMultipleContainerFocus() {
if (this.disabled) {
// For ScreenReaders
return;
}
2022-09-06 12:03:37 +00:00
this.focused = true;
},
onMultipleContainerBlur() {
this.focusedMultipleOptionIndex = -1;
this.focused = false;
},
onMultipleContainerKeyDown(event) {
if (this.disabled) {
event.preventDefault();
return;
}
2022-09-06 12:03:37 +00:00
switch (event.code) {
case 'ArrowLeft':
this.onArrowLeftKeyOnMultiple(event);
break;
case 'ArrowRight':
this.onArrowRightKeyOnMultiple(event);
break;
case 'Backspace':
this.onBackspaceKeyOnMultiple(event);
break;
default:
break;
}
},
onContainerClick(event) {
this.clicked = true;
if (this.disabled || this.searching || this.loading || this.isInputClicked(event) || this.isDropdownClicked(event)) {
2022-09-06 12:03:37 +00:00
return;
}
if (!this.overlay || !this.overlay.contains(event.target)) {
2024-04-08 12:58:38 +00:00
DomHandler.focus(this.multiple ? this.$refs.focusInput : this.$refs.focusInput.$el);
2022-09-06 12:03:37 +00:00
}
},
onDropdownClick(event) {
let query = undefined;
if (this.overlayVisible) {
this.hide(true);
2022-09-14 11:26:01 +00:00
} else {
2024-04-08 12:58:38 +00:00
DomHandler.focus(this.multiple ? this.$refs.focusInput : this.$refs.focusInput.$el);
2024-03-18 10:01:24 +00:00
query = this.$refs.focusInput.$el.value;
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
if (this.dropdownMode === 'blank') this.search(event, '', 'dropdown');
else if (this.dropdownMode === 'current') this.search(event, query, 'dropdown');
2022-09-06 12:03:37 +00:00
}
this.$emit('dropdown-click', { originalEvent: event, query });
},
2022-09-14 11:26:01 +00:00
onOptionSelect(event, option, isHide = true) {
2022-09-06 12:03:37 +00:00
const value = this.getOptionValue(option);
if (this.multiple) {
2024-04-08 12:58:38 +00:00
this.$refs.focusInput.value = '';
2022-09-06 12:03:37 +00:00
if (!this.isSelected(option)) {
this.updateModel(event, [...(this.modelValue || []), value]);
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
this.updateModel(event, value);
}
this.$emit('item-select', { originalEvent: event, value: option });
2024-05-10 11:50:11 +00:00
this.$emit('option-select', { originalEvent: event, value: option });
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
isHide && this.hide(true);
2022-09-06 12:03:37 +00:00
},
onOptionMouseMove(event, index) {
if (this.focusOnHover) {
this.changeFocusedOptionIndex(event, index);
}
},
onOverlayClick(event) {
OverlayEventBus.emit('overlay-click', {
originalEvent: event,
target: this.$el
});
},
onOverlayKeyDown(event) {
switch (event.code) {
case 'Escape':
this.onEscapeKey(event);
break;
default:
break;
}
},
onArrowDownKey(event) {
if (!this.overlayVisible) {
return;
}
const optionIndex = this.focusedOptionIndex !== -1 ? this.findNextOptionIndex(this.focusedOptionIndex) : this.clicked ? this.findFirstOptionIndex() : this.findFirstFocusedOptionIndex();
2022-09-06 12:03:37 +00:00
this.changeFocusedOptionIndex(event, optionIndex);
event.preventDefault();
},
onArrowUpKey(event) {
if (!this.overlayVisible) {
return;
}
if (event.altKey) {
if (this.focusedOptionIndex !== -1) {
this.onOptionSelect(event, this.visibleOptions[this.focusedOptionIndex]);
}
this.overlayVisible && this.hide();
event.preventDefault();
2022-09-14 11:26:01 +00:00
} else {
const optionIndex = this.focusedOptionIndex !== -1 ? this.findPrevOptionIndex(this.focusedOptionIndex) : this.clicked ? this.findLastOptionIndex() : this.findLastFocusedOptionIndex();
2022-09-06 12:03:37 +00:00
this.changeFocusedOptionIndex(event, optionIndex);
event.preventDefault();
}
},
onArrowLeftKey(event) {
const target = event.currentTarget;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.focusedOptionIndex = -1;
if (this.multiple) {
if (ObjectUtils.isEmpty(target.value) && this.hasSelectedOption) {
2022-09-14 11:26:01 +00:00
DomHandler.focus(this.$refs.multiContainer);
2022-09-06 12:03:37 +00:00
this.focusedMultipleOptionIndex = this.modelValue.length;
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
event.stopPropagation(); // To prevent onArrowLeftKeyOnMultiple method
}
}
},
onArrowRightKey(event) {
this.focusedOptionIndex = -1;
this.multiple && event.stopPropagation(); // To prevent onArrowRightKeyOnMultiple method
},
onHomeKey(event) {
const { currentTarget } = event;
const len = currentTarget.value.length;
2022-12-08 11:04:25 +00:00
currentTarget.setSelectionRange(0, event.shiftKey ? len : 0);
2022-09-06 12:03:37 +00:00
this.focusedOptionIndex = -1;
event.preventDefault();
},
onEndKey(event) {
const { currentTarget } = event;
const len = currentTarget.value.length;
2022-12-08 11:04:25 +00:00
currentTarget.setSelectionRange(event.shiftKey ? 0 : len, len);
2022-09-06 12:03:37 +00:00
this.focusedOptionIndex = -1;
event.preventDefault();
},
onPageUpKey(event) {
this.scrollInView(0);
event.preventDefault();
},
onPageDownKey(event) {
this.scrollInView(this.visibleOptions.length - 1);
event.preventDefault();
},
onEnterKey(event) {
2024-05-15 10:47:01 +00:00
if (!this.typeahead) {
if (this.multiple) {
this.updateModel(event, [...(this.modelValue || []), event.target.value]);
this.$refs.focusInput.value = '';
2022-09-06 12:03:37 +00:00
}
2024-05-15 10:47:01 +00:00
} else {
if (!this.overlayVisible) {
this.focusedOptionIndex = -1; // reset
this.onArrowDownKey(event);
} else {
if (this.focusedOptionIndex !== -1) {
this.onOptionSelect(event, this.visibleOptions[this.focusedOptionIndex]);
}
2022-09-06 12:03:37 +00:00
2024-05-15 10:47:01 +00:00
this.hide();
}
2022-09-06 12:03:37 +00:00
}
},
onEscapeKey(event) {
this.overlayVisible && this.hide(true);
event.preventDefault();
},
onTabKey(event) {
if (this.focusedOptionIndex !== -1) {
this.onOptionSelect(event, this.visibleOptions[this.focusedOptionIndex]);
}
this.overlayVisible && this.hide();
},
onBackspaceKey(event) {
if (this.multiple) {
2024-04-08 12:58:38 +00:00
if (ObjectUtils.isNotEmpty(this.modelValue) && !this.$refs.focusInput.value) {
2022-09-06 12:03:37 +00:00
const removedValue = this.modelValue[this.modelValue.length - 1];
const newValue = this.modelValue.slice(0, -1);
this.$emit('update:modelValue', newValue);
this.$emit('item-unselect', { originalEvent: event, value: removedValue });
2024-05-10 11:50:11 +00:00
this.$emit('option-unselect', { originalEvent: event, value: removedValue });
2022-09-06 12:03:37 +00:00
}
event.stopPropagation(); // To prevent onBackspaceKeyOnMultiple method
}
},
onArrowLeftKeyOnMultiple() {
this.focusedMultipleOptionIndex = this.focusedMultipleOptionIndex < 1 ? 0 : this.focusedMultipleOptionIndex - 1;
},
onArrowRightKeyOnMultiple() {
this.focusedMultipleOptionIndex++;
2022-09-14 11:26:01 +00:00
if (this.focusedMultipleOptionIndex > this.modelValue.length - 1) {
2022-09-06 12:03:37 +00:00
this.focusedMultipleOptionIndex = -1;
2024-04-08 12:58:38 +00:00
DomHandler.focus(this.$refs.focusInput);
2022-09-06 12:03:37 +00:00
}
},
onBackspaceKeyOnMultiple(event) {
if (this.focusedMultipleOptionIndex !== -1) {
this.removeOption(event, this.focusedMultipleOptionIndex);
}
},
onOverlayEnter(el) {
ZIndexUtils.set('overlay', el, this.$primevue.config.zIndex.overlay);
2023-05-23 12:44:11 +00:00
DomHandler.addStyles(el, { position: 'absolute', top: '0', left: '0' });
2022-09-06 12:03:37 +00:00
this.alignOverlay();
},
onOverlayAfterEnter() {
this.bindOutsideClickListener();
this.bindScrollListener();
this.bindResizeListener();
this.$emit('show');
},
onOverlayLeave() {
this.unbindOutsideClickListener();
this.unbindScrollListener();
this.unbindResizeListener();
this.$emit('hide');
this.overlay = null;
},
onOverlayAfterLeave(el) {
ZIndexUtils.clear(el);
},
alignOverlay() {
2024-03-18 10:01:24 +00:00
let target = this.multiple ? this.$refs.multiContainer : this.$refs.focusInput.$el;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (this.appendTo === 'self') {
DomHandler.relativePosition(this.overlay, target);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
this.overlay.style.minWidth = DomHandler.getOuterWidth(target) + 'px';
DomHandler.absolutePosition(this.overlay, target);
}
},
bindOutsideClickListener() {
if (!this.outsideClickListener) {
this.outsideClickListener = (event) => {
if (this.overlayVisible && this.overlay && this.isOutsideClicked(event)) {
this.hide();
}
};
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
document.addEventListener('click', this.outsideClickListener);
}
},
unbindOutsideClickListener() {
if (this.outsideClickListener) {
document.removeEventListener('click', this.outsideClickListener);
this.outsideClickListener = null;
}
},
bindScrollListener() {
if (!this.scrollHandler) {
this.scrollHandler = new ConnectedOverlayScrollHandler(this.$refs.container, () => {
if (this.overlayVisible) {
this.hide();
}
});
}
this.scrollHandler.bindScrollListener();
},
unbindScrollListener() {
if (this.scrollHandler) {
this.scrollHandler.unbindScrollListener();
}
},
bindResizeListener() {
if (!this.resizeListener) {
this.resizeListener = () => {
if (this.overlayVisible && !DomHandler.isTouchDevice()) {
this.hide();
}
};
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
window.addEventListener('resize', this.resizeListener);
}
},
unbindResizeListener() {
if (this.resizeListener) {
window.removeEventListener('resize', this.resizeListener);
this.resizeListener = null;
}
},
isOutsideClicked(event) {
return !this.overlay.contains(event.target) && !this.isInputClicked(event) && !this.isDropdownClicked(event);
},
isInputClicked(event) {
2022-09-14 11:26:01 +00:00
if (this.multiple) return event.target === this.$refs.multiContainer || this.$refs.multiContainer.contains(event.target);
2024-03-18 10:01:24 +00:00
else return event.target === this.$refs.focusInput.$el;
2022-09-06 12:03:37 +00:00
},
isDropdownClicked(event) {
2024-03-18 10:01:24 +00:00
return this.$refs.dropdownButton ? event.target === this.$refs.dropdownButton || this.$refs.dropdownButton.contains(event.target) : false;
2022-09-06 12:03:37 +00:00
},
isOptionMatched(option, value) {
return this.isValidOption(option) && this.getOptionLabel(option)?.toLocaleLowerCase(this.searchLocale) === value.toLocaleLowerCase(this.searchLocale);
2022-09-06 12:03:37 +00:00
},
isValidOption(option) {
return ObjectUtils.isNotEmpty(option) && !(this.isOptionDisabled(option) || this.isOptionGroup(option));
2022-09-06 12:03:37 +00:00
},
isValidSelectedOption(option) {
return this.isValidOption(option) && this.isSelected(option);
},
isEquals(value1, value2) {
return ObjectUtils.equals(value1, value2, this.equalityKey);
},
2022-09-06 12:03:37 +00:00
isSelected(option) {
const optionValue = this.getOptionValue(option);
2024-03-19 14:18:17 +00:00
return this.multiple ? (this.modelValue || []).some((value) => this.isEquals(value, optionValue)) : this.isEquals(this.modelValue, this.getOptionValue(option));
2022-09-06 12:03:37 +00:00
},
findFirstOptionIndex() {
2022-09-14 11:26:01 +00:00
return this.visibleOptions.findIndex((option) => this.isValidOption(option));
2022-09-06 12:03:37 +00:00
},
findLastOptionIndex() {
2022-09-14 11:26:01 +00:00
return ObjectUtils.findLastIndex(this.visibleOptions, (option) => this.isValidOption(option));
2022-09-06 12:03:37 +00:00
},
findNextOptionIndex(index) {
2022-09-14 11:26:01 +00:00
const matchedOptionIndex = index < this.visibleOptions.length - 1 ? this.visibleOptions.slice(index + 1).findIndex((option) => this.isValidOption(option)) : -1;
2022-09-06 12:03:37 +00:00
return matchedOptionIndex > -1 ? matchedOptionIndex + index + 1 : index;
},
findPrevOptionIndex(index) {
2022-09-14 11:26:01 +00:00
const matchedOptionIndex = index > 0 ? ObjectUtils.findLastIndex(this.visibleOptions.slice(0, index), (option) => this.isValidOption(option)) : -1;
2022-09-06 12:03:37 +00:00
return matchedOptionIndex > -1 ? matchedOptionIndex : index;
},
findSelectedOptionIndex() {
2022-09-14 11:26:01 +00:00
return this.hasSelectedOption ? this.visibleOptions.findIndex((option) => this.isValidSelectedOption(option)) : -1;
2022-09-06 12:03:37 +00:00
},
findFirstFocusedOptionIndex() {
const selectedIndex = this.findSelectedOptionIndex();
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return selectedIndex < 0 ? this.findFirstOptionIndex() : selectedIndex;
},
findLastFocusedOptionIndex() {
const selectedIndex = this.findSelectedOptionIndex();
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return selectedIndex < 0 ? this.findLastOptionIndex() : selectedIndex;
},
search(event, query, source) {
//allow empty string but not undefined or null
if (query === undefined || query === null) {
return;
}
//do not search blank values on input change
if (source === 'input' && query.trim().length === 0) {
return;
}
this.searching = true;
this.$emit('complete', { originalEvent: event, query });
},
removeOption(event, index) {
const removedOption = this.modelValue[index];
2022-09-14 11:26:01 +00:00
const value = this.modelValue.filter((_, i) => i !== index).map((option) => this.getOptionValue(option));
2022-09-06 12:03:37 +00:00
this.updateModel(event, value);
this.$emit('item-unselect', { originalEvent: event, value: removedOption });
2024-05-10 11:50:11 +00:00
this.$emit('option-unselect', { originalEvent: event, value: removedOption });
2022-09-06 12:03:37 +00:00
this.dirty = true;
2024-04-08 12:58:38 +00:00
DomHandler.focus(this.multiple ? this.$refs.focusInput : this.$refs.focusInput.$el);
2022-09-06 12:03:37 +00:00
},
changeFocusedOptionIndex(event, index) {
if (this.focusedOptionIndex !== index) {
this.focusedOptionIndex = index;
this.scrollInView();
if (this.selectOnFocus) {
2022-09-14 11:26:01 +00:00
this.onOptionSelect(event, this.visibleOptions[index], false);
2022-09-06 12:03:37 +00:00
}
}
},
scrollInView(index = -1) {
this.$nextTick(() => {
const id = index !== -1 ? `${this.id}_${index}` : this.focusedOptionId;
const element = DomHandler.findSingle(this.list, `li[id="${id}"]`);
2022-09-14 11:26:01 +00:00
if (element) {
element.scrollIntoView && element.scrollIntoView({ block: 'nearest', inline: 'start' });
} else if (!this.virtualScrollerDisabled) {
2022-09-06 12:03:37 +00:00
this.virtualScroller && this.virtualScroller.scrollToIndex(index !== -1 ? index : this.focusedOptionIndex);
}
});
2022-09-06 12:03:37 +00:00
},
autoUpdateModel() {
if (this.selectOnFocus && this.autoOptionFocus && !this.hasSelectedOption) {
2022-09-06 12:03:37 +00:00
this.focusedOptionIndex = this.findFirstFocusedOptionIndex();
2022-09-14 11:26:01 +00:00
this.onOptionSelect(null, this.visibleOptions[this.focusedOptionIndex], false);
2022-09-06 12:03:37 +00:00
}
},
updateModel(event, value) {
this.$emit('update:modelValue', value);
this.$emit('change', { originalEvent: event, value });
},
flatOptions(options) {
return (options || []).reduce((result, option, index) => {
result.push({ optionGroup: option, group: true, index });
const optionGroupChildren = this.getOptionGroupChildren(option);
2022-09-14 11:26:01 +00:00
optionGroupChildren && optionGroupChildren.forEach((o) => result.push(o));
2022-09-06 12:03:37 +00:00
return result;
}, []);
},
overlayRef(el) {
this.overlay = el;
},
listRef(el, contentRef) {
this.list = el;
contentRef && contentRef(el); // For VirtualScroller
},
virtualScrollerRef(el) {
this.virtualScroller = el;
}
},
computed: {
visibleOptions() {
2022-09-14 11:26:01 +00:00
return this.optionGroupLabel ? this.flatOptions(this.suggestions) : this.suggestions || [];
2022-09-06 12:03:37 +00:00
},
inputValue() {
if (ObjectUtils.isNotEmpty(this.modelValue)) {
2022-09-06 12:03:37 +00:00
if (typeof this.modelValue === 'object') {
const label = this.getOptionLabel(this.modelValue);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return label != null ? label : this.modelValue;
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
return this.modelValue;
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
return '';
}
},
hasSelectedOption() {
return ObjectUtils.isNotEmpty(this.modelValue);
},
equalityKey() {
return this.dataKey; // TODO: The 'optionValue' properties can be added.
},
searchResultMessageText() {
return ObjectUtils.isNotEmpty(this.visibleOptions) && this.overlayVisible ? this.searchMessageText.replaceAll('{0}', this.visibleOptions.length) : this.emptySearchMessageText;
},
searchMessageText() {
return this.searchMessage || this.$primevue.config.locale.searchMessage || '';
},
emptySearchMessageText() {
return this.emptySearchMessage || this.$primevue.config.locale.emptySearchMessage || '';
},
selectionMessageText() {
return this.selectionMessage || this.$primevue.config.locale.selectionMessage || '';
},
emptySelectionMessageText() {
return this.emptySelectionMessage || this.$primevue.config.locale.emptySelectionMessage || '';
},
selectedMessageText() {
return this.hasSelectedOption ? this.selectionMessageText.replaceAll('{0}', this.multiple ? this.modelValue.length : '1') : this.emptySelectionMessageText;
},
listAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.listLabel : undefined;
},
2022-09-06 12:03:37 +00:00
focusedOptionId() {
return this.focusedOptionIndex !== -1 ? `${this.id}_${this.focusedOptionIndex}` : null;
},
focusedMultipleOptionId() {
return this.focusedMultipleOptionIndex !== -1 ? `${this.id}_multiple_option_${this.focusedMultipleOptionIndex}` : null;
},
ariaSetSize() {
2022-09-14 11:26:01 +00:00
return this.visibleOptions.filter((option) => !this.isOptionGroup(option)).length;
2022-09-06 12:03:37 +00:00
},
virtualScrollerDisabled() {
return !this.virtualScrollerOptions;
},
panelId() {
return this.id + '_panel';
2022-09-06 12:03:37 +00:00
}
},
components: {
2024-03-19 14:14:13 +00:00
InputText,
VirtualScroller,
Portal,
ChevronDownIcon,
SpinnerIcon,
2024-04-08 12:20:42 +00:00
Chip
2022-09-06 12:03:37 +00:00
},
directives: {
2022-09-14 11:26:01 +00:00
ripple: Ripple
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>