From 4de664d6dc641064800f278d48b5605b720c1f74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20Fie=C3=9Finger?= <41627893+bfiessinger@users.noreply.github.com> Date: Thu, 26 Aug 2021 17:02:25 +0200 Subject: [PATCH 01/12] Add support for multiple attribute - added support for HTML attribute multiple - added new view for multiple select tag --- resources/views/multiple.blade.php | 22 ++++++++++++++++++++++ resources/views/select.blade.php | 21 +++++++++++++++------ src/LivewireSelect.php | 8 ++++++++ 3 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 resources/views/multiple.blade.php diff --git a/resources/views/multiple.blade.php b/resources/views/multiple.blade.php new file mode 100644 index 0000000..5087d2a --- /dev/null +++ b/resources/views/multiple.blade.php @@ -0,0 +1,22 @@ + + +@push('livewire-select-scripts') + +@endpush diff --git a/resources/views/select.blade.php b/resources/views/select.blade.php index 18adc57..02624b9 100644 --- a/resources/views/select.blade.php +++ b/resources/views/select.blade.php @@ -2,12 +2,21 @@
@if(!$searchable && $shouldShow) - @include($defaultView, [ - 'name' => $name, - 'options' => $options, - 'placeholder' => $placeholder, - 'styles' => $styles, - ]) + @if(!$multiple) + @include($defaultView, [ + 'name' => $name, + 'options' => $options, + 'placeholder' => $placeholder, + 'styles' => $styles, + ]) + @else + @include($multipleView, [ + 'name' => $name, + 'options' => $options, + 'placeholder' => $placeholder, + 'styles' => $styles, + ]) + @endif @endif
diff --git a/src/LivewireSelect.php b/src/LivewireSelect.php index 3422f70..5f7ac34 100755 --- a/src/LivewireSelect.php +++ b/src/LivewireSelect.php @@ -37,6 +37,8 @@ class LivewireSelect extends Component public $searchable; public $searchTerm; + public $multiple; + public $dependsOn; public $dependsOnValues; @@ -46,6 +48,7 @@ class LivewireSelect extends Component public $selectView; public $defaultView; + public $multipleView; public $searchView; public $searchInputView; public $searchOptionsContainer; @@ -57,12 +60,14 @@ public function mount($name, $value = null, $placeholder = 'Select an option', $searchable = false, + $multiple = false, $dependsOn = [], $dependsOnValues = [], $waitForDependenciesToShow = false, $noResultsMessage = 'No options found', $selectView = 'livewire-select::select', $defaultView = 'livewire-select::default', + $multipleView = 'livewire-select::multiple', $searchView = 'livewire-select::search', $searchInputView = 'livewire-select::search-input', $searchOptionsContainer = 'livewire-select::search-options-container', @@ -79,6 +84,8 @@ public function mount($name, $this->searchable = $searchable; $this->searchTerm = ''; + $this->multiple = !!$multiple; + $this->dependsOn = $dependsOn; $this->dependsOnValues = collect($this->dependsOn) @@ -97,6 +104,7 @@ public function mount($name, $this->selectView = $selectView; $this->defaultView = $defaultView; + $this->multipleView = $multipleView; $this->searchView = $searchView; $this->searchInputView = $searchInputView; $this->searchOptionsContainer = $searchOptionsContainer; From 995ef1feb96e36112bdbfb5ab02fc4943653b949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20Fie=C3=9Finger?= <41627893+bfiessinger@users.noreply.github.com> Date: Fri, 27 Aug 2021 17:13:36 +0200 Subject: [PATCH 02/12] Update blade directives - add new blade directive livewireSelectStyles for adding custom css - update directive livewireSelectScripts single assets can now be included and excluded by options --- src/LivewireSelect.php | 65 +++++++++++++++++++++++++++ src/LivewireSelectServiceProvider.php | 30 +++---------- 2 files changed, 71 insertions(+), 24 deletions(-) diff --git a/src/LivewireSelect.php b/src/LivewireSelect.php index 5f7ac34..3d2b206 100755 --- a/src/LivewireSelect.php +++ b/src/LivewireSelect.php @@ -230,6 +230,42 @@ public function styles() ]; } + public function css($options = null) { + $assets = [ + + ]; + + return $this->renderAssets($assets, $options); + } + + public function js($options = null) { + $assets = [ + 'livewireSelect' => '', + ]; + + return $this->renderAssets($assets, $options); + } + public function render() { if ($this->searchable) { @@ -262,4 +298,33 @@ public function render() 'styles' => $styles, ]); } + + /** + * Generate a string of required assets + * + * @param array $assets + * @param array $options + * + * @return string + */ + private function renderAssets($assets = [], $options = null) { + if ($options) { + $options = explode(',', $options); + $options[] = 'livewireSelect'; + $assetArray = []; + + foreach ($assets as $asset => $link) { + if (in_array($asset, $options)) { + $assetArray[] = $link; + } + } + } else { + $assetArray = $assets; + } + + $assetStr = implode(PHP_EOL, $assetArray); + return << - window.livewire.on('livewire-select-focus-search', (data) => { - const el = document.getElementById(`${data.name || 'invalid'}`); - - if (!el) { - return; - } - - el.focus(); - }); - - window.livewire.on('livewire-select-focus-selected', (data) => { - const el = document.getElementById(`${data.name || 'invalid'}-selected`); - - if (!el) { - return; - } - - el.focus(); - }); - -HTML; + $livewireSelect = new LivewireSelect; + Blade::directive('livewireSelectScripts', function ($options) use ($livewireSelect) { + return $livewireSelect->js($options); + }); + Blade::directive('livewireSelectStyles', function ($options) use ($livewireSelect) { + return $livewireSelect->css($options); }); } From 2b8b1661e5e319a2a394022cc7612ab38d524b65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20Fie=C3=9Finger?= <41627893+bfiessinger@users.noreply.github.com> Date: Fri, 27 Aug 2021 17:14:53 +0200 Subject: [PATCH 03/12] move from native multiselect to custom alpine js tag input --- resources/views/multiple.blade.php | 108 +++++++++++++++++++++++------ src/LivewireSelect.php | 79 +++++++++++++++++++++ 2 files changed, 167 insertions(+), 20 deletions(-) diff --git a/resources/views/multiple.blade.php b/resources/views/multiple.blade.php index 5087d2a..e3f465c 100644 --- a/resources/views/multiple.blade.php +++ b/resources/views/multiple.blade.php @@ -1,22 +1,90 @@ - + + @foreach($options as $option) + - @endforeach - - -@push('livewire-select-scripts') - -@endpush + @endforeach + +
+
+
+
+
+ +
+ +
+
+
+ + +
+
+
+
+
+
+ +
+
+
+
+
+ diff --git a/src/LivewireSelect.php b/src/LivewireSelect.php index 3d2b206..27de83f 100755 --- a/src/LivewireSelect.php +++ b/src/LivewireSelect.php @@ -261,6 +261,85 @@ public function js($options = null) { el.focus(); }); ', + 'livewireSelectMultiple' => '' ]; return $this->renderAssets($assets, $options); From e3a2ca0993708e0f9df298d109c59652837fc9f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20Fie=C3=9Finger?= <41627893+bfiessinger@users.noreply.github.com> Date: Fri, 27 Aug 2021 17:31:54 +0200 Subject: [PATCH 04/12] remove unused svelte binding --- resources/views/multiple.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/multiple.blade.php b/resources/views/multiple.blade.php index e3f465c..6c1f4f7 100644 --- a/resources/views/multiple.blade.php +++ b/resources/views/multiple.blade.php @@ -45,7 +45,7 @@ class="livewire-select-input {{ $styles['default'] }}" -
+