From 41dd0c98635105b343c4f2e9ccce862e5c469c7a Mon Sep 17 00:00:00 2001 From: Bezhan Salleh Date: Thu, 25 Jan 2024 16:16:07 +0430 Subject: [PATCH] fixes select all toggle --- resources/views/forms/shield-toggle.blade.php | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/resources/views/forms/shield-toggle.blade.php b/resources/views/forms/shield-toggle.blade.php index 190a57a..addef5f 100644 --- a/resources/views/forms/shield-toggle.blade.php +++ b/resources/views/forms/shield-toggle.blade.php @@ -16,14 +16,19 @@ state: $wire.{{ $applyStateBindingModifiers("\$entangle('{$statePath}')") }}, checkboxes: [], checkboxLists: [], - toggleAllCheckboxes: function () { + toggleAll: function () { + this.state = ! this.state; + this.checkboxLists.forEach(checkboxList => { + Alpine.$data(checkboxList.parentNode).areAllCheckboxesChecked = false; + Alpine.$data(checkboxList.parentNode).checkIfAllCheckboxesAreChecked() + Alpine.$data(checkboxList.parentNode).updateVisibleCheckboxListOptions(); + Alpine.$data(checkboxList.parentNode).toggleAllCheckboxes(); + }) + this.checkboxes.forEach(checkbox => { checkbox.checked = this.state; }); this.updateStateBasedOnCheckboxes(); - this.checkboxLists.forEach(checkboxList => { - Alpine.$data(checkboxList.parentNode).checkIfAllCheckboxesAreChecked(); - }) }, updateStateBasedOnCheckboxes: function () { @@ -33,18 +38,28 @@ init: function() { this.checkboxLists = Array.from(document.querySelectorAll('.fi-fo-checkbox-list')) this.checkboxes = Array.from(document.querySelectorAll('.fi-fo-checkbox-list-option-label input[type=\'checkbox\']')); + + this.checkboxes.forEach((checkbox) => { checkbox.addEventListener('change', () => { this.updateStateBasedOnCheckboxes(); }); }); + $nextTick(() => { this.updateStateBasedOnCheckboxes(); }); + + $watch('state', (value, old) => { + console.log('new',value,' - old',old) + if (value === old) { + this.toggleAll(); + } + }); } }" x-init="init()" - x-on:click="state = !state; toggleAllCheckboxes();" + x-on:click="toggleAll();" x-bind:class=" state ? '{{ @@ -156,5 +171,4 @@ class="absolute inset-0 flex items-center justify-center w-full h-full transitio @else {{ $content() }} @endif - - + \ No newline at end of file