Skip to content

Commit

Permalink
Set reorder th attributes (#1811)
Browse files Browse the repository at this point in the history
* Initial Commit

* Add Docs

* Update generic styling doc

* Add test for setReorderThAttributes


---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Aug 3, 2024
1 parent 102f3a2 commit 44ca978
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/datatable/styling.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public function configure(): void
Set a list of attributes to override on the th elements.

Note: If you are using Bulk Actions, then the th for Bulk Actions is [styled separately](../bulk-actions/customisations).
Note: If you are using Reorder, then the th for Reorder is [styled separately](../reordering/available-methods).

```php
public function configure(): void
Expand Down
16 changes: 16 additions & 0 deletions docs/reordering/available-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,19 @@ public function configure(): void
$this->setDefaultReorderSort('order', 'desc');
}
```


## setReorderThAttributes

You may pass an array to this method, which allows you to pass Custom Attributes into the table header for the Reorder Column

```php
public function configure(): void
{

$this->setReorderThAttributes([
'class' => 'bg-red-500',
'default' => false
]);
}
```
11 changes: 9 additions & 2 deletions resources/views/components/table/th/reorder.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
@aware(['component', 'tableName'])
@aware(['tableName'])

<x-livewire-tables::table.th.plain x-cloak x-show="currentlyReorderingStatus" wire:key="{{ $tableName }}-thead-reorder" :displayMinimisedOnReorder="false">
<x-livewire-tables::table.th.plain x-cloak x-show="currentlyReorderingStatus" wire:key="{{ $tableName }}-thead-reorder" :displayMinimisedOnReorder="false" {{
$attributes->merge($this->getReorderThAttributes())->class([
'table-cell px-3 py-2 md:px-6 md:py-3 text-center md:text-left bg-gray-50 dark:bg-gray-800 laravel-livewire-tables-reorderingMinimised' => ($this->isTailwind()) && ($this->getReorderThAttributes['default'] ?? true),
'laravel-livewire-tables-reorderingMinimised' => ($this->isBootstrap()) && ($this->getReorderThAttributes['default'] ?? true),
])
}}
>
<div x-cloak x-show="currentlyReorderingStatus"></div>
</x-livewire-tables::table.th.plain>

10 changes: 10 additions & 0 deletions src/Traits/Configuration/ReorderingConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,14 @@ public function setDefaultReorderSort(string $field, string $direction = 'asc'):

return $this;
}

/**
* Used to set attributes for the <th> for Reorder Column
*/
public function setReorderThAttributes(array $reorderThAttributes): self
{
$this->reorderThAttributes = [...$this->reorderThAttributes, ...$reorderThAttributes];

return $this;
}
}
11 changes: 11 additions & 0 deletions src/Traits/Helpers/ReorderingHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,15 @@ public function getReorderingBackupSessionKey(): string
{
return $this->getTableName().'-reordering-backup';
}

/**
* Used to get attributes for the <th> for Bulk Actions
*
* @return array<mixed>
*/
#[Computed]
public function getReorderThAttributes(): array
{
return $this->reorderThAttributes ?? ['default' => true];
}
}
2 changes: 2 additions & 0 deletions src/Traits/WithReordering.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ trait WithReordering

public array $orderedItems = [];

protected array $reorderThAttributes = ['default' => true];

public function setupReordering(): void
{
if ($this->reorderIsDisabled()) {
Expand Down
18 changes: 18 additions & 0 deletions tests/Traits/Configuration/ReorderingConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,22 @@ public function test_can_set_default_reorder_column_and_direction(): void
$this->assertSame('sort2', $this->basicTable->getDefaultReorderColumn());
$this->assertSame('desc', $this->basicTable->getDefaultReorderDirection());
}

public function test_can_set_reorder_th_attributes(): void
{
$this->assertSame(['default' => true], $this->basicTable->getReorderThAttributes());

$this->basicTable->setReorderThAttributes(['class' => 'bg-blue-500']);

$this->assertSame(['default' => true, 'class' => 'bg-blue-500'], $this->basicTable->getReorderThAttributes());

$this->basicTable->setReorderThAttributes(['class' => 'bg-red-500', 'default' => false]);

$this->assertSame(['default' => false, 'class' => 'bg-red-500'], $this->basicTable->getReorderThAttributes());

$this->basicTable->setReorderThAttributes(['style' => 'font:black', 'default' => true]);

$this->assertSame(['default' => true, 'class' => 'bg-red-500', 'style' => 'font:black'], $this->basicTable->getReorderThAttributes());

}
}

0 comments on commit 44ca978

Please sign in to comment.