Skip to content

Instantly share code, notes, and snippets.

@sprobejames
Created September 12, 2024 05:52
Show Gist options
  • Select an option

  • Save sprobejames/92634680a6b54b3dbf62c8389c9eb2ff to your computer and use it in GitHub Desktop.

Select an option

Save sprobejames/92634680a6b54b3dbf62c8389c9eb2ff to your computer and use it in GitHub Desktop.
<?php
<x-multi-select
model="some_public_property"
class="select-custom"
placeholderValue="Select all you want"
:options="[['label' => 'FOO', 'value' => 'foo'], ['label' => 'BAR', 'value' => 'bar']]"
/>
<?php
@props(['options' => [], 'placeholderValue' => '', 'model'])
@php
$uniqId = 'select' . uniqid();
@endphp
<div wire:ignore
x-data
x-init="
$nextTick(() => {
const choices = new Choices($refs.{{ $uniqId }}, {
removeItems: true,
removeItemButton: true,
placeholderValue: '{{ $placeholderValue }}',
})
})"
>
<select
x-ref="{{ $uniqId }}"
wire:change="$set('{{ $model }}', [...$event.target.options].filter(option => option.selected).map(option => option.value))"
{{ $attributes }}
multiple
>
@foreach($options as $option)
<option value="{{ $option['value'] }}">{{ $option['label'] }}</option>
@endforeach
</select>
</div>
@pushOnce('styles')
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/choices.js/public/assets/styles/choices.min.css"/>
@endPushOnce
@pushOnce('scripts')
<script src="https://cdn.jsdelivr.net/npm/choices.js/public/assets/scripts/choices.min.js"></script>
@endPushOnce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment