2023-11-29 06:47:20 +08:00
|
|
|
<template>
|
2024-03-05 20:29:21 +08:00
|
|
|
<div v-click-outside="close"
|
|
|
|
@focus="open"
|
|
|
|
@keydown="keySelectOptions($event)"
|
2024-05-23 14:29:17 +08:00
|
|
|
tabindex="0" class="w-full focus:outline-none"
|
2024-05-23 19:12:05 +08:00
|
|
|
:data-e2e="e2eValue"
|
2024-03-05 20:29:21 +08:00
|
|
|
>
|
2023-11-29 06:47:20 +08:00
|
|
|
<div
|
|
|
|
ref="field"
|
2024-03-05 20:29:21 +08:00
|
|
|
class="px-3 py-1 border border-solid rounded flex items-center cursor-pointer"
|
2023-11-29 06:47:20 +08:00
|
|
|
@click="open"
|
|
|
|
:class="[sizeClass, {
|
2024-03-05 20:29:21 +08:00
|
|
|
'!border-sn-blue': isOpen,
|
|
|
|
'!border-sn-light-grey': !isOpen,
|
2024-03-20 22:17:56 +08:00
|
|
|
'bg-sn-super-light-grey': disabled
|
2023-11-29 06:47:20 +08:00
|
|
|
}]"
|
|
|
|
>
|
2023-12-12 19:17:38 +08:00
|
|
|
<template v-if="!tagsView">
|
2023-11-29 06:47:20 +08:00
|
|
|
<template v-if="!isOpen || !searchable">
|
|
|
|
<div class="truncate" v-if="labelRenderer && label" v-html="label"></div>
|
|
|
|
<div class="truncate" v-else-if="label">{{ label }}</div>
|
2023-12-11 16:18:22 +08:00
|
|
|
<div class="text-sn-grey truncate" v-else>
|
|
|
|
{{ placeholder || this.i18n.t('general.select_dropdown.placeholder') }}
|
|
|
|
</div>
|
2023-11-29 06:47:20 +08:00
|
|
|
</template>
|
|
|
|
<input type="text"
|
|
|
|
ref="search"
|
|
|
|
v-else
|
|
|
|
v-model="query"
|
2024-05-23 18:24:17 +08:00
|
|
|
:placeholder="placeholderRender"
|
2024-05-06 18:42:19 +08:00
|
|
|
@keyup="fetchOptions"
|
2024-05-23 19:53:18 +08:00
|
|
|
@change.stop
|
2023-11-29 06:47:20 +08:00
|
|
|
class="w-full border-0 outline-none pl-0 placeholder:text-sn-grey" />
|
2023-12-12 19:17:38 +08:00
|
|
|
</template>
|
2024-03-26 22:07:23 +08:00
|
|
|
<div v-else class="flex items-center gap-1 flex-wrap">
|
|
|
|
<div v-for="tag in tags" class="px-2 py-1 rounded-sm bg-sn-super-light-grey grid grid-cols-[auto_1fr] items-center gap-1">
|
2023-12-12 19:17:38 +08:00
|
|
|
<div class="truncate" v-if="labelRenderer" v-html="tag.label"></div>
|
|
|
|
<div class="truncate" v-else>{{ tag.label }}</div>
|
|
|
|
<i @click="removeTag(tag.value)" class="sn-icon mini ml-auto sn-icon-close cursor-pointer"></i>
|
|
|
|
</div>
|
|
|
|
<input type="text"
|
|
|
|
ref="search"
|
|
|
|
v-if="searchable && isOpen"
|
|
|
|
v-model="query"
|
|
|
|
:placeholder="tags.length > 0 ? '' : (placeholder || this.i18n.t('general.select_dropdown.placeholder'))"
|
|
|
|
:style="{ width: searchInputSize }"
|
|
|
|
:class="{ 'pl-2': tags.length > 0 }"
|
2024-05-23 19:53:18 +08:00
|
|
|
@change.stop
|
2023-12-12 19:17:38 +08:00
|
|
|
class="border-0 outline-none pl-0 py-1 placeholder:text-sn-grey" />
|
|
|
|
<div v-else-if="tags.length == 0" class="text-sn-grey truncate">
|
|
|
|
{{ placeholder || this.i18n.t('general.select_dropdown.placeholder') }}
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-11-29 06:47:20 +08:00
|
|
|
<i v-if="canClear" @click="clear" class="sn-icon ml-auto sn-icon-close"></i>
|
2024-04-05 17:16:58 +08:00
|
|
|
<i v-else class="sn-icon ml-auto"
|
2023-12-11 16:18:22 +08:00
|
|
|
:class="{ 'sn-icon-down': !isOpen, 'sn-icon-up': isOpen, 'text-sn-grey': disabled}"></i>
|
2023-11-29 06:47:20 +08:00
|
|
|
</div>
|
2024-02-20 17:55:03 +08:00
|
|
|
<template v-if="isOpen">
|
|
|
|
<teleport to="body">
|
|
|
|
<div ref="flyout"
|
|
|
|
class="sn-select-dropdown bg-white inline-block sn-shadow-menu-sm rounded w-full
|
|
|
|
fixed z-[3000]">
|
|
|
|
<div v-if="multiple && withCheckboxes" class="p-2.5 pb-0">
|
|
|
|
<div @click="selectAll" :class="sizeClass"
|
2024-03-05 20:29:21 +08:00
|
|
|
class="border border-x-0 !border-transparent border-solid !border-b-sn-light-grey
|
2024-02-20 17:55:03 +08:00
|
|
|
py-1.5 px-3 cursor-pointer flex items-center gap-2 shrink-0">
|
|
|
|
<div class="sn-checkbox-icon"
|
|
|
|
:class="selectAllState"
|
2023-12-05 03:59:16 +08:00
|
|
|
></div>
|
2024-02-20 17:55:03 +08:00
|
|
|
{{ i18n.t('general.select_all') }}
|
2023-12-05 03:59:16 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2024-02-20 17:55:03 +08:00
|
|
|
<perfect-scrollbar class="p-2.5 flex flex-col max-h-80 relative" :class="{ 'pt-0': withCheckboxes }">
|
2024-03-05 20:29:21 +08:00
|
|
|
<template v-for="(option, i) in filteredOptions" :key="option[0]">
|
2024-02-20 17:55:03 +08:00
|
|
|
<div
|
|
|
|
@click.stop="setValue(option[0])"
|
2024-03-05 20:29:21 +08:00
|
|
|
ref="options"
|
2024-04-22 19:31:40 +08:00
|
|
|
:title="option[2]?.tooltip || option[1]"
|
2024-03-26 22:07:23 +08:00
|
|
|
class="py-1.5 px-3 rounded cursor-pointer flex items-center gap-2 shrink-0 hover:bg-sn-super-light-grey"
|
2024-03-05 20:29:21 +08:00
|
|
|
:class="[sizeClass, {
|
|
|
|
'!bg-sn-super-light-blue': valueSelected(option[0]) && focusedOption !== i,
|
|
|
|
'!bg-sn-super-light-grey': focusedOption === i ,
|
|
|
|
}]"
|
2024-02-20 17:55:03 +08:00
|
|
|
>
|
|
|
|
<div v-if="withCheckboxes"
|
2024-03-04 01:13:09 +08:00
|
|
|
class="sn-checkbox-icon shrink-0"
|
2024-02-20 17:55:03 +08:00
|
|
|
:class="{
|
|
|
|
'checked': valueSelected(option[0]),
|
|
|
|
'unchecked': !valueSelected(option[0]),
|
|
|
|
}"
|
|
|
|
></div>
|
2024-03-04 01:13:09 +08:00
|
|
|
<div class="truncate" v-if="optionRenderer" v-html="optionRenderer(option)"></div>
|
|
|
|
<div class="truncate" v-else >{{ option[1] }}</div>
|
2024-02-20 17:55:03 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<div v-if="filteredOptions.length === 0" class="text-sn-grey text-center py-2.5">
|
|
|
|
{{ noOptionsPlaceholder || this.i18n.t('general.select_dropdown.no_options_placeholder') }}
|
|
|
|
</div>
|
|
|
|
</perfect-scrollbar>
|
|
|
|
</div>
|
|
|
|
</teleport>
|
|
|
|
</template>
|
2023-11-29 06:47:20 +08:00
|
|
|
</div>
|
|
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-12-11 16:18:22 +08:00
|
|
|
import { vOnClickOutside } from '@vueuse/components';
|
2023-12-05 03:59:16 +08:00
|
|
|
import FixedFlyoutMixin from './mixins/fixed_flyout.js';
|
2024-01-11 02:56:01 +08:00
|
|
|
import axios from '../../packs/custom_axios.js';
|
2023-11-29 06:47:20 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'SelectDropdown',
|
|
|
|
props: {
|
|
|
|
value: { type: [String, Number, Array] },
|
|
|
|
options: { type: Array, default: () => [] },
|
|
|
|
optionsUrl: { type: String },
|
2023-12-11 16:18:22 +08:00
|
|
|
placeholder: { type: String },
|
2023-11-29 06:47:20 +08:00
|
|
|
noOptionsPlaceholder: { type: String },
|
|
|
|
fewOptionsPlaceholder: { type: String },
|
|
|
|
allOptionsPlaceholder: { type: String },
|
|
|
|
optionRenderer: { type: Function },
|
|
|
|
labelRenderer: { type: Function },
|
|
|
|
disabled: { type: Boolean, default: false },
|
|
|
|
size: { type: String, default: 'md' },
|
|
|
|
multiple: { type: Boolean, default: false },
|
|
|
|
withCheckboxes: { type: Boolean, default: false },
|
|
|
|
searchable: { type: Boolean, default: false },
|
|
|
|
clearable: { type: Boolean, default: false },
|
2024-01-11 02:56:01 +08:00
|
|
|
tagsView: { type: Boolean, default: false },
|
2024-05-23 14:29:17 +08:00
|
|
|
urlParams: { type: Object, default: () => ({}) },
|
2024-05-23 19:12:05 +08:00
|
|
|
e2eValue: { type: String, default: '' }
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
|
|
|
directives: {
|
2024-03-05 20:29:21 +08:00
|
|
|
'click-outside': vOnClickOutside
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
newValue: null,
|
|
|
|
isOpen: false,
|
|
|
|
fetchedOptions: [],
|
|
|
|
selectAllState: 'unchecked',
|
|
|
|
query: '',
|
2023-12-11 16:18:22 +08:00
|
|
|
fixedWidth: true,
|
2024-05-06 18:42:19 +08:00
|
|
|
focusedOption: null,
|
|
|
|
skipQueryCallback: false
|
2023-12-11 16:18:22 +08:00
|
|
|
};
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
2023-12-05 03:59:16 +08:00
|
|
|
mixins: [FixedFlyoutMixin],
|
2023-11-29 06:47:20 +08:00
|
|
|
computed: {
|
2024-05-23 18:24:17 +08:00
|
|
|
placeholderRender() {
|
|
|
|
if (this.searchable && this.labelRenderer && this.label) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.label || this.placeholder || this.i18n.t('general.select_dropdown.placeholder');
|
|
|
|
},
|
2023-11-29 06:47:20 +08:00
|
|
|
sizeClass() {
|
|
|
|
switch (this.size) {
|
|
|
|
case 'xs':
|
2023-12-11 16:18:22 +08:00
|
|
|
return 'min-h-[36px]';
|
2023-11-29 06:47:20 +08:00
|
|
|
case 'sm':
|
2023-12-11 16:18:22 +08:00
|
|
|
return 'min-h-[40px]';
|
2023-11-29 06:47:20 +08:00
|
|
|
case 'md':
|
2023-12-11 16:18:22 +08:00
|
|
|
return 'min-h-[44px]';
|
|
|
|
default:
|
|
|
|
return 'min-h-[44px]';
|
2023-11-29 06:47:20 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
canClear() {
|
2023-12-12 19:17:38 +08:00
|
|
|
return this.clearable && this.label && this.isOpen && !this.tagsView;
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
|
|
|
rawOptions() {
|
|
|
|
if (this.optionsUrl) {
|
2023-12-11 16:18:22 +08:00
|
|
|
return this.fetchedOptions;
|
2023-11-29 06:47:20 +08:00
|
|
|
}
|
2023-12-11 16:18:22 +08:00
|
|
|
return this.options;
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
|
|
|
filteredOptions() {
|
2023-12-11 16:18:22 +08:00
|
|
|
if (this.query.length > 0 && !this.optionsUrl) {
|
|
|
|
return this.rawOptions.filter((option) => (
|
|
|
|
option[1].toLowerCase().includes(this.query.toLowerCase())
|
|
|
|
));
|
2023-11-29 06:47:20 +08:00
|
|
|
}
|
2023-12-11 16:18:22 +08:00
|
|
|
return this.rawOptions;
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
|
|
|
label() {
|
|
|
|
if (this.multiple) {
|
2023-12-11 16:18:22 +08:00
|
|
|
return this.multipleLabel;
|
2023-11-29 06:47:20 +08:00
|
|
|
}
|
2023-12-11 16:18:22 +08:00
|
|
|
return this.singleLabel;
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
2023-12-12 19:17:38 +08:00
|
|
|
tags() {
|
|
|
|
if (!this.newValue) return [];
|
2023-11-29 06:47:20 +08:00
|
|
|
|
2024-03-26 22:07:23 +08:00
|
|
|
this.selectAllState = 'indeterminate';
|
|
|
|
if (this.newValue.length === 0) {
|
|
|
|
this.selectAllState = 'unchecked';
|
|
|
|
} else if (this.newValue.length === this.rawOptions.length) {
|
|
|
|
this.selectAllState = 'checked';
|
|
|
|
}
|
|
|
|
|
2023-12-12 19:17:38 +08:00
|
|
|
return this.newValue.map((value) => {
|
|
|
|
const option = this.rawOptions.find((i) => i[0] === value);
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
label: this.renderLabel(option)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
2023-11-29 06:47:20 +08:00
|
|
|
singleLabel() {
|
2023-12-11 16:18:22 +08:00
|
|
|
const option = this.rawOptions.find((i) => i[0] === this.newValue);
|
|
|
|
return this.renderLabel(option);
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
|
|
|
multipleLabel() {
|
|
|
|
if (!this.newValue) return false;
|
|
|
|
|
2023-11-29 18:36:44 +08:00
|
|
|
this.selectAllState = 'unchecked';
|
2023-11-29 06:47:20 +08:00
|
|
|
|
|
|
|
if (this.newValue.length === 0) {
|
|
|
|
return false;
|
2023-12-11 16:18:22 +08:00
|
|
|
}
|
|
|
|
if (this.newValue.length === 1) {
|
|
|
|
this.selectAllState = 'indeterminate';
|
|
|
|
return this.renderLabel(this.rawOptions.find((option) => option[0] === this.newValue[0]));
|
|
|
|
}
|
|
|
|
if (this.newValue.length === this.rawOptions.length) {
|
2023-11-29 06:47:20 +08:00
|
|
|
this.selectAllState = 'checked';
|
2023-12-11 16:18:22 +08:00
|
|
|
return this.allOptionsPlaceholder || this.i18n.t('general.select_dropdown.all_options_placeholder');
|
2023-11-29 06:47:20 +08:00
|
|
|
}
|
2023-12-11 16:18:22 +08:00
|
|
|
this.selectAllState = 'indeterminate';
|
|
|
|
return `${this.newValue.length} ${
|
|
|
|
this.fewOptionsPlaceholder || this.i18n.t('general.select_dropdown.few_options_placeholder')
|
|
|
|
}`;
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
2023-12-01 07:01:08 +08:00
|
|
|
valueChanged() {
|
|
|
|
if (this.multiple) {
|
2023-12-11 16:18:22 +08:00
|
|
|
return !this.compareArrays(this.newValue, this.value);
|
2023-12-01 07:01:08 +08:00
|
|
|
}
|
2023-12-11 16:18:22 +08:00
|
|
|
return this.newValue !== this.value;
|
|
|
|
},
|
2023-12-12 19:17:38 +08:00
|
|
|
searchInputSize() {
|
|
|
|
let characterCount = 10;
|
|
|
|
|
|
|
|
if (this.tags.length === 0) {
|
|
|
|
characterCount = (this.placeholder || this.i18n.t('general.select_dropdown.placeholder')).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.query.length > 0) {
|
|
|
|
characterCount = this.query.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${(characterCount * 8) + 16}px`;
|
|
|
|
}
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.newValue = this.value;
|
|
|
|
if (!this.newValue && this.multiple) {
|
2023-12-11 16:18:22 +08:00
|
|
|
this.newValue = [];
|
2023-11-29 06:47:20 +08:00
|
|
|
}
|
|
|
|
this.fetchOptions();
|
|
|
|
},
|
|
|
|
watch: {
|
2024-02-19 22:12:37 +08:00
|
|
|
value(newValue) {
|
|
|
|
this.newValue = newValue;
|
|
|
|
},
|
2024-04-19 22:28:00 +08:00
|
|
|
isOpen(newVal) {
|
|
|
|
this.$emit('isOpen', newVal);
|
2023-11-29 06:47:20 +08:00
|
|
|
if (this.isOpen) {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.setPosition();
|
|
|
|
this.$refs.search?.focus();
|
2023-12-11 16:18:22 +08:00
|
|
|
});
|
2023-11-29 06:47:20 +08:00
|
|
|
}
|
2024-06-06 17:04:41 +08:00
|
|
|
},
|
2024-06-18 21:52:10 +08:00
|
|
|
urlParams: {
|
|
|
|
handler(oldVal, newVal) {
|
|
|
|
if (!this.compareObjects(oldVal, newVal)) {
|
|
|
|
this.fetchOptions();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
deep: true
|
2024-03-06 00:16:09 +08:00
|
|
|
}
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
renderLabel(option) {
|
|
|
|
if (!option) return false;
|
|
|
|
|
|
|
|
if (this.labelRenderer) {
|
2023-12-11 16:18:22 +08:00
|
|
|
return this.labelRenderer(option);
|
2023-11-29 06:47:20 +08:00
|
|
|
}
|
2023-12-11 16:18:22 +08:00
|
|
|
return option[1];
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
|
|
|
valueSelected(value) {
|
|
|
|
if (!this.newValue) return false;
|
|
|
|
if (this.multiple) {
|
|
|
|
return this.newValue.includes(value);
|
|
|
|
}
|
2023-12-11 16:18:22 +08:00
|
|
|
return this.newValue === value;
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
|
|
|
open() {
|
2023-12-11 16:18:22 +08:00
|
|
|
if (!this.disabled) this.isOpen = true;
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
|
|
|
clear() {
|
2023-11-29 18:36:44 +08:00
|
|
|
this.newValue = this.multiple ? [] : null;
|
2023-11-29 06:47:20 +08:00
|
|
|
this.query = '';
|
2024-04-19 22:28:00 +08:00
|
|
|
this.$emit('change', this.newValue, '');
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
2023-12-11 16:18:22 +08:00
|
|
|
close(e) {
|
2024-01-09 01:33:28 +08:00
|
|
|
if (e && e.target.closest('.sn-select-dropdown')) return;
|
2023-12-11 16:18:22 +08:00
|
|
|
|
2023-12-01 07:01:08 +08:00
|
|
|
if (!this.isOpen) return;
|
|
|
|
|
2023-12-11 16:18:22 +08:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.isOpen = false;
|
|
|
|
if (this.valueChanged) {
|
2024-05-06 18:42:19 +08:00
|
|
|
this.$emit('change', this.newValue, this.getLabels(this.newValue));
|
2023-12-11 16:18:22 +08:00
|
|
|
}
|
|
|
|
this.query = '';
|
|
|
|
});
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
|
|
|
setValue(value) {
|
2023-12-11 16:18:22 +08:00
|
|
|
if (this.multiple) {
|
2023-11-29 06:47:20 +08:00
|
|
|
if (this.newValue.includes(value)) {
|
2023-12-11 16:18:22 +08:00
|
|
|
this.newValue = this.newValue.filter((v) => v !== value);
|
2023-11-29 06:47:20 +08:00
|
|
|
} else {
|
2024-03-06 00:16:09 +08:00
|
|
|
this.newValue = [...this.newValue, value];
|
2023-11-29 06:47:20 +08:00
|
|
|
}
|
|
|
|
} else {
|
2023-12-11 16:18:22 +08:00
|
|
|
this.newValue = value;
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.close();
|
|
|
|
});
|
2023-11-29 06:47:20 +08:00
|
|
|
}
|
|
|
|
},
|
2023-12-12 19:17:38 +08:00
|
|
|
removeTag(value) {
|
|
|
|
this.newValue = this.newValue.filter((v) => v !== value);
|
2024-05-06 18:42:19 +08:00
|
|
|
this.$emit('change', this.newValue, this.getLabels(this.newValue));
|
2023-12-12 19:17:38 +08:00
|
|
|
},
|
2023-11-29 06:47:20 +08:00
|
|
|
selectAll() {
|
|
|
|
if (this.selectAllState === 'checked') {
|
2023-12-11 16:18:22 +08:00
|
|
|
this.newValue = [];
|
2023-11-29 06:47:20 +08:00
|
|
|
} else {
|
2023-12-11 16:18:22 +08:00
|
|
|
this.newValue = this.rawOptions.map((option) => option[0]);
|
2023-11-29 06:47:20 +08:00
|
|
|
}
|
2024-05-06 18:42:19 +08:00
|
|
|
this.$emit('change', this.newValue, this.getLabels(this.newValue));
|
|
|
|
},
|
|
|
|
getLabels(value) {
|
|
|
|
if (typeof value === 'string' || typeof value === 'number') {
|
|
|
|
const option = this.rawOptions.find((i) => i[0] === value);
|
|
|
|
return option[1];
|
|
|
|
}
|
|
|
|
return this.rawOptions.filter((option) => value.includes(option[0])).map((option) => option[1]);
|
2023-11-29 06:47:20 +08:00
|
|
|
},
|
|
|
|
fetchOptions() {
|
|
|
|
if (this.optionsUrl) {
|
2024-01-11 02:56:01 +08:00
|
|
|
const params = { query: this.query, ...this.urlParams };
|
|
|
|
axios.get(this.optionsUrl, { params })
|
|
|
|
.then((response) => {
|
|
|
|
this.fetchedOptions = response.data.data;
|
2023-11-29 06:47:20 +08:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.setPosition();
|
2023-12-11 16:18:22 +08:00
|
|
|
});
|
|
|
|
});
|
2023-11-29 06:47:20 +08:00
|
|
|
}
|
2023-12-01 07:01:08 +08:00
|
|
|
},
|
|
|
|
compareArrays(arr1, arr2) {
|
|
|
|
if (!arr1 || !arr2) return false;
|
|
|
|
if (arr1.length !== arr2.length) return false;
|
|
|
|
|
2023-12-11 16:18:22 +08:00
|
|
|
for (let i = 0; i < arr1.length; i += 1) {
|
2023-12-01 07:01:08 +08:00
|
|
|
if (!arr2.includes(arr1[i])) return false;
|
|
|
|
}
|
|
|
|
return true;
|
2023-12-11 16:18:22 +08:00
|
|
|
},
|
2024-03-05 20:29:21 +08:00
|
|
|
keySelectOptions(e) {
|
2024-03-12 22:39:56 +08:00
|
|
|
if (e.key === 'Tab') this.close();
|
|
|
|
if (['ArrowDown', 'ArrowUp', 'Enter'].some((key) => e.key === key)) {
|
|
|
|
if (e.key === 'ArrowDown') {
|
|
|
|
this.focusedOption = this.focusedOption === null ? 0 : this.focusedOption + 1;
|
|
|
|
if (this.focusedOption > this.$refs.options.length - 1) this.focusedOption = 0;
|
|
|
|
} else if (e.key === 'ArrowUp') {
|
|
|
|
this.focusedOption = (this.focusedOption || this.$refs.options.length) - 1;
|
|
|
|
if (this.focusedOption < 0) this.focusedOption = this.$refs.options.length - 1;
|
|
|
|
} else if (e.key === 'Enter' && this.focusedOption !== null) {
|
|
|
|
this.setValue(this.filteredOptions[this.focusedOption][0]);
|
|
|
|
}
|
2024-03-05 20:29:21 +08:00
|
|
|
}
|
2024-03-12 22:39:56 +08:00
|
|
|
if (this.$refs.options) {
|
|
|
|
this.$refs.options[this.focusedOption]?.scrollIntoView({ block: 'nearest' });
|
2024-03-05 20:29:21 +08:00
|
|
|
}
|
2024-06-18 21:52:10 +08:00
|
|
|
},
|
|
|
|
compareObjects(o1, o2) {
|
|
|
|
const normalizedObj1 = Object.fromEntries(Object.entries(o1).sort(([k1], [k2]) => k1.localeCompare(k2)));
|
|
|
|
const normalizedObj2 = Object.fromEntries(Object.entries(o2).sort(([k1], [k2]) => k1.localeCompare(k2)));
|
|
|
|
return JSON.stringify(normalizedObj1) === JSON.stringify(normalizedObj2);
|
2024-03-05 20:29:21 +08:00
|
|
|
}
|
|
|
|
}
|
2023-12-11 16:18:22 +08:00
|
|
|
};
|
2023-11-29 06:47:20 +08:00
|
|
|
</script>
|