Skip to content

Commit 5f839b3

Browse files
committed
Fix BooleanColumn unexpected truthy behaviour
1 parent ff38951 commit 5f839b3

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

resources/js/laravel-livewire-tables.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,44 @@ document.addEventListener('alpine:init', () => {
252252
}));
253253

254254

255-
Alpine.data('booleanFilter', (wire,filterKey,tableName,defaultValue) => ({
255+
Alpine.data('booleanFilter', (filterKey,tableName,defaultValue) => ({
256256
switchOn: false,
257-
value: wire.entangle('filterComponents.'+filterKey).live,
257+
value: false,
258+
toggleStatus()
259+
{
260+
let tempValue = Boolean(Number(this.$wire.get('filterComponents.'+filterKey) ?? this.value));
261+
let newBoolean = !tempValue;
262+
let newValue = Number(newBoolean);
263+
this.value = newBoolean;
264+
this.switchOn = newBoolean;
265+
this.updateStatus(newValue);
266+
},
267+
toggleStatusWithReset()
268+
{
269+
let tempValue = Boolean(Number(this.$wire.get('filterComponents.'+filterKey) ?? this.value));
270+
let newBoolean = !tempValue;
271+
let newValue = Number(newBoolean);
272+
this.value = newBoolean;
273+
this.switchOn = newBoolean;
274+
this.$wire.call('resetFilter',filterKey);
275+
276+
},
277+
updateStatus(newValue)
278+
{
279+
this.$wire.set('filterComponents.'+filterKey, newValue);
280+
},
281+
setSwitchOn(val)
282+
{
283+
let number = Number(val ?? 0);
284+
this.switchOn = Boolean(number);
285+
},
258286
init() {
259-
this.switchOn = false;
260-
if (typeof this.value !== 'undefined') {
261-
this.switchOn = Boolean(Number(this.value));
262-
}
287+
this.$nextTick(() => {
288+
this.value = this.$wire.get('filterComponents.'+filterKey) ?? defaultValue;
289+
let number = Number(this.value ?? 0);
290+
this.switchOn = Boolean(number);
291+
});
292+
263293
this.listeners.push(
264294
Livewire.on('filter-was-set', (detail) => {
265295
if(detail.tableName == tableName && detail.filterKey == filterKey) {
@@ -341,7 +371,7 @@ document.addEventListener('alpine:init', () => {
341371
}));
342372

343373
Alpine.data('flatpickrFilter', (wire, filterKey, filterConfig, refLocation, locale) => ({
344-
wireValues: wire.entangle('filterComponents.' + filterKey),
374+
wireValues: null,
345375
flatpickrInstance: flatpickr(refLocation, {
346376
mode: 'range',
347377
altFormat: filterConfig['altFormat'] ?? "F j, Y",
@@ -386,6 +416,7 @@ document.addEventListener('alpine:init', () => {
386416
}
387417
},
388418
setupWire() {
419+
this.wireValues = this.$wire.get('filterComponents.'+filterKey);
389420
if (this.wireValues !== undefined) {
390421
if (this.wireValues.minDate !== undefined && this.wireValues.maxDate !== undefined) {
391422
let initialDateArray = [this.wireValues.minDate, this.wireValues.maxDate];
@@ -400,7 +431,10 @@ document.addEventListener('alpine:init', () => {
400431
}
401432
},
402433
init() {
403-
this.setupWire();
434+
this.$nextTick(() => {
435+
this.setupWire();
436+
});
437+
404438
this.$watch('wireValues', value => this.setupWire());
405439
}
406440

0 commit comments

Comments
 (0)