summaryrefslogtreecommitdiff
path: root/app/src/components/Select.vue
blob: 0f55ccae0832555e83c847a0bedb82e4ac3275fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<template>
  <select ref="$el" />
</template>

<script setup>
import { onMounted, onUnmounted, ref, watch } from 'vue';
import 'select2/dist/js/select2.min';
import $ from 'jquery';

const props = defineProps({
  modelValue: {},

  options: {
    type: Array,
    required: true,
  },

  modelValue: {
    type: [Number, String],
    required: true,
  },

  config: {
    type: Object,
    default: () => ({}),
  },
});
const emit = defineEmits(['update:modelValue']);

const $el = ref(null);

onMounted(() => {
  $($el.value)
    .select2({ ...props.config, data: props.options })
    .val(props.modelValue)
    .trigger('change')
    .on('change', (event) => {
      emit('update:modelValue', event.target.value);
    });
});

onUnmounted(() => {
  $($el.value)
    .off()
    .select2('destroy');
});

watch(() => props.options, (value) => {
  $($el.value)
    .empty().select2({ data: value });
});

watch(() => props.modelValue, (value) => {
  $($el.value)
    .val(value)
    .trigger('change');
});
</script>

<style lang="scss">
@import '~select2/dist/css/select2.min.css';

.select2 {
  &-selection {
    height: 2.4375rem !important;
    border-radius: 0 !important;
    border: 1px solid #cacaca !important;

    &__rendered {
      padding: .5rem;
      font-size: 1rem !important;
      line-height: 1.5 !important;
    }

    &__arrow {
      height: 2.4375rem !important;
      top: 0;
    }
  }
}
</style>