diff --git a/resources/views/components/forms/checkbox.blade.php b/resources/views/components/forms/checkbox.blade.php new file mode 100644 index 000000000..4b2b9b940 --- /dev/null +++ b/resources/views/components/forms/checkbox.blade.php @@ -0,0 +1,11 @@ +@aware(['tableName','primaryKey', 'isTailwind', 'isBootstrap', 'isBootstrap4', 'isBootstrap5']) +@props(['checkboxAttributes']) +merge($checkboxAttributes)->class([ + 'border-gray-300 text-indigo-600 focus:border-indigo-300 focus:ring-indigo-200 dark:bg-gray-900 dark:text-white dark:border-gray-600 dark:hover:bg-gray-600 dark:focus:bg-gray-600' => ($isTailwind) && ($checkboxAttributes['default-colors'] ?? ($checkboxAttributes['default'] ?? true)), + 'rounded shadow-sm transition duration-150 ease-in-out focus:ring focus:ring-opacity-50' => ($isTailwind) && ($checkboxAttributes['default-styling'] ?? ($checkboxAttributes['default'] ?? true)), + 'form-check-input' => ($isBootstrap5) && ($checkboxAttributes['default'] ?? true), + ])->except(['default','default-styling','default-colors']) + }} +/> \ No newline at end of file diff --git a/resources/views/components/table/td/bulk-actions.blade.php b/resources/views/components/table/td/bulk-actions.blade.php index 80cb3e5ff..1b4049042 100644 --- a/resources/views/components/table/td/bulk-actions.blade.php +++ b/resources/views/components/table/td/bulk-actions.blade.php @@ -2,29 +2,20 @@ @props(['row', 'rowIndex']) @php - $customAttributes = $this->getBulkActionsTdAttributes(); - $bulkActionsTdCheckboxAttributes = $this->getBulkActionsTdCheckboxAttributes(); + $tdAttributes = $this->getBulkActionsTdAttributes; + $tdCheckboxAttributes = $this->getBulkActionsTdCheckboxAttributes; @endphp -@if ($this->bulkActionsAreEnabled() && $this->hasBulkActions()) - +@if ($this->showBulkActionsSections()) +
$isTailwind, 'form-check' => $isBootstrap5, ])> - merge($bulkActionsTdCheckboxAttributes)->class([ - 'rounded border-gray-300 text-indigo-600 shadow-sm transition duration-150 ease-in-out focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 dark:bg-gray-900 dark:text-white dark:border-gray-600 dark:hover:bg-gray-600 dark:focus:bg-gray-600' => ($isTailwind) && ($bulkActionsTdCheckboxAttributes['default'] ?? true), - 'form-check-input' => ($isBootstrap5) && ($bulkActionsTdCheckboxAttributes['default'] ?? true), - ])->except(['default','default-styling','default-colors']) - }} + :checkboxAttributes=$tdCheckboxAttributes />
diff --git a/resources/views/components/table/th/bulk-actions.blade.php b/resources/views/components/table/th/bulk-actions.blade.php index 90f9ba442..64bc3632c 100644 --- a/resources/views/components/table/th/bulk-actions.blade.php +++ b/resources/views/components/table/th/bulk-actions.blade.php @@ -1,11 +1,11 @@ -@aware(['isTailwind', 'isBootstrap']) +@aware(['tableName','isTailwind', 'isBootstrap']) @php $customAttributes = $this->hasBulkActionsThAttributes ? $this->getBulkActionsThAttributes : $this->getAllThAttributes($this->getBulkActionsColumn())['customAttributes']; $bulkActionsThCheckboxAttributes = $this->getBulkActionsThCheckboxAttributes(); @endphp @if ($this->bulkActionsAreEnabled() && $this->hasBulkActions()) - +
-@endif +@endif \ No newline at end of file diff --git a/src/Traits/Core/HasCustomAttributes.php b/src/Traits/Core/HasCustomAttributes.php index a642c3cac..442dcb804 100644 --- a/src/Traits/Core/HasCustomAttributes.php +++ b/src/Traits/Core/HasCustomAttributes.php @@ -76,4 +76,54 @@ public function getCustomAttributesBagFromArray(array $attributesArray): Compone { return new ComponentAttributeBag($attributesArray); } + + public function getCustomAttributesNew(string $propertyName, bool $default = false, bool $classicMode = true): array + { + + if ($classicMode) { + $defaultItems = ['default', 'default-colors', 'default-styling']; + + if ($this->hasCustomAttributes($propertyName)) { + $vals = $this->{$propertyName}; + + foreach ($defaultItems as $defaultItem) { + if (! array_key_exists($defaultItem, $vals) || is_null($vals[$defaultItem])) { + $vals[$defaultItem] = $default; + } + } + + ksort($vals); + + return $vals; + } + + return ['default' => $default, 'default-colors' => $default, 'default-styling' => $default]; + } else { + $defaultItems = ['default-colors', 'default-styling']; + + if ($this->hasCustomAttributes($propertyName)) { + $vals = $this->{$propertyName}; + foreach ($defaultItems as $defaultItem) { + if (! array_key_exists($defaultItem, $vals) || is_null($vals[$defaultItem])) { + $vals[$defaultItem] = $default; + } + } + + ksort($vals); + + return $vals; + } + + return ['default-colors' => $default, 'default-styling' => $default]; + + } + } + + public function setCustomAttributesDefaults(string $propertyName, array $customAttributes, bool $default = false, bool $classicMode = true): self + { + + $this->{$propertyName} = array_merge($this->getCustomAttributesNew(propertyName: $propertyName, default: $default, classicMode: $classicMode), $customAttributes); + + return $this; + } } diff --git a/src/Traits/Styling/HasBulkActionsStyling.php b/src/Traits/Styling/HasBulkActionsStyling.php index 5f4638455..73cbe6402 100644 --- a/src/Traits/Styling/HasBulkActionsStyling.php +++ b/src/Traits/Styling/HasBulkActionsStyling.php @@ -9,13 +9,13 @@ trait HasBulkActionsStyling { protected array $bulkActionsCheckboxAttributes = []; - protected array $bulkActionsThAttributes = ['default' => true]; + protected array $bulkActionsThAttributes = ['default' => null, 'default-colors' => null, 'default-styling' => null]; - protected array $bulkActionsThCheckboxAttributes = ['default' => true]; + protected array $bulkActionsThCheckboxAttributes = ['default' => null, 'default-colors' => null, 'default-styling' => null]; - protected array $bulkActionsTdAttributes = ['default' => true]; + protected array $bulkActionsTdAttributes = ['default' => null, 'default-colors' => null, 'default-styling' => null]; - protected array $bulkActionsTdCheckboxAttributes = ['default' => true]; + protected array $bulkActionsTdCheckboxAttributes = ['default' => null, 'default-colors' => null, 'default-styling' => null]; protected array $bulkActionsButtonAttributes = ['default-colors' => true, 'default-styling' => true]; @@ -69,7 +69,7 @@ public function getBulkActionsMenuItemAttributes(): array #[Computed] public function getBulkActionsThAttributes(): array { - return $this->getCustomAttributes('bulkActionsThAttributes'); + return $this->getCustomAttributesNew('bulkActionsThAttributes', true, true); } @@ -79,7 +79,7 @@ public function getBulkActionsThAttributes(): array #[Computed] public function hasBulkActionsThAttributes(): bool { - return $this->getBulkActionsThAttributes() != ['default' => true, 'default-colors' => false, 'default-styling' => false]; + return $this->getBulkActionsThAttributes() != ['default' => true, 'default-colors' => true, 'default-styling' => true]; } /** @@ -89,7 +89,8 @@ public function hasBulkActionsThAttributes(): bool */ public function getBulkActionsThCheckboxAttributes(): array { - return $this->getCustomAttributes('bulkActionsThCheckboxAttributes'); + return $this->getCustomAttributesNew('bulkActionsThCheckboxAttributes', true, true); + } /** @@ -97,9 +98,10 @@ public function getBulkActionsThCheckboxAttributes(): array * * @return array */ + #[Computed] public function getBulkActionsTdAttributes(): array { - return $this->getCustomAttributes('bulkActionsTdAttributes'); + return $this->getCustomAttributesNew('bulkActionsTdAttributes', true, true); } /** @@ -107,10 +109,18 @@ public function getBulkActionsTdAttributes(): array * * @return array */ + #[Computed] public function getBulkActionsTdCheckboxAttributes(): array { - return $this->getCustomAttributes('bulkActionsTdCheckboxAttributes'); - + return array_merge( + [ + 'x-show' => '!currentlyReorderingStatus', + 'x-model' => 'selectedItems', + 'wire:loading.attr.delay' => 'disabled', + 'type' => 'checkbox', + ], + $this->getCustomAttributesNew('bulkActionsTdCheckboxAttributes', true, true) + ); } /** @@ -136,9 +146,7 @@ public function getBulkActionsRowButtonAttributesBag(): ComponentAttributeBag */ public function setBulkActionsButtonAttributes(array $bulkActionsButtonAttributes): self { - $this->setCustomAttributes('bulkActionsButtonAttributes', $bulkActionsButtonAttributes); - - return $this; + return $this->setCustomAttributes('bulkActionsButtonAttributes', $bulkActionsButtonAttributes); } /** @@ -146,9 +154,7 @@ public function setBulkActionsButtonAttributes(array $bulkActionsButtonAttribute */ public function setBulkActionsMenuAttributes(array $bulkActionsMenuAttributes): self { - $this->setCustomAttributes('bulkActionsMenuAttributes', $bulkActionsMenuAttributes); - - return $this; + return $this->setCustomAttributes('bulkActionsMenuAttributes', $bulkActionsMenuAttributes); } /** @@ -156,9 +162,7 @@ public function setBulkActionsMenuAttributes(array $bulkActionsMenuAttributes): */ public function setBulkActionsMenuItemAttributes(array $bulkActionsMenuItemAttributes): self { - $this->setCustomAttributes('bulkActionsMenuItemAttributes', $bulkActionsMenuItemAttributes); - - return $this; + return $this->setCustomAttributes('bulkActionsMenuItemAttributes', $bulkActionsMenuItemAttributes); } /** @@ -166,9 +170,8 @@ public function setBulkActionsMenuItemAttributes(array $bulkActionsMenuItemAttri */ public function setBulkActionsTdAttributes(array $bulkActionsTdAttributes): self { - $this->setCustomAttributes('bulkActionsTdAttributes', $bulkActionsTdAttributes); + return $this->setCustomAttributesDefaults('bulkActionsTdAttributes', $bulkActionsTdAttributes); - return $this; } /** @@ -176,9 +179,7 @@ public function setBulkActionsTdAttributes(array $bulkActionsTdAttributes): self */ public function setBulkActionsTdCheckboxAttributes(array $bulkActionsTdCheckboxAttributes): self { - $this->setCustomAttributes('bulkActionsTdCheckboxAttributes', $bulkActionsTdCheckboxAttributes); - - return $this; + return $this->setCustomAttributesDefaults('bulkActionsTdCheckboxAttributes', $bulkActionsTdCheckboxAttributes); } /** @@ -186,9 +187,7 @@ public function setBulkActionsTdCheckboxAttributes(array $bulkActionsTdCheckboxA */ public function setBulkActionsThAttributes(array $bulkActionsThAttributes): self { - $this->setCustomAttributes('bulkActionsThAttributes', $bulkActionsThAttributes); - - return $this; + return $this->setCustomAttributesDefaults('bulkActionsThAttributes', $bulkActionsThAttributes); } /** @@ -196,9 +195,7 @@ public function setBulkActionsThAttributes(array $bulkActionsThAttributes): self */ public function setBulkActionsThCheckboxAttributes(array $bulkActionsThCheckboxAttributes): self { - $this->setCustomAttributes('bulkActionsThCheckboxAttributes', $bulkActionsThCheckboxAttributes); - - return $this; + return $this->setCustomAttributesDefaults('bulkActionsThCheckboxAttributes', $bulkActionsThCheckboxAttributes); } /** @@ -206,8 +203,6 @@ public function setBulkActionsThCheckboxAttributes(array $bulkActionsThCheckboxA */ public function setBulkActionsRowButtonAttributes(array $bulkActionsRowButtonAttributes): self { - $this->setCustomAttributes('bulkActionsRowButtonAttributes', $bulkActionsRowButtonAttributes); - - return $this; + return $this->setCustomAttributes('bulkActionsRowButtonAttributes', $bulkActionsRowButtonAttributes); } } diff --git a/tests/Unit/Traits/Configuration/BulkActionsStylingConfigurationTest.php b/tests/Unit/Traits/Configuration/BulkActionsStylingConfigurationTest.php index 019c13d22..05569f39f 100644 --- a/tests/Unit/Traits/Configuration/BulkActionsStylingConfigurationTest.php +++ b/tests/Unit/Traits/Configuration/BulkActionsStylingConfigurationTest.php @@ -73,12 +73,14 @@ public function test_can_set_bulk_actions_attributes_via_provider(string $setMet 'default-styling' => $defaultStyling, ]); + $returnedData = collect($this->basicTable->{$getMethod}())->only(['class', 'default', 'default-colors', 'default-styling'])->sortKeys()->toArray(); + $this->assertSame([ 'class' => $class1, 'default' => $default, 'default-colors' => $defaultColors, 'default-styling' => $defaultStyling, - ], $this->basicTable->{$getMethod}()); + ], $returnedData); $this->basicTable->{$setMethod}([ 'default' => $default, @@ -87,12 +89,14 @@ public function test_can_set_bulk_actions_attributes_via_provider(string $setMet 'default-styling' => $defaultStyling, ]); + $returnedData = collect($this->basicTable->{$getMethod}())->only(['class', 'default', 'default-colors', 'default-styling'])->sortKeys()->toArray(); + $this->assertSame([ 'class' => $class2, 'default' => $default, 'default-colors' => ! $defaultColors, 'default-styling' => $defaultStyling, - ], $this->basicTable->{$getMethod}()); + ], $returnedData); } #[DataProvider('bulkActionAttributesProvider')] @@ -107,7 +111,9 @@ public function test_can_get_bulk_actions_attributes_bag_via_provider(string $se $this->basicTable->{$setMethod}($data); - $this->assertSame($data, $this->basicTable->{$getMethod}()); + $returnedData = collect($this->basicTable->{$getMethod}())->only(['class', 'default', 'default-colors', 'default-styling'])->sortKeys()->toArray(); + + $this->assertSame($data, $returnedData); $attributeBag = new ComponentAttributeBag($data); @@ -116,21 +122,77 @@ public function test_can_get_bulk_actions_attributes_bag_via_provider(string $se public function test_bulk_actions_td_attributes_returns_default_true_if_not_set(): void { - $this->assertSame(['default' => true, 'default-colors' => false, 'default-styling' => false], $this->basicTable->getBulkActionsTdAttributes()); + $this->assertSame(['default' => true, 'default-colors' => true, 'default-styling' => true], $this->basicTable->getBulkActionsTdAttributes()); } public function test_bulk_actions_td_checkbox_attributes_returns_default_true_if_not_set(): void { - $this->assertSame(['default' => true, 'default-colors' => false, 'default-styling' => false], $this->basicTable->getBulkActionsTdCheckboxAttributes()); + + $this->assertSame(['default' => true, 'default-colors' => true, 'default-styling' => true], collect($this->basicTable->getBulkActionsTdCheckboxAttributes())->only(['class', 'default', 'default-colors', 'default-styling'])->sortKeys()->toArray()); } public function test_bulk_actions_th_attributes_returns_default_true_if_not_set(): void { - $this->assertSame(['default' => true, 'default-colors' => false, 'default-styling' => false], $this->basicTable->getBulkActionsThAttributes()); + $this->assertSame(['default' => true, 'default-colors' => true, 'default-styling' => true], $this->basicTable->getBulkActionsThAttributes()); } public function test_bulk_actions_th_checkbox_attributes_returns_default_true_if_not_set(): void { - $this->assertSame(['default' => true, 'default-colors' => false, 'default-styling' => false], $this->basicTable->getBulkActionsThCheckboxAttributes()); + $this->assertSame(['default' => true, 'default-colors' => true, 'default-styling' => true], $this->basicTable->getBulkActionsThCheckboxAttributes()); + } + + public function test_bulk_actions_td_checkbox_attributes_returns_additional_data(): void + { + $defaultData = [ + 'x-show' => '!currentlyReorderingStatus', + 'x-model' => 'selectedItems', + 'wire:loading.attr.delay' => 'disabled', + 'type' => 'checkbox', + 'default' => true, + 'default-colors' => true, + 'default-styling' => true, + ]; + ksort($defaultData); + + $returnedData = $this->basicTable->getBulkActionsTdCheckboxAttributes(); + ksort($returnedData); + + $this->assertSame($defaultData, $returnedData); + } + + public function test_bulk_actions_td_checkbox_attributes_returns_additional_data_with_bag(): void + { + $defaultAttributeBag = new ComponentAttributeBag([ + 'x-show' => '!currentlyReorderingStatus', + 'x-model' => 'selectedItems', + 'wire:loading.attr.delay' => 'disabled', + 'type' => 'checkbox', + 'default' => true, + 'default-colors' => true, + 'default-styling' => true, + ]); + $returnedAttributeBag = new ComponentAttributeBag($this->basicTable->getBulkActionsTdCheckboxAttributes()); + + $this->assertSame($defaultAttributeBag->getAttributes(), $returnedAttributeBag->getAttributes()); + + $this->basicTable->setBulkActionsTdCheckboxAttributes([ + 'default-colors' => true, + 'class' => 'w-12', + ]); + + $customAttributeBag = new ComponentAttributeBag([ + 'x-show' => '!currentlyReorderingStatus', + 'x-model' => 'selectedItems', + 'wire:loading.attr.delay' => 'disabled', + 'type' => 'checkbox', + 'class' => 'w-12', + 'default' => false, + 'default-colors' => true, + 'default-styling' => false, + ]); + + $updatedAttributeBag = new ComponentAttributeBag($this->basicTable->getBulkActionsTdCheckboxAttributes()); + $this->assertSame($customAttributeBag->getAttributes(), $updatedAttributeBag->getAttributes()); + } }