Skip to content

Commit

Permalink
Use Core Attribute Bag (#1916)
Browse files Browse the repository at this point in the history
* Initial Commit

* Fix styling

* Add Missing Test

* Fix styling

* Adjustments

* Fix styling

* Adjust Test

* Fix styling

* Fix Sorting Visual Test

---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Sep 1, 2024
1 parent 2ebaf3b commit 2322e8d
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 17 deletions.
47 changes: 34 additions & 13 deletions resources/js/laravel-livewire-tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

document.addEventListener('alpine:init', () => {

Alpine.data('laravellivewiretable', (wire, showBulkActionsAlpine, tableID, primaryKeyName) => ({
Alpine.data('laravellivewiretable', (wire) => ({
tableId: '',
showBulkActionsAlpine: false,
primaryKeyName: '',
shouldBeDisplayed: wire.entangle('shouldBeDisplayed'),
tableName: wire.entangle('tableName'),
dataTableFingerprint: wire.entangle('dataTableFingerprint'),
Expand Down Expand Up @@ -70,7 +73,7 @@ document.addEventListener('alpine:init', () => {
element.classList.remove("laravel-livewire-table-dragging");
let originalPosition = element.rowIndex;
let newPosition = target.rowIndex;
let table = document.getElementById(tableID);
let table = document.getElementById(this.tableId);
let loopStart = originalPosition;
if (event.offsetY > (target.getBoundingClientRect().height / 2)) {
parent.insertBefore(element, target.nextSibling);
Expand Down Expand Up @@ -126,17 +129,17 @@ document.addEventListener('alpine:init', () => {

},
updateOrderedItems() {
let table = document.getElementById(tableID);
let table = document.getElementById(this.tableId);
let orderedRows = [];
for (let i = 1, row; row = table.rows[i]; i++) {
orderedRows.push({ [primaryKeyName]: row.getAttribute('rowpk'), [this.defaultReorderColumn]: i });
orderedRows.push({ [this.primaryKeyName]: row.getAttribute('rowpk'), [this.defaultReorderColumn]: i });
}
wire.storeReorder(orderedRows);
},
setupEvenOddClasses() {
if (this.evenNotInOdd.length === undefined || this.evenNotInOdd.length == 0 || this.oddNotInEven.length === undefined || this.oddNotInEven.length == 0)
{
let tbody = document.getElementById(tableID).getElementsByTagName('tbody')[0];
let tbody = document.getElementById(this.tableId).getElementsByTagName('tbody')[0];
let evenRowClassArray = [];
let oddRowClassArray = [];

Expand All @@ -152,7 +155,7 @@ document.addEventListener('alpine:init', () => {
}
},
toggleSelectAll() {
if (!showBulkActionsAlpine) {
if (!this.showBulkActionsAlpine) {
return;
}

Expand All @@ -171,14 +174,14 @@ document.addEventListener('alpine:init', () => {
}
},
setAllItemsSelected() {
if (!showBulkActionsAlpine) {
if (!this.showBulkActionsAlpine) {
return;
}
this.selectAllStatus = true;
this.selectAllOnPage();
},
setAllSelected() {
if (!showBulkActionsAlpine) {
if (!this.showBulkActionsAlpine) {
return;
}
if (this.delaySelectAll)
Expand All @@ -192,14 +195,14 @@ document.addEventListener('alpine:init', () => {
}
},
clearSelected() {
if (!showBulkActionsAlpine) {
if (!this.showBulkActionsAlpine) {
return;
}
this.selectAllStatus = false;
wire.clearSelected();
},
selectAllOnPage() {
if (!showBulkActionsAlpine) {
if (!this.showBulkActionsAlpine) {
return;
}

Expand All @@ -210,14 +213,32 @@ document.addEventListener('alpine:init', () => {
}
this.selectedItems = [...new Set(tempSelectedItems)];
},
showTable(eventTableName = '', eventTableFingerpint = '')
setTableId(tableId)
{
if ((eventTableName != '' && eventTableName === this.tableName) || (eventTableFingerprint != '' && eventTableFingerpint === this.dataTableFingerprint)) {
this.tableId = tableId;
},
setAlpineBulkActions(showBulkActionsAlpine)
{
this.showBulkActionsAlpine = showBulkActionsAlpine;
},
setPrimaryKeyName(primaryKeyName)
{
this.primaryKeyName = primaryKeyName;
},
showTable(event)
{
let eventTableName = event.detail.tableName ?? '';
let eventTableFingerprint = event.detail.tableFingerpint ?? '';

if (((eventTableName ?? '') != '' && eventTableName === this.tableName) || (eventTableFingerprint != '' && eventTableFingerpint === this.dataTableFingerprint)) {
this.shouldBeDisplayed = true;
}
},
hideTable(eventTableName = '', eventTableFingerpint = '')
hideTable(event)
{
let eventTableName = event.detail.tableName ?? '';
let eventTableFingerprint = event.detail.tableFingerpint ?? '';

if ((eventTableName != '' && eventTableName === this.tableName) || (eventTableFingerprint != '' && eventTableFingerpint === this.dataTableFingerprint)) {
this.shouldBeDisplayed = false;
}
Expand Down
2 changes: 1 addition & 1 deletion resources/js/laravel-livewire-tables.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions resources/views/components/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
{{ $attributes->merge($customAttributes['table'])
->class(['min-w-full divide-y divide-gray-200 dark:divide-none' => $customAttributes['table']['default'] ?? true])
->except('default') }}

>
<thead wire:key="{{ $tableName }}-thead"
{{ $attributes->merge($customAttributes['thead'])
Expand Down
2 changes: 1 addition & 1 deletion resources/views/datatable.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@php($isBootstrap4 = $this->isBootstrap4)
@php($isBootstrap5 = $this->isBootstrap5)

<div x-data="laravellivewiretable($wire, '{{ $this->showBulkActionsDropdownAlpine() }}', '{{ $tableId }}', '{{ $primaryKey }}')" x-cloak x-show="shouldBeDisplayed" x-on:show-table.window="showTable(event.detail.tableName ?? '', event.detail.tableFingerpint ?? '')" x-on:hide-table.window="hideTable(event.detail.tableName ?? '', event.detail.tableFingerpint ?? '')">
<div {{ $this->getTopLevelAttributes() }}>
<x-livewire-tables::wrapper :component="$this" :tableName="$tableName" :$primaryKey :$isTailwind :$isBootstrap :$isBootstrap4 :$isBootstrap5>
@if($this->hasActions && !$this->showActionsInToolbar)
<x-livewire-tables::includes.actions/>
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/Helpers/ComponentHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getTableName(): string
#[Computed]
public function getTableId(): string
{
return $this->getTableAttributes()['id'];
return $this->getTableAttributes()['id'] ?? 'table-'.$this->getTableName();
}

public function isTableNamed(string $name): bool
Expand Down
19 changes: 19 additions & 0 deletions src/Traits/Helpers/TableAttributeHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Rappasoft\LaravelLivewireTables\Traits\Helpers;

use Illuminate\Database\Eloquent\Model;
use Illuminate\View\ComponentAttributeBag;
use Livewire\Attributes\Computed;
use Rappasoft\LaravelLivewireTables\Views\Column;

Expand Down Expand Up @@ -82,4 +83,22 @@ public function getShouldBeDisplayed(): bool
{
return $this->shouldBeDisplayed;
}

public function getTopLevelAttributesArray(): array
{
return [
'x-data' => 'laravellivewiretable($wire)',
'x-init' => "setTableId('".$this->getTableAttributes()['id']."'); setAlpineBulkActions('".$this->showBulkActionsDropdownAlpine()."'); setPrimaryKeyName('".$this->getPrimaryKey()."');",
'x-cloak',
'x-show' => 'shouldBeDisplayed',
'x-on:show-table.window' => 'showTable(event)',
'x-on:hide-table.window' => 'hideTable(event)',
];
}

#[Computed]
public function getTopLevelAttributes(): ComponentAttributeBag
{
return new ComponentAttributeBag($this->getTopLevelAttributesArray());
}
}
29 changes: 29 additions & 0 deletions tests/Traits/Helpers/TableAttributeHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Traits\Helpers;

use Rappasoft\LaravelLivewireTables\Tests\TestCase;

final class TableAttributeHelpersTest extends TestCase
{
public function test_top_level_attributes_match(): void
{
$topLevelAttributesArray = $this->basicTable->getTopLevelAttributesArray();
$this->assertSame('shouldBeDisplayed', $topLevelAttributesArray['x-show']);
$this->assertSame('showTable(event)', $topLevelAttributesArray['x-on:show-table.window']);
$this->assertSame('hideTable(event)', $topLevelAttributesArray['x-on:hide-table.window']);
$this->assertSame('laravellivewiretable($wire)', $topLevelAttributesArray['x-data']);
}

public function test_top_level_attribute_bag_matches(): void
{
$topLevelAttributeBag = $this->basicTable->getTopLevelAttributes();

$topLevelAttributesArray = $topLevelAttributeBag->getAttributes();

$this->assertSame('shouldBeDisplayed', $topLevelAttributesArray['x-show']);
$this->assertSame('showTable(event)', $topLevelAttributesArray['x-on:show-table.window']);
$this->assertSame('hideTable(event)', $topLevelAttributesArray['x-on:hide-table.window']);
$this->assertSame('laravellivewiretable($wire)', $topLevelAttributesArray['x-data']);
}
}
1 change: 0 additions & 1 deletion tests/Traits/Visuals/SortingVisualsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ public function test_multiple_sorting_pill_shows_with_single_column_off(): void
public function test_sorting_pill_shows_correct_name_and_direction(): void
{
Livewire::test(PetsTable::class)
->assertDontSee('Key')
->call('sortBy', 'id')
->assertSee('Key')
->assertSee('0-9')
Expand Down

0 comments on commit 2322e8d

Please sign in to comment.