@props([
'options' => [],
'placeholder' => __('Select'),
'searchPlaceholder' => __('Search'),
'emptyLabel' => __('No results'),
])
@php
$normalizedOptions = collect($options)
->map(fn ($label) => is_scalar($label) ? (string) $label : '')
->all();
@endphp
whereDoesntStartWith('wire:model')->class('relative w-full') }}
x-data="{
open: false,
query: '',
value: @if ($attributes->wire('model')->hasModifier('live')) @entangle($attributes->wire('model')->value()).live @else @entangle($attributes->wire('model')->value()) @endif,
options: @js($normalizedOptions),
panelStyle: '',
matches(id, label) {
const query = this.query.trim().toLowerCase();
const text = String(label ?? '').toLowerCase();
return query === '' || text.includes(query) || String(id).toLowerCase().includes(query);
},
get hasResults() {
return Object.entries(this.options).some(([id, label]) => this.matches(id, label));
},
get selectedLabel() {
const hasValue = this.value !== null && this.value !== undefined && String(this.value) !== '';
return hasValue && this.options[String(this.value)] ? this.options[String(this.value)] : @js($placeholder);
},
positionPanel() {
const button = this.$refs.button;
if (!button) return;
const rect = button.getBoundingClientRect();
const width = rect.width;
const spaceBelow = window.innerHeight - rect.bottom;
const openAbove = spaceBelow < 280 && rect.top > spaceBelow;
const top = openAbove ? Math.max(8, rect.top - 276) : rect.bottom + 4;
const left = Math.min(Math.max(8, rect.left), window.innerWidth - width - 8);
this.panelStyle = `position: fixed; top: ${top}px; left: ${left}px; width: ${width}px;`;
},
choose(id) {
this.value = id;
this.open = false;
this.query = '';
},
clear() {
this.value = '';
this.open = false;
this.query = '';
}
}"
:class="{ 'z-[90]': open }"
@resize.window="if (open) positionPanel()"
@scroll.window="if (open) positionPanel()"
@keydown.escape.window="open = false"
data-no-global-spinner
>
@foreach($normalizedOptions as $id => $label)
@endforeach
{{ $emptyLabel }}