2023-05-03 18:37:14 +08:00
|
|
|
<template>
|
|
|
|
<div @click="toggle" ref="container" class="sn-select" :class="{ 'sn-select--open': isOpen, 'sn-select--blank': !valueLabel, 'disabled': disabled }">
|
|
|
|
<slot>
|
|
|
|
<button ref="focusElement" class="sn-select__value">
|
|
|
|
<span>{{ valueLabel || (placeholder || i18n.t('general.select')) }}</span>
|
|
|
|
</button>
|
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
|
|
|
</slot>
|
2023-06-13 17:51:00 +08:00
|
|
|
<perfect-scrollbar
|
|
|
|
ref="optionsContainer"
|
|
|
|
:style="optionPositionStyle"
|
|
|
|
class="sn-select__options scroll-container"
|
|
|
|
>
|
2023-05-24 16:47:27 +08:00
|
|
|
<template v-if="options.length">
|
|
|
|
<div
|
|
|
|
v-for="option in options"
|
2023-06-14 20:57:58 +08:00
|
|
|
:key="option[0]" @mousedown.prevent.stop="setValue(option[0])"
|
2023-05-24 16:47:27 +08:00
|
|
|
class="sn-select__option"
|
|
|
|
>
|
|
|
|
{{ option[1] }}
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<template v-else>
|
|
|
|
<div
|
|
|
|
class="sn-select__no-options"
|
|
|
|
>
|
|
|
|
{{ this.noOptionsPlaceholder }}
|
|
|
|
</div>
|
2023-06-13 17:51:00 +08:00
|
|
|
</template>
|
|
|
|
</perfect-scrollbar>
|
2023-05-03 18:37:14 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<script>
|
2023-06-13 17:51:00 +08:00
|
|
|
import PerfectScrollbar from 'vue2-perfect-scrollbar';
|
|
|
|
|
2023-05-03 18:37:14 +08:00
|
|
|
export default {
|
|
|
|
name: 'Select',
|
|
|
|
props: {
|
2023-05-18 01:01:16 +08:00
|
|
|
value: { type: [String, Number] },
|
2023-05-03 18:37:14 +08:00
|
|
|
options: { type: Array, default: () => [] },
|
|
|
|
initialValue: { type: [String, Number] },
|
|
|
|
placeholder: { type: String },
|
2023-05-24 16:47:27 +08:00
|
|
|
noOptionsPlaceholder: { type: String },
|
2023-05-03 18:37:14 +08:00
|
|
|
disabled: { type: Boolean, default: false }
|
|
|
|
},
|
2023-06-13 17:51:00 +08:00
|
|
|
comments: { PerfectScrollbar },
|
2023-05-03 18:37:14 +08:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
isOpen: false,
|
2023-06-06 21:31:40 +08:00
|
|
|
optionPositionStyle: '',
|
|
|
|
blurPrevented: false
|
2023-05-03 18:37:14 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
valueLabel() {
|
|
|
|
let option = this.options.find((o) => o[0] === this.value);
|
|
|
|
return option && option[1];
|
|
|
|
},
|
|
|
|
focusElement() {
|
|
|
|
return this.$refs.focusElement || this.$scopedSlots.default()[0].context.$refs.focusElement;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.focusElement.onblur = this.blur;
|
|
|
|
document.addEventListener("scroll", this.updateOptionPosition);
|
|
|
|
},
|
|
|
|
methods: {
|
2023-06-06 21:31:40 +08:00
|
|
|
preventBlur() {
|
|
|
|
this.blurPrevented = true;
|
|
|
|
},
|
|
|
|
allowBlur() {
|
|
|
|
setTimeout(() => { this.blurPrevented = false }, 200);
|
|
|
|
},
|
2023-05-03 18:37:14 +08:00
|
|
|
blur() {
|
|
|
|
setTimeout(() => {
|
2023-06-06 21:31:40 +08:00
|
|
|
if (this.blurPrevented) {
|
|
|
|
this.focusElement.focus();
|
|
|
|
} else {
|
|
|
|
this.isOpen = false;
|
|
|
|
this.$emit('blur');
|
|
|
|
}
|
2023-06-07 15:52:57 +08:00
|
|
|
}, 100);
|
2023-05-03 18:37:14 +08:00
|
|
|
},
|
|
|
|
toggle() {
|
2023-06-06 21:31:40 +08:00
|
|
|
if (this.isOpen && this.blurPrevented) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-03 18:37:14 +08:00
|
|
|
this.isOpen = !this.isOpen;
|
|
|
|
|
|
|
|
if (this.isOpen) {
|
|
|
|
this.$emit('open');
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.focusElement.focus();
|
|
|
|
});
|
|
|
|
this.$refs.optionsContainer.scrollTop = 0;
|
|
|
|
this.updateOptionPosition();
|
2023-06-07 15:52:57 +08:00
|
|
|
this.setUpBlurHandlers();
|
2023-05-03 18:37:14 +08:00
|
|
|
} else {
|
|
|
|
this.optionPositionStyle = '';
|
|
|
|
this.$emit('close');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setValue(value) {
|
2023-05-18 01:01:16 +08:00
|
|
|
this.$emit('change', value);
|
2023-05-03 18:37:14 +08:00
|
|
|
},
|
|
|
|
updateOptionPosition() {
|
2023-05-26 17:41:47 +08:00
|
|
|
const container = $(this.$refs.container);
|
|
|
|
const rect = container.get(0).getBoundingClientRect();
|
2023-05-08 17:05:22 +08:00
|
|
|
let width = rect.width;
|
|
|
|
let top = rect.top + rect.height;
|
|
|
|
let left = rect.left;
|
2023-05-03 18:37:14 +08:00
|
|
|
|
2023-05-26 17:41:47 +08:00
|
|
|
const modal = container.parents('.modal-content');
|
2023-05-08 17:05:22 +08:00
|
|
|
|
|
|
|
if (modal.length > 0) {
|
|
|
|
const modalRect = modal.get(0).getBoundingClientRect();
|
|
|
|
top -= modalRect.top;
|
|
|
|
left -= modalRect.left;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.optionPositionStyle = `position: fixed; top: ${top}px; left: ${left}px; width: ${width}px`
|
2023-06-07 15:52:57 +08:00
|
|
|
},
|
|
|
|
setUpBlurHandlers() {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.$refs.optionsContainer.$el.querySelector('.ps__thumb-y').addEventListener('mousedown', this.preventBlur);
|
|
|
|
this.$refs.optionsContainer.$el.querySelector('.ps__thumb-y').addEventListener('mouseup', this.allowBlur);
|
|
|
|
document.addEventListener('mouseup', this.allowBlur);
|
|
|
|
}, 100);
|
2023-05-03 18:37:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|