2023-05-03 18:37:14 +08:00
|
|
|
<template>
|
2023-05-24 16:47:27 +08:00
|
|
|
<Select
|
2023-11-29 17:59:24 +08:00
|
|
|
class="sn-select sn-select--search hover:border-sn-sleepy-grey"
|
2023-11-26 18:29:04 +08:00
|
|
|
:class="customClass"
|
2023-11-13 21:13:53 +08:00
|
|
|
:className="className"
|
|
|
|
:optionsClassName="optionsClassName"
|
2023-10-24 21:49:37 +08:00
|
|
|
:withEditCursor="withEditCursor"
|
|
|
|
:withClearButton="withClearButton"
|
2023-05-24 16:47:27 +08:00
|
|
|
:value="value"
|
|
|
|
:options="currentOptions"
|
|
|
|
:placeholder="placeholder"
|
2023-06-09 20:20:41 +08:00
|
|
|
:noOptionsPlaceholder="isLoading ? i18n.t('general.loading') : noOptionsPlaceholder"
|
2023-05-24 16:47:27 +08:00
|
|
|
v-bind:disabled="disabled"
|
|
|
|
@change="change"
|
|
|
|
@blur="blur"
|
|
|
|
@open="open"
|
|
|
|
@close="close"
|
2023-11-28 17:40:35 +08:00
|
|
|
@focus="focus"
|
2023-05-24 16:47:27 +08:00
|
|
|
>
|
2023-05-03 18:37:14 +08:00
|
|
|
<input ref="focusElement" v-model="query" type="text" class="sn-select__search-input" :placeholder="searchPlaceholder" />
|
|
|
|
<span class="sn-select__value">{{ valueLabel || (placeholder || i18n.t('general.select')) }}</span>
|
2023-06-13 17:51:00 +08:00
|
|
|
<i class="sn-icon" :class="{ 'sn-icon-down': !isOpen, 'sn-icon-up': isOpen}"></i>
|
2023-05-03 18:37:14 +08:00
|
|
|
</Select>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import Select from './select.vue'
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'SelectSearch',
|
|
|
|
props: {
|
2023-10-24 21:49:37 +08:00
|
|
|
withClearButton: { type: Boolean, default: false },
|
|
|
|
withEditCursor: { type: Boolean, default: false },
|
2023-05-18 01:01:16 +08:00
|
|
|
value: { type: [String, Number] },
|
2023-05-03 18:37:14 +08:00
|
|
|
options: { type: Array, default: () => [] },
|
|
|
|
optionsUrl: { type: String },
|
|
|
|
placeholder: { type: String },
|
2023-05-03 19:39:10 +08:00
|
|
|
searchPlaceholder: { type: String },
|
2023-05-24 16:47:27 +08:00
|
|
|
noOptionsPlaceholder: { type: String },
|
2023-06-09 20:20:41 +08:00
|
|
|
disabled: { type: Boolean },
|
2023-11-13 21:13:53 +08:00
|
|
|
isLoading: { type: Boolean, default: false },
|
|
|
|
className: { type: String, default: '' },
|
2023-11-26 18:29:04 +08:00
|
|
|
optionsClassName: { type: String, default: '' },
|
|
|
|
customClass: { type: String, default: '' }
|
2023-05-03 18:37:14 +08:00
|
|
|
},
|
|
|
|
components: { Select },
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
query: null,
|
|
|
|
currentOptions: null,
|
|
|
|
isOpen: false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
this.currentOptions = this.options;
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
query() {
|
|
|
|
if(!this.query) {
|
|
|
|
this.currentOptions = this.options;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.optionsUrl) {
|
|
|
|
this.fetchOptions();
|
|
|
|
} else {
|
|
|
|
this.currentOptions = this.options.filter((o) => o[1].toLowerCase().includes(this.query.toLowerCase()));
|
|
|
|
}
|
2023-05-05 16:31:35 +08:00
|
|
|
},
|
|
|
|
options() {
|
|
|
|
this.currentOptions = this.options;
|
2023-05-03 18:37:14 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
valueLabel() {
|
2023-05-05 16:31:35 +08:00
|
|
|
let option = this.currentOptions.find((o) => o[0] === this.value);
|
2023-05-03 18:37:14 +08:00
|
|
|
return option && option[1];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2023-11-28 17:40:35 +08:00
|
|
|
focus() {
|
|
|
|
this.$refs.focusElement.focus();
|
|
|
|
},
|
2023-05-03 18:37:14 +08:00
|
|
|
blur() {
|
2023-05-05 16:31:35 +08:00
|
|
|
this.isOpen = false;
|
2023-05-03 18:37:14 +08:00
|
|
|
this.$emit('blur');
|
|
|
|
},
|
|
|
|
change(value) {
|
2023-05-03 19:39:10 +08:00
|
|
|
this.isOpen = false;
|
2023-05-18 01:01:16 +08:00
|
|
|
this.$emit('change', value);
|
2023-05-03 18:37:14 +08:00
|
|
|
},
|
|
|
|
open() {
|
|
|
|
this.isOpen = true;
|
|
|
|
this.$emit('open');
|
|
|
|
},
|
|
|
|
close() {
|
2023-05-18 01:01:16 +08:00
|
|
|
this.query = '';
|
2023-05-03 18:37:14 +08:00
|
|
|
this.isOpen = false;
|
|
|
|
this.$emit('close');
|
|
|
|
},
|
|
|
|
fetchOptions() {
|
2023-05-05 16:31:35 +08:00
|
|
|
$.get(`${this.optionsUrl}?query=${this.query || ''}`,
|
2023-05-03 18:37:14 +08:00
|
|
|
(data) => {
|
|
|
|
this.currentOptions = data;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|