Hello,
I'm trying to write my own custom select options field in a blade component with AlpineJS.
I have an error : items.find is not a function. It comes from items: '{{ $items }}' but I don't know what to do else (@json($items) also gives me an error).
Do you have any idea how to solve this error ?
Beyond the error, how can I improve this code ? I'm sure it can probably be better. Perhaps by using Livewire + AlpineJS ?
Thanks for your help.
V
@props([
'label',
'field',
'optionLabel' => 'name',
'optionId' => 'id',
'model',
'nullOptionLabel',
'items',
])
<div
x-data="{
show: false,
items: '{{ $items }}',
selectedOptionId: '{{ $model->$field }}',
selectedOptionLabel: '',
selectOption(id, label) {
this.selectedOptionId = id;
this.selectedOptionLabel = label;
this.show = false;
}
}"
x-init="
selectedOptionLabel = selectedOptionId > 0 ? items.find(item => item.id == selectedOptionId).name : '{{ $nullOptionLabel }}';
"
x-on:click.outside="show = false"
class="flex flex-col gap-1 select-none"
>
<label class="text-gray-500" for="{{ $field }}">{{ $label }}</label>
<div class="relative cursor-pointer">
<input type="hidden" :value="selectedOptionId">
<div x-on:click="show = !show" class="flex justify-between items-center px-2 py-1 bg-gray-100 border border-x border-gray-900 rounded-t" :class="{ 'rounded-b': !show }">
<span x-text="selectedOptionLabel"></span>
<span class="text-sm"><i class="fa-solid fa-chevron-down"></i></span>
</div>
<template x-if="true">
<ul x-transition x-show="show" class="absolute w-full z-10 bg-gray-100 border-x border-b border-gray-900 rounded-b max-h-48 overflow-y-auto">
@foreach ($items as $item)
<li x-on:click="selectOption({{ $item->$optionId }}, '{{ $item->$optionLabel }}')" class="px-2 py-1 last:rounded-b hover:bg-green-200">{{ $item->$optionLabel }}</li>
@endforeach
</ul>
</template>
</div>