From a0be9a4ab91fc6f049072bf72bfa3565723e7236 Mon Sep 17 00:00:00 2001 From: kristiyan Date: Wed, 17 Sep 2025 11:59:40 +0200 Subject: [PATCH] Add support for closures as values for TdCheckboxAttribute parameter --- .../views/components/forms/checkbox.blade.php | 7 +++- .../table/td/bulk-actions.blade.php | 6 ++- src/Traits/Styling/HasBulkActionsStyling.php | 41 ++++++++++++------- .../BulkActionsStylingConfigurationTest.php | 2 + 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/resources/views/components/forms/checkbox.blade.php b/resources/views/components/forms/checkbox.blade.php index 4b2b9b940..e566470be 100644 --- a/resources/views/components/forms/checkbox.blade.php +++ b/resources/views/components/forms/checkbox.blade.php @@ -1,5 +1,7 @@ @aware(['tableName','primaryKey', 'isTailwind', 'isBootstrap', 'isBootstrap4', 'isBootstrap5']) -@props(['checkboxAttributes']) +@props(['checkboxAttributes', +//'row' +]) merge($checkboxAttributes)->class([ @@ -8,4 +10,5 @@ 'form-check-input' => ($isBootstrap5) && ($checkboxAttributes['default'] ?? true), ])->except(['default','default-styling','default-colors']) }} -/> \ No newline at end of file + {{-- aria-label="select row number {{$row->id}}"--}} +/> diff --git a/resources/views/components/table/td/bulk-actions.blade.php b/resources/views/components/table/td/bulk-actions.blade.php index 1b4049042..0a2e90f20 100644 --- a/resources/views/components/table/td/bulk-actions.blade.php +++ b/resources/views/components/table/td/bulk-actions.blade.php @@ -3,6 +3,7 @@ @php $tdAttributes = $this->getBulkActionsTdAttributes; + // $tdCheckboxAttributes = $this->getBulkActionsTdCheckboxAttributes($row, $rowIndex); $tdCheckboxAttributes = $this->getBulkActionsTdCheckboxAttributes; @endphp @@ -12,9 +13,10 @@ 'inline-flex rounded-md shadow-sm' => $isTailwind, 'form-check' => $isBootstrap5, ])> - diff --git a/src/Traits/Styling/HasBulkActionsStyling.php b/src/Traits/Styling/HasBulkActionsStyling.php index 73cbe6402..d4f611c74 100644 --- a/src/Traits/Styling/HasBulkActionsStyling.php +++ b/src/Traits/Styling/HasBulkActionsStyling.php @@ -2,6 +2,8 @@ namespace Rappasoft\LaravelLivewireTables\Traits\Styling; +use Closure; +use Illuminate\Database\Eloquent\Model; use Illuminate\View\ComponentAttributeBag; use Livewire\Attributes\Computed; @@ -15,7 +17,7 @@ trait HasBulkActionsStyling protected array $bulkActionsTdAttributes = ['default' => null, 'default-colors' => null, 'default-styling' => null]; - protected array $bulkActionsTdCheckboxAttributes = ['default' => null, 'default-colors' => null, 'default-styling' => null]; + protected array|Closure $bulkActionsTdCheckboxAttributes = ['default' => null, 'default-colors' => null, 'default-styling' => null]; protected array $bulkActionsButtonAttributes = ['default-colors' => true, 'default-styling' => true]; @@ -105,22 +107,32 @@ public function getBulkActionsTdAttributes(): array } /** - * Used to get attributes for the Bulk Actions TD + * Used to get attributes for the Bulk Actions TD. + * The parameters are nullable so that the method can be called without any arguments. (Backward compatibility) * * @return array */ #[Computed] - public function getBulkActionsTdCheckboxAttributes(): array + public function getBulkActionsTdCheckboxAttributes(Model $row = null, int $index = null): array { - return array_merge( - [ - 'x-show' => '!currentlyReorderingStatus', - 'x-model' => 'selectedItems', - 'wire:loading.attr.delay' => 'disabled', - 'type' => 'checkbox', - ], - $this->getCustomAttributesNew('bulkActionsTdCheckboxAttributes', true, true) - ); + $defaultAttributes = [ + 'x-show' => '!currentlyReorderingStatus', + 'x-model' => 'selectedItems', + 'wire:loading.attr.delay' => 'disabled', + 'type' => 'checkbox', + ]; + + if (is_callable($this->bulkActionsTdCheckboxAttributes)) { + $customAttributes = ($this->bulkActionsTdCheckboxAttributes)($row, $index); + return array_merge($defaultAttributes, $customAttributes ?: []); + } + + if (is_array($this->bulkActionsTdCheckboxAttributes)) { + return array_merge($defaultAttributes, + $this->getCustomAttributesNew('bulkActionsTdCheckboxAttributes', true, true)); + } + + return $defaultAttributes; } /** @@ -177,9 +189,10 @@ public function setBulkActionsTdAttributes(array $bulkActionsTdAttributes): self /** * Used to set attributes for the Bulk Actions Checkbox in the Row */ - public function setBulkActionsTdCheckboxAttributes(array $bulkActionsTdCheckboxAttributes): self + public function setBulkActionsTdCheckboxAttributes(array|Closure $bulkActionsTdCheckboxAttributes): self { - return $this->setCustomAttributesDefaults('bulkActionsTdCheckboxAttributes', $bulkActionsTdCheckboxAttributes); + $this->bulkActionsTdCheckboxAttributes = $bulkActionsTdCheckboxAttributes; + return $this; } /** diff --git a/tests/Unit/Traits/Configuration/BulkActionsStylingConfigurationTest.php b/tests/Unit/Traits/Configuration/BulkActionsStylingConfigurationTest.php index 05569f39f..f501225ab 100644 --- a/tests/Unit/Traits/Configuration/BulkActionsStylingConfigurationTest.php +++ b/tests/Unit/Traits/Configuration/BulkActionsStylingConfigurationTest.php @@ -176,7 +176,9 @@ public function test_bulk_actions_td_checkbox_attributes_returns_additional_data $this->assertSame($defaultAttributeBag->getAttributes(), $returnedAttributeBag->getAttributes()); $this->basicTable->setBulkActionsTdCheckboxAttributes([ + 'default' => false, 'default-colors' => true, + 'default-styling' => false, 'class' => 'w-12', ]);