Vue.component('tt-select', { props: { options: { type: Array, required: true }, label: { type: String, default: null }, required: { type: Boolean, default: false }, value: { type: [String, Number, Array], default: null }, disabled: { type: Boolean, default: false }, suffix: { type: String, default: null }, sm: { type: Boolean, default: false }, row: { type: Boolean, default: false }, multiple: { type: Boolean, default: false }, searchable: { type: Boolean, default: true } }, data() { return { open: false, searchQuery: '', // internal model: array if multiple, else primitive selected: this.multiple ? (Array.isArray(this.value) ? [...this.value] : []) : this.value }; }, watch: { // keep internal model in sync with external v-model value(newVal) { this.selected = this.multiple ? (Array.isArray(newVal) ? [...newVal] : []) : newVal; } }, mounted() { document.addEventListener('click', this.onClickOutside); }, beforeDestroy() { document.removeEventListener('click', this.onClickOutside); }, computed: { // normalize all options to { value, text, disabled } normalizedOptions() { return this.options.map(opt => { if (opt !== null && typeof opt === 'object') { return { value: opt.value, text: opt.text, disabled: !!opt.disabled }; } return { value: opt, text: String(opt), disabled: false }; }); }, // filter by searchQuery filteredOptions() { if (!this.searchable || !this.searchQuery) { return this.normalizedOptions; } const q = this.searchQuery.toLowerCase(); return this.normalizedOptions.filter(o => o.text.toLowerCase().includes(q) ); }, // what to show on the button displayText() { if (this.multiple) { const arr = Array.isArray(this.selected) ? this.selected : []; const count = arr.length; if (count === 0) return ''; if (count === 1) { const o = this.normalizedOptions.find(x => x.value === arr[0]); return o ? o.text + (this.suffix ? ' ' + this.suffix : '') : ''; } // more than one selected return `${count} ausgewählt`; } // single select const sel = this.normalizedOptions.find(o => o.value === this.selected); return sel ? sel.text + (this.suffix ? ' ' + this.suffix : '') : ''; } }, methods: { toggleDropdown() { if (this.disabled) return; this.open = !this.open; }, onClickOutside(e) { if (!this.$el.contains(e.target)) { this.open = false; } }, isSelected(opt) { return this.multiple ? Array.isArray(this.selected) && this.selected.includes(opt.value) : this.selected === opt.value; }, selectOption(opt) { if (opt.disabled) return; if (this.multiple) { const arr = Array.isArray(this.selected) ? [...this.selected] : []; const idx = arr.indexOf(opt.value); if (idx > -1) arr.splice(idx, 1); else arr.push(opt.value); this.selected = arr; this.$emit('input', arr); // Fixed: Always emit the array when multiple } else { this.selected = opt.value; this.$emit('input', opt.value); this.open = false; } } }, template: `