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

955 lines
38 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')">
2022-09-14 11:26:01 +00:00
<input
v-if="!multiple"
ref="focusInput"
:id="inputId"
type="text"
:class="[cx('input'), inputClass]"
2022-09-14 11:26:01 +00:00
:style="inputStyle"
:value="inputValue"
: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"
:aria-invalid="invalid || undefined"
2022-09-14 11:26:01 +00:00
@focus="onFocus"
@blur="onBlur"
@keydown="onKeyDown"
@input="onInput"
@change="onChange"
2023-05-05 06:40:09 +00:00
v-bind="{ ...inputProps, ...ptm('input') }"
2022-09-14 11:26:01 +00:00
/>
<ul
v-if="multiple"
ref="multiContainer"
2023-05-23 12:44:11 +00:00
:class="cx('container')"
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"
2023-05-05 06:40:09 +00:00
v-bind="ptm('container')"
2022-09-14 11:26:01 +00:00
>
<li
v-for="(option, i) of modelValue"
:key="i"
:id="id + '_multiple_option_' + i"
2023-05-23 12:44:11 +00:00
:class="cx('token', { 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"
2023-05-05 06:40:09 +00:00
v-bind="ptm('token')"
2022-09-14 11:26:01 +00:00
>
2022-09-06 12:03:37 +00:00
<slot name="chip" :value="option">
<span :class="cx('tokenLabel')" v-bind="ptm('tokenLabel')">{{ getOptionLabel(option) }}</span>
2022-09-06 12:03:37 +00:00
</slot>
2023-10-31 17:21:47 +00:00
<slot name="removetokenicon" :class="cx('removeTokenIcon')" :index="i" :onClick="(event) => removeOption(event, i)" :removeCallback="(event) => removeOption(event, i)">
<component :is="removeTokenIcon ? 'span' : 'TimesCircleIcon'" :class="[cx('removeTokenIcon'), removeTokenIcon]" @click="removeOption($event, i)" aria-hidden="true" v-bind="ptm('removeTokenIcon')" />
</slot>
2022-09-06 12:03:37 +00:00
</li>
<li :class="cx('inputToken')" role="option" v-bind="ptm('inputToken')">
2022-09-14 11:26:01 +00:00
<input
ref="focusInput"
:id="inputId"
type="text"
: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"
:aria-invalid="invalid || undefined"
2022-09-14 11:26:01 +00:00
@focus="onFocus"
@blur="onBlur"
@keydown="onKeyDown"
@input="onInput"
@change="onChange"
2023-05-05 06:40:09 +00:00
v-bind="{ ...inputProps, ...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('loadingIcon')" name="loadingicon">
<i v-if="loadingIcon" :class="['pi-spin', cx('loadingIcon'), loadingIcon]" aria-hidden="true" v-bind="ptm('loadingIcon')" />
<SpinnerIcon v-else :class="[cx('loadingIcon'), loadingIcon]" spin aria-hidden="true" v-bind="ptm('loadingIcon')" />
</slot>
<Button v-if="dropdown" ref="dropdownButton" type="button" tabindex="-1" :class="[cx('dropdownButton'), dropdownClass]" :disabled="disabled" aria-hidden="true" @click="onDropdownClick" :unstyled="unstyled" :pt="ptm('dropdownButton')">
<template #icon>
<slot name="dropdownicon" :class="dropdownIcon">
2023-09-06 07:13:22 +00:00
<component :is="dropdownIcon ? 'span' : 'ChevronDownIcon'" :class="dropdownIcon" v-bind="ptm('dropdownButton')['icon']" data-pc-section="dropdownicon" />
</slot>
</template>
</Button>
<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"
:class="[cx('panel'), panelClass]"
2023-05-05 06:40:09 +00:00
:style="{ ...panelStyle, 'max-height': virtualScrollerDisabled ? scrollHeight : '' }"
@click="onOverlayClick"
@keydown="onOverlayKeyDown"
v-bind="{ ...panelProps, ...ptm('panel') }"
>
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" 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('itemGroup')" role="option" v-bind="ptm('itemGroup')">
2022-09-14 11:26:01 +00:00
<slot name="optiongroup" :option="option.optionGroup" :item="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 }"
2023-05-23 12:44:11 +00:00
:class="cx('item', { 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))"
2023-05-23 12:44:11 +00:00
:data-p-highlight="isSelected(option)"
:data-p-focus="focusedOptionIndex === getOptionIndex(i, getItemOptions)"
2023-05-23 12:44:11 +00:00
:data-p-disabled="isOptionDisabled(option)"
2023-05-08 07:12:02 +00:00
v-bind="getPTOptions(option, getItemOptions, i, 'item')"
2022-09-14 11:26:01 +00:00
>
<slot v-if="$slots.option" name="option" :option="option" :index="getOptionIndex(i, getItemOptions)">{{ getOptionLabel(option) }}</slot>
<slot v-else name="item" :item="option" :index="getOptionIndex(i, getItemOptions)">{{ getOptionLabel(option) }}</slot>
<!--TODO: Deprecated since v3.16.0-->
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>
import Button from 'primevue/button';
import ChevronDownIcon from 'primevue/icons/chevrondown';
import SpinnerIcon from 'primevue/icons/spinner';
import TimesCircleIcon from 'primevue/icons/timescircle';
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,
2022-09-06 12:03:37 +00:00
emits: ['update:modelValue', 'change', 'focus', 'blur', 'item-select', 'item-unselect', 'dropdown-click', 'clear', 'complete', 'before-show', 'before-hide', 'show', 'hide'],
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,
searching: false
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
watch: {
'$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-01-23 18:06:31 +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.field || this.optionLabel ? ObjectUtils.resolveFieldData(option, this.field || this.optionLabel) : option;
},
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;
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
isFocus && DomHandler.focus(this.$refs.focusInput);
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;
2022-09-14 11:26:01 +00:00
isFocus && DomHandler.focus(this.$refs.focusInput);
};
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) {
if (this.searchTimeout) {
clearTimeout(this.searchTimeout);
}
let query = event.target.value;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (!this.multiple) {
this.updateModel(event, query);
}
if (query.length === 0) {
this.hide();
this.$emit('clear');
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
if (query.length >= this.minLength) {
this.focusedOptionIndex = -1;
this.searchTimeout = setTimeout(() => {
this.search(event, query, 'input');
}, this.delay);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
this.hide();
}
}
},
onChange(event) {
if (this.forceSelection) {
let valid = false;
// when forceSelection is on, prevent called twice onOptionSelect()
if (this.visibleOptions && !this.multiple) {
2022-12-08 11:04:25 +00:00
const matchedValue = this.visibleOptions.find((option) => this.isOptionMatched(option, this.$refs.focusInput.value || ''));
2022-09-06 12:03:37 +00:00
if (matchedValue !== undefined) {
valid = true;
!this.isSelected(matchedValue) && this.onOptionSelect(event, matchedValue);
}
}
if (!valid) {
this.$refs.focusInput.value = '';
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)) {
2022-09-14 11:26:01 +00:00
DomHandler.focus(this.$refs.focusInput);
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 {
DomHandler.focus(this.$refs.focusInput);
2022-09-06 12:03:37 +00:00
query = this.$refs.focusInput.value;
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) {
this.$refs.focusInput.value = '';
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 });
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) {
if (!this.overlayVisible) {
this.focusedOptionIndex = -1; // reset
2022-09-06 12:03:37 +00:00
this.onArrowDownKey(event);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
if (this.focusedOptionIndex !== -1) {
this.onOptionSelect(event, this.visibleOptions[this.focusedOptionIndex]);
}
this.hide();
}
event.preventDefault();
},
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) {
if (ObjectUtils.isNotEmpty(this.modelValue) && !this.$refs.focusInput.value) {
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 });
}
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;
2022-09-14 11:26:01 +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() {
let target = this.multiple ? this.$refs.multiContainer : this.$refs.focusInput;
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);
else return event.target === this.$refs.focusInput;
2022-09-06 12:03:37 +00:00
},
isDropdownClicked(event) {
2022-09-14 11:26:01 +00:00
return this.$refs.dropdownButton ? event.target === this.$refs.dropdownButton || this.$refs.dropdownButton.$el.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 });
this.dirty = true;
2022-09-14 11:26:01 +00:00
DomHandler.focus(this.$refs.focusInput);
2022-09-06 12:03:37 +00:00
},
changeFocusedOptionIndex(event, index) {
if (this.focusedOptionIndex !== index) {
this.focusedOptionIndex = index;
this.scrollInView();
if (this.selectOnFocus || this.autoHighlight) {
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.autoHighlight) && this.autoOptionFocus && !this.hasSelectedOption) {
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;
},
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;
}
},
components: {
2022-09-14 11:26:01 +00:00
Button: Button,
VirtualScroller: VirtualScroller,
Portal: Portal,
ChevronDownIcon: ChevronDownIcon,
SpinnerIcon: SpinnerIcon,
TimesCircleIcon: TimesCircleIcon
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>