@props([ 'id' => null, 'htmlId' => null, 'label' => null, 'helper' => null, 'required' => false, 'options' => [], // list of ['value' => ..., 'label' => ..., 'description' => ?string, 'disabled' => bool] 'placeholder' => 'Select…', 'emptyText' => 'No options available.', 'live' => false, 'onChange' => null, // optional $wire method to call after a selection 'onChangeArgs' => null, // optional arguments followed by the selected value 'wire' => true, // false = purely client-side value (no Livewire binding) 'value' => null, // initial value when wire=false 'disabled' => false, 'tooltip' => true, 'portal' => false, 'preserveValue' => false, 'canGate' => null, 'canResource' => null, 'autoDisable' => true, 'multiple' => false, // true = the bound value is an array of option values 'searchable' => false, // true = a search field filters the options 'searchPlaceholder' => 'Search…', 'searchEmptyText' => 'No matching options', 'allowCustom' => false, // true (with searchable) = the search text can be chosen as a custom value ]) @php if ($canGate && $canResource && $autoDisable && ! Illuminate\Support\Facades\Gate::allows($canGate, $canResource)) { $disabled = true; } $triggerId = ($htmlId ?? $id).'-trigger'; $panelId = ($htmlId ?? $id).'-panel'; // A multiple listbox also shows item errors, for example a rule on `ids.*`. $errorMessage = $wire && $id && isset($errors) ? ($errors->first($id) ?: ($multiple ? $errors->first($id.'.*') : null)) : null; if ($multiple) { // Server-rendered trigger text; Alpine keeps it current after init. if (! $wire) { $value = is_array($value) ? array_values($value) : []; } $selectedValues = $wire ? data_get($__livewire ?? null, $id) : $value; $selectedValues = is_array($selectedValues) ? array_map('strval', $selectedValues) : []; $selectedLabels = collect($options) ->filter(fn ($option) => empty($option['header']) && in_array((string) $option['value'], $selectedValues, true)) ->pluck('label'); $multipleLabel = match (true) { $selectedLabels->isEmpty() => $placeholder, $selectedLabels->count() <= 2 => $selectedLabels->implode(', '), default => $selectedLabels->count().' selected', }; } @endphp