2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
|
|
|
<div ref="container" :class="containerClass" @click="onContainerClick">
|
2022-09-14 11:26:01 +00:00
|
|
|
<input
|
|
|
|
v-if="!multiple"
|
|
|
|
ref="focusInput"
|
|
|
|
:id="inputId"
|
|
|
|
type="text"
|
|
|
|
:style="inputStyle"
|
|
|
|
:class="inputStyleClass"
|
|
|
|
: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"
|
|
|
|
@focus="onFocus"
|
|
|
|
@blur="onBlur"
|
|
|
|
@keydown="onKeyDown"
|
|
|
|
@input="onInput"
|
|
|
|
@change="onChange"
|
|
|
|
v-bind="inputProps"
|
|
|
|
/>
|
|
|
|
<ul
|
|
|
|
v-if="multiple"
|
|
|
|
ref="multiContainer"
|
|
|
|
:class="multiContainerClass"
|
|
|
|
tabindex="-1"
|
|
|
|
role="listbox"
|
|
|
|
aria-orientation="horizontal"
|
|
|
|
:aria-activedescendant="focused ? focusedMultipleOptionId : undefined"
|
|
|
|
@focus="onMultipleContainerFocus"
|
|
|
|
@blur="onMultipleContainerBlur"
|
|
|
|
@keydown="onMultipleContainerKeyDown"
|
|
|
|
>
|
|
|
|
<li
|
|
|
|
v-for="(option, i) of modelValue"
|
|
|
|
:key="i"
|
|
|
|
:id="id + '_multiple_option_' + i"
|
|
|
|
:class="['p-autocomplete-token', { 'p-focus': focusedMultipleOptionIndex === i }]"
|
|
|
|
role="option"
|
|
|
|
:aria-label="getOptionLabel(option)"
|
|
|
|
:aria-selected="true"
|
|
|
|
:aria-setsize="modelValue.length"
|
|
|
|
:aria-posinset="i + 1"
|
|
|
|
>
|
2022-09-06 12:03:37 +00:00
|
|
|
<slot name="chip" :value="option">
|
2022-09-14 11:26:01 +00:00
|
|
|
<span class="p-autocomplete-token-label">{{ getOptionLabel(option) }}</span>
|
2022-09-06 12:03:37 +00:00
|
|
|
</slot>
|
2023-04-04 12:38:11 +00:00
|
|
|
<slot name="removetokenicon">
|
|
|
|
<component :is="removeTokenIcon ? 'span' : 'TimesCircleIcon'" :class="['p-autocomplete-token-icon', removeTokenIcon]" @click="removeOption($event, i)" aria-hidden="true" />
|
|
|
|
</slot>
|
2022-09-06 12:03:37 +00:00
|
|
|
</li>
|
|
|
|
<li class="p-autocomplete-input-token" role="option">
|
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"
|
|
|
|
@focus="onFocus"
|
|
|
|
@blur="onBlur"
|
|
|
|
@keydown="onKeyDown"
|
|
|
|
@input="onInput"
|
|
|
|
@change="onChange"
|
|
|
|
v-bind="inputProps"
|
|
|
|
/>
|
2022-09-06 12:03:37 +00:00
|
|
|
</li>
|
|
|
|
</ul>
|
2023-04-04 12:38:11 +00:00
|
|
|
<slot v-if="searching" name="loadingicon">
|
2023-04-18 07:35:20 +00:00
|
|
|
<i v-if="loadingIcon" :class="['p-autocomplete-loader pi-spin', loadingIcon]" aria-hidden="true" />
|
|
|
|
<SpinnerIcon v-else class="p-autocomplete-loader" spin aria-hidden="true" />
|
2023-04-04 12:38:11 +00:00
|
|
|
</slot>
|
|
|
|
<Button v-if="dropdown" ref="dropdownButton" type="button" tabindex="-1" :class="['p-autocomplete-dropdown', dropdownClass]" :disabled="disabled" aria-hidden="true" @click="onDropdownClick">
|
2023-04-13 09:20:09 +00:00
|
|
|
<template #icon>
|
|
|
|
<slot name="dropdownicon">
|
|
|
|
<component :is="dropdownIcon ? 'span' : 'ChevronDownIcon'" :class="dropdownIcon" />
|
|
|
|
</slot>
|
|
|
|
</template>
|
2023-04-04 12:38:11 +00:00
|
|
|
</Button>
|
2022-09-06 12:03:37 +00:00
|
|
|
<span role="status" aria-live="polite" class="p-hidden-accessible">
|
2022-09-14 11:26:01 +00:00
|
|
|
{{ searchResultMessageText }}
|
2022-09-06 12:03:37 +00:00
|
|
|
</span>
|
|
|
|
<Portal :appendTo="appendTo">
|
|
|
|
<transition name="p-connected-overlay" @enter="onOverlayEnter" @after-enter="onOverlayAfterEnter" @leave="onOverlayLeave" @after-leave="onOverlayAfterLeave">
|
2022-09-14 11:26:01 +00:00
|
|
|
<div v-if="overlayVisible" :ref="overlayRef" :class="panelStyleClass" :style="{ ...panelStyle, 'max-height': virtualScrollerDisabled ? scrollHeight : '' }" @click="onOverlayClick" @keydown="onOverlayKeyDown" v-bind="panelProps">
|
2022-09-06 12:03:37 +00:00
|
|
|
<slot name="header" :value="modelValue" :suggestions="visibleOptions"></slot>
|
2022-09-14 11:26:01 +00:00
|
|
|
<VirtualScroller :ref="virtualScrollerRef" v-bind="virtualScrollerOptions" :style="{ height: scrollHeight }" :items="visibleOptions" :tabindex="-1" :disabled="virtualScrollerDisabled">
|
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="['p-autocomplete-items', styleClass]" :style="contentStyle" role="listbox">
|
|
|
|
<template v-for="(option, i) of items" :key="getOptionRenderKey(option, getOptionIndex(i, getItemOptions))">
|
2022-09-14 11:26:01 +00:00
|
|
|
<li v-if="isOptionGroup(option)" :id="id + '_' + getOptionIndex(i, getItemOptions)" :style="{ height: itemSize ? itemSize + 'px' : undefined }" class="p-autocomplete-item-group" role="option">
|
|
|
|
<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 }"
|
|
|
|
:class="['p-autocomplete-item', { 'p-highlight': isSelected(option), 'p-focus': focusedOptionIndex === getOptionIndex(i, getItemOptions), 'p-disabled': isOptionDisabled(option) }]"
|
|
|
|
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))"
|
|
|
|
>
|
|
|
|
<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>
|
2023-01-09 14:41:49 +00:00
|
|
|
<li v-if="!items || (items && items.length === 0)" class="p-autocomplete-empty-message" role="option">
|
|
|
|
<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>
|
2022-12-08 11:04:25 +00:00
|
|
|
<span role="status" aria-live="polite" class="p-hidden-accessible">
|
|
|
|
{{ selectedMessageText }}
|
|
|
|
</span>
|
2022-09-06 12:03:37 +00:00
|
|
|
</div>
|
|
|
|
</transition>
|
|
|
|
</Portal>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import Button from 'primevue/button';
|
2023-04-18 12:53:43 +00:00
|
|
|
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';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'AutoComplete',
|
|
|
|
emits: ['update:modelValue', 'change', 'focus', 'blur', 'item-select', 'item-unselect', 'dropdown-click', 'clear', 'complete', 'before-show', 'before-hide', 'show', 'hide'],
|
|
|
|
props: {
|
|
|
|
modelValue: null,
|
|
|
|
suggestions: {
|
|
|
|
type: Array,
|
|
|
|
default: null
|
|
|
|
},
|
2022-09-14 11:26:01 +00:00
|
|
|
field: {
|
|
|
|
// TODO: Deprecated since v3.16.0
|
|
|
|
type: [String, Function],
|
2022-09-06 12:03:37 +00:00
|
|
|
default: null
|
|
|
|
},
|
|
|
|
optionLabel: null,
|
|
|
|
optionDisabled: null,
|
|
|
|
optionGroupLabel: null,
|
|
|
|
optionGroupChildren: null,
|
|
|
|
scrollHeight: {
|
|
|
|
type: String,
|
|
|
|
default: '200px'
|
|
|
|
},
|
|
|
|
dropdown: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
dropdownMode: {
|
|
|
|
type: String,
|
|
|
|
default: 'blank'
|
|
|
|
},
|
2022-09-14 11:26:01 +00:00
|
|
|
autoHighlight: {
|
|
|
|
// TODO: Deprecated since v3.16.0. Use selectOnFocus property instead.
|
2022-09-06 12:03:37 +00:00
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
multiple: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
placeholder: {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
dataKey: {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
minLength: {
|
|
|
|
type: Number,
|
|
|
|
default: 1
|
|
|
|
},
|
|
|
|
delay: {
|
|
|
|
type: Number,
|
|
|
|
default: 300
|
|
|
|
},
|
|
|
|
appendTo: {
|
|
|
|
type: String,
|
|
|
|
default: 'body'
|
|
|
|
},
|
|
|
|
forceSelection: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
completeOnFocus: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
2022-09-14 11:26:01 +00:00
|
|
|
inputId: {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
inputStyle: {
|
2023-03-09 07:02:25 +00:00
|
|
|
type: Object,
|
2022-09-14 11:26:01 +00:00
|
|
|
default: null
|
|
|
|
},
|
|
|
|
inputClass: {
|
2023-03-09 07:02:25 +00:00
|
|
|
type: [String, Object],
|
2022-09-14 11:26:01 +00:00
|
|
|
default: null
|
|
|
|
},
|
|
|
|
inputProps: {
|
|
|
|
type: null,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
panelStyle: {
|
2023-03-09 07:02:25 +00:00
|
|
|
type: Object,
|
2022-09-14 11:26:01 +00:00
|
|
|
default: null
|
|
|
|
},
|
|
|
|
panelClass: {
|
2023-03-09 07:02:25 +00:00
|
|
|
type: [String, Object],
|
2022-09-14 11:26:01 +00:00
|
|
|
default: null
|
|
|
|
},
|
|
|
|
panelProps: {
|
|
|
|
type: null,
|
|
|
|
default: null
|
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
dropdownIcon: {
|
|
|
|
type: String,
|
2023-04-04 12:38:11 +00:00
|
|
|
default: undefined
|
2022-12-08 11:04:25 +00:00
|
|
|
},
|
|
|
|
dropdownClass: {
|
2023-03-09 07:02:25 +00:00
|
|
|
type: [String, Object],
|
2022-12-08 11:04:25 +00:00
|
|
|
default: null
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
loadingIcon: {
|
|
|
|
type: String,
|
2023-04-04 12:38:11 +00:00
|
|
|
default: undefined
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
removeTokenIcon: {
|
|
|
|
type: String,
|
2023-04-04 12:38:11 +00:00
|
|
|
default: undefined
|
2022-12-08 11:04:25 +00:00
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
virtualScrollerOptions: {
|
|
|
|
type: Object,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
autoOptionFocus: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
|
|
|
},
|
2022-09-14 11:26:01 +00:00
|
|
|
selectOnFocus: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
searchLocale: {
|
|
|
|
type: String,
|
|
|
|
default: undefined
|
|
|
|
},
|
|
|
|
searchMessage: {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
selectionMessage: {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
emptySelectionMessage: {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
emptySearchMessage: {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
tabindex: {
|
|
|
|
type: Number,
|
|
|
|
default: 0
|
|
|
|
},
|
|
|
|
'aria-label': {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
'aria-labelledby': {
|
|
|
|
type: String,
|
|
|
|
default: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
outsideClickListener: null,
|
|
|
|
resizeListener: null,
|
|
|
|
scrollHandler: null,
|
|
|
|
overlay: null,
|
|
|
|
virtualScroller: null,
|
|
|
|
searchTimeout: null,
|
|
|
|
focusOnHover: false,
|
|
|
|
dirty: false,
|
|
|
|
data() {
|
|
|
|
return {
|
2023-01-19 04:01:03 +00:00
|
|
|
id: this.$attrs.id,
|
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: {
|
2023-01-19 04:01:03 +00:00
|
|
|
'$attrs.id': function (newValue) {
|
|
|
|
this.id = newValue || UniqueComponentId();
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
suggestions() {
|
|
|
|
if (this.searching) {
|
2023-01-09 14:41:49 +00:00
|
|
|
ObjectUtils.isNotEmpty(this.suggestions) ? this.show() : !!this.$slots.empty ? this.show() : this.hide();
|
2022-09-06 12:03:37 +00:00
|
|
|
this.focusedOptionIndex = this.overlayVisible && this.autoOptionFocus ? this.findFirstFocusedOptionIndex() : -1;
|
|
|
|
this.searching = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.autoUpdateModel();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {
|
2023-01-19 04:01:03 +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;
|
|
|
|
},
|
|
|
|
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.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) {
|
2023-01-11 08:57:49 +00:00
|
|
|
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;
|
2022-12-08 11:04:25 +00:00
|
|
|
this.focusedOptionIndex = this.focusedOptionIndex !== -1 ? this.focusedOptionIndex : this.overlayVisible && this.autoOptionFocus ? this.findFirstFocusedOptionIndex() : -1;
|
2022-09-06 12:03:37 +00:00
|
|
|
this.overlayVisible && this.scrollInView(this.focusedOptionIndex);
|
|
|
|
this.$emit('focus', event);
|
|
|
|
},
|
|
|
|
onBlur(event) {
|
|
|
|
this.dirty = false;
|
|
|
|
this.focused = false;
|
|
|
|
this.focusedOptionIndex = -1;
|
|
|
|
this.$emit('blur', event);
|
|
|
|
},
|
|
|
|
onKeyDown(event) {
|
2023-01-11 08:57:49 +00:00
|
|
|
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':
|
2023-03-30 08:59:23 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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;
|
|
|
|
|
2022-09-14 11:26:01 +00:00
|
|
|
if (this.visibleOptions) {
|
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() {
|
2023-01-11 08:57:49 +00:00
|
|
|
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) {
|
2023-01-11 08:57:49 +00:00
|
|
|
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) {
|
|
|
|
if (this.disabled || this.searching || this.isInputClicked(event) || this.isDropdownClicked(event)) {
|
|
|
|
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.findFirstFocusedOptionIndex();
|
|
|
|
|
|
|
|
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 {
|
2022-09-06 12:03:37 +00:00
|
|
|
const optionIndex = this.focusedOptionIndex !== -1 ? this.findPrevOptionIndex(this.focusedOptionIndex) : this.findLastFocusedOptionIndex();
|
|
|
|
|
|
|
|
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) {
|
2023-01-09 13:06:17 +00:00
|
|
|
const { currentTarget } = event;
|
|
|
|
const len = currentTarget.value.length;
|
2022-12-08 11:04:25 +00:00
|
|
|
|
2023-01-09 13:06:17 +00:00
|
|
|
currentTarget.setSelectionRange(0, event.shiftKey ? len : 0);
|
2022-09-06 12:03:37 +00:00
|
|
|
this.focusedOptionIndex = -1;
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
onEndKey(event) {
|
2023-01-09 13:06:17 +00:00
|
|
|
const { currentTarget } = event;
|
|
|
|
const len = currentTarget.value.length;
|
2022-12-08 11:04:25 +00:00
|
|
|
|
2023-01-09 13:06:17 +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.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);
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
isValidOption(option) {
|
|
|
|
return option && !(this.isOptionDisabled(option) || this.isOptionGroup(option));
|
|
|
|
},
|
|
|
|
isValidSelectedOption(option) {
|
|
|
|
return this.isValidOption(option) && this.isSelected(option);
|
|
|
|
},
|
|
|
|
isSelected(option) {
|
|
|
|
return ObjectUtils.equals(this.modelValue, this.getOptionValue(option), this.equalityKey);
|
|
|
|
},
|
|
|
|
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) {
|
|
|
|
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
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
if (element) {
|
|
|
|
element.scrollIntoView && element.scrollIntoView({ block: 'nearest', inline: 'start' });
|
2022-09-14 11:26:01 +00:00
|
|
|
} else if (!this.virtualScrollerDisabled) {
|
2022-09-06 12:03:37 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
this.virtualScroller && this.virtualScroller.scrollToIndex(index !== -1 ? index : this.focusedOptionIndex);
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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: {
|
|
|
|
containerClass() {
|
2022-09-14 11:26:01 +00:00
|
|
|
return [
|
|
|
|
'p-autocomplete p-component p-inputwrapper',
|
|
|
|
{
|
|
|
|
'p-disabled': this.disabled,
|
|
|
|
'p-focus': this.focused,
|
|
|
|
'p-autocomplete-dd': this.dropdown,
|
|
|
|
'p-autocomplete-multiple': this.multiple,
|
|
|
|
'p-inputwrapper-filled': this.modelValue || ObjectUtils.isNotEmpty(this.inputValue),
|
|
|
|
'p-inputwrapper-focus': this.focused,
|
|
|
|
'p-overlay-open': this.overlayVisible
|
|
|
|
}
|
|
|
|
];
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
inputStyleClass() {
|
2022-09-14 11:26:01 +00:00
|
|
|
return [
|
|
|
|
'p-autocomplete-input p-inputtext p-component',
|
|
|
|
this.inputClass,
|
|
|
|
{
|
|
|
|
'p-autocomplete-dd-input': this.dropdown
|
|
|
|
}
|
|
|
|
];
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
multiContainerClass() {
|
|
|
|
return ['p-autocomplete-multiple-container p-component p-inputtext'];
|
|
|
|
},
|
|
|
|
panelStyleClass() {
|
2022-09-14 11:26:01 +00:00
|
|
|
return [
|
|
|
|
'p-autocomplete-panel p-component',
|
|
|
|
this.panelClass,
|
|
|
|
{
|
|
|
|
'p-input-filled': this.$primevue.config.inputStyle === 'filled',
|
|
|
|
'p-ripple-disabled': this.$primevue.config.ripple === false
|
|
|
|
}
|
|
|
|
];
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
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 (this.modelValue) {
|
|
|
|
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,
|
2023-04-04 12:38:11 +00:00
|
|
|
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>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.p-autocomplete {
|
|
|
|
display: inline-flex;
|
|
|
|
position: relative;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-autocomplete-loader {
|
|
|
|
position: absolute;
|
|
|
|
top: 50%;
|
2022-09-14 11:26:01 +00:00
|
|
|
margin-top: -0.5rem;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
.p-autocomplete-dd .p-autocomplete-input {
|
|
|
|
flex: 1 1 auto;
|
|
|
|
width: 1%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-autocomplete-dd .p-autocomplete-input,
|
|
|
|
.p-autocomplete-dd .p-autocomplete-multiple-container {
|
2022-09-14 11:26:01 +00:00
|
|
|
border-top-right-radius: 0;
|
|
|
|
border-bottom-right-radius: 0;
|
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
.p-autocomplete-dd .p-autocomplete-dropdown {
|
2022-09-14 11:26:01 +00:00
|
|
|
border-top-left-radius: 0;
|
|
|
|
border-bottom-left-radius: 0px;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
.p-autocomplete .p-autocomplete-panel {
|
|
|
|
min-width: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-autocomplete-panel {
|
|
|
|
position: absolute;
|
|
|
|
overflow: auto;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-autocomplete-items {
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
list-style-type: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-autocomplete-item {
|
|
|
|
cursor: pointer;
|
|
|
|
white-space: nowrap;
|
|
|
|
position: relative;
|
|
|
|
overflow: hidden;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-autocomplete-multiple-container {
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
list-style-type: none;
|
|
|
|
cursor: text;
|
|
|
|
overflow: hidden;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-autocomplete-token {
|
|
|
|
cursor: default;
|
|
|
|
display: inline-flex;
|
|
|
|
align-items: center;
|
|
|
|
flex: 0 0 auto;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-autocomplete-token-icon {
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-autocomplete-input-token {
|
|
|
|
flex: 1 1 auto;
|
|
|
|
display: inline-flex;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-autocomplete-input-token input {
|
|
|
|
border: 0 none;
|
|
|
|
outline: 0 none;
|
|
|
|
background-color: transparent;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
box-shadow: none;
|
|
|
|
border-radius: 0;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-fluid .p-autocomplete {
|
|
|
|
display: flex;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-fluid .p-autocomplete-dd .p-autocomplete-input {
|
|
|
|
width: 1%;
|
|
|
|
}
|
|
|
|
</style>
|