@props([
// item format: [ 'id' => '', 'title' => '', 'subtitle' => '' ]
'options' => [],
// optional, only use if wire:model is not functioning
// correctly: ['ITEM1','ITEM2','ITEM3']
'selectedItems' => [],
'titleField' => 'title',
'subtitleField' => 'subtitle',
'idField' => 'id'
])
{{--
Github docs: https://gist.github.com/CallumCarmicheal/3bcbfb178443c9a11c673be83530ac8d
--}}
@php
// Get the wire model and convert it into a unique id
$modelId = $attributes->wire('model');
if ($modelId != null) $modelId = str_replace('\'', '', str_replace('\"', '', $modelId));
// Get the id, attribute
$attrId = $attributes->get('id') ?? null;
if ($attrId != null) $attrId = str_replace('\'', '', str_replace('\"', '', $attrId));
$compId = (!isset($attrId) || trim($attrId) === '') ? str_replace('.', '_', $modelId) : $attrId;
@endphp
{{--
{{$attrId}} - {{$modelId}} - {{$compId}}
--}}
{{--
{{!isset($attrId) ? 't' : 'f' }} | {{ trim($attrId) === '' ? 't' : 'f' }} | {{(!isset($attrId) || trim($attrId) === '') ? 't' : 'f'}}
--}}
{{--
{{ str_replace('.', '_', $modelId) }} = {{$compId}}
--}}
wire('model') }}
class="d-none" x-data="{ value: null }" x-modelable="value"
x-init="$watch('value', (v) => {
let tomSelect = $('#{{$compId}}_container').find('select')[0].tomselect;
if (tomSelect.getValue() !== v) {
// Value is different on the server, update our drop-down.
//console.log('Different value: ', tomSelect.getValue(), v);
tomSelect.setValue(v, true); // silent: true (don't trigger $wire.$refresh())
} else {
// Value is the same, no change required.
//console.log('value same: ', tomSelect.getValue(), v);
}
})">