Skip to content

Commit ec093cd

Browse files
authored
Tweaks for Bulk Actions
1 parent 495cd44 commit ec093cd

File tree

5 files changed

+166
-71
lines changed

5 files changed

+166
-71
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@aware(['tableName','primaryKey', 'isTailwind', 'isBootstrap', 'isBootstrap4', 'isBootstrap5'])
2+
@props(['checkboxAttributes'])
3+
<input x-cloak
4+
{{
5+
$attributes->merge($checkboxAttributes)->class([
6+
'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)),
7+
'rounded shadow-sm transition duration-150 ease-in-out focus:ring focus:ring-opacity-50' => ($isTailwind) && ($checkboxAttributes['default-styling'] ?? ($checkboxAttributes['default'] ?? true)),
8+
'form-check-input' => ($isBootstrap5) && ($checkboxAttributes['default'] ?? true),
9+
])->except(['default','default-styling','default-colors'])
10+
}}
11+
/>

resources/views/components/table/td/bulk-actions.blade.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,21 @@
22
@props(['row', 'rowIndex'])
33

44
@php
5-
$customAttributes = $this->getBulkActionsTdAttributes();
6-
$bulkActionsTdCheckboxAttributes = $this->getBulkActionsTdCheckboxAttributes();
5+
$tdAttributes = $this->getBulkActionsTdAttributes;
6+
$tdCheckboxAttributes = $this->getBulkActionsTdCheckboxAttributes;
77
@endphp
88

9-
@if ($this->bulkActionsAreEnabled() && $this->hasBulkActions())
10-
<x-livewire-tables::table.td.plain wire:key="{{ $tableName }}-tbody-td-bulk-actions-td-{{ $row->{$primaryKey} }}" :displayMinimisedOnReorder="true" :$customAttributes>
9+
@if ($this->showBulkActionsSections())
10+
<x-livewire-tables::table.td.plain wire:key="{{ $tableName }}-tbody-td-bulk-actions-td-{{ $row->{$primaryKey} }}" :displayMinimisedOnReorder="true" :customAttributes=$tdAttributes>
1111
<div @class([
1212
'inline-flex rounded-md shadow-sm' => $isTailwind,
1313
'form-check' => $isBootstrap5,
1414
])>
15-
<input wire:key="{{ $tableName . 'selectedItems-'.$row->{$primaryKey} }}"
15+
<x-livewire-tables::forms.checkbox
16+
wire:key="{{ $tableName . 'selectedItems-'.$row->{$primaryKey} }}"
1617
value="{{ $row->{$primaryKey} }}"
17-
{{
18-
$attributes->merge($bulkActionsTdCheckboxAttributes)->class([
19-
'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) && (($bulkActionsTdCheckboxAttributes['default'] ?? true) || ($bulkActionsTdCheckboxAttributes['default-colors'] ?? true)),
20-
'rounded shadow-sm transition duration-150 ease-in-out focus:ring focus:ring-opacity-50' => ($isTailwind) && (($bulkActionsTdCheckboxAttributes['default'] ?? true) || ($bulkActionsTdCheckboxAttributes['default-styling'] ?? true)),
21-
'form-check-input' => ($isBootstrap5) && ($bulkActionsTdCheckboxAttributes['default'] ?? true),
22-
])->except(['default','default-styling','default-colors'])
23-
}}
18+
:checkboxAttributes=$tdCheckboxAttributes
2419
/>
2520
</div>
2621
</x-livewire-tables::table.td.plain>
27-
@endif
22+
@endif

src/Traits/Core/HasCustomAttributes.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,60 @@ public function getCustomAttributesBagFromArray(array $attributesArray): Compone
7676
{
7777
return new ComponentAttributeBag($attributesArray);
7878
}
79+
80+
public function getCustomAttributesNew(string $propertyName, bool $default = false, bool $classicMode = true): array
81+
{
82+
83+
if ($classicMode) {
84+
$defaultItems = ['default','default-colors','default-styling'];
85+
86+
if ($this->hasCustomAttributes($propertyName)) {
87+
$vals = $this->{$propertyName};
88+
89+
foreach($defaultItems as $defaultItem)
90+
{
91+
if(!array_key_exists($defaultItem, $vals) || is_null($vals[$defaultItem]))
92+
{
93+
$vals[$defaultItem] = $default;
94+
}
95+
}
96+
97+
98+
ksort($vals);
99+
100+
return $vals;
101+
}
102+
103+
return ['default' => $default, 'default-colors' => $default, 'default-styling' => $default];
104+
} else {
105+
$defaultItems = ['default-colors','default-styling'];
106+
107+
if ($this->hasCustomAttributes($propertyName)) {
108+
$vals = $this->{$propertyName};
109+
foreach($defaultItems as $defaultItem)
110+
{
111+
if(!array_key_exists($defaultItem, $vals) || is_null($vals[$defaultItem]))
112+
{
113+
$vals[$defaultItem] = $default;
114+
}
115+
}
116+
117+
ksort($vals);
118+
119+
return $vals;
120+
}
121+
122+
return ['default-colors' => $default, 'default-styling' => $default];
123+
124+
}
125+
}
126+
127+
public function setCustomAttributesDefaults(string $propertyName, array $customAttributes, bool $default = false, bool $classicMode = true): self
128+
{
129+
130+
$this->{$propertyName} = array_merge($this->getCustomAttributesNew(propertyName: $propertyName, default: $default, classicMode: $classicMode),$customAttributes);
131+
132+
return $this;
133+
}
134+
79135
}

src/Traits/Styling/HasBulkActionsStyling.php

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ trait HasBulkActionsStyling
99
{
1010
protected array $bulkActionsCheckboxAttributes = [];
1111

12-
protected array $bulkActionsThAttributes = ['default' => true];
12+
protected array $bulkActionsThAttributes = ['default' => null, 'default-colors' => null, 'default-styling' => null];
1313

14-
protected array $bulkActionsThCheckboxAttributes = ['default' => true];
14+
protected array $bulkActionsThCheckboxAttributes = ['default' => null, 'default-colors' => null, 'default-styling' => null];
1515

16-
protected array $bulkActionsTdAttributes = ['default' => true];
17-
18-
protected array $bulkActionsTdCheckboxAttributes = ['default' => true, 'default-colors' => false, 'default-styling' => false];
16+
protected array $bulkActionsTdAttributes = ['default' => null, 'default-colors' => null, 'default-styling' => null];
17+
18+
protected array $bulkActionsTdCheckboxAttributes = ['default' => null, 'default-colors' => null, 'default-styling' => null];
1919

2020
protected array $bulkActionsButtonAttributes = ['default-colors' => true, 'default-styling' => true];
2121

@@ -69,7 +69,7 @@ public function getBulkActionsMenuItemAttributes(): array
6969
#[Computed]
7070
public function getBulkActionsThAttributes(): array
7171
{
72-
return $this->getCustomAttributes('bulkActionsThAttributes');
72+
return $this->getCustomAttributesNew('bulkActionsThAttributes', true, true);
7373

7474
}
7575

@@ -79,7 +79,7 @@ public function getBulkActionsThAttributes(): array
7979
#[Computed]
8080
public function hasBulkActionsThAttributes(): bool
8181
{
82-
return $this->getBulkActionsThAttributes() != ['default' => true, 'default-colors' => false, 'default-styling' => false];
82+
return $this->getBulkActionsThAttributes() != ['default' => true, 'default-colors' => true, 'default-styling' => true];
8383
}
8484

8585
/**
@@ -89,41 +89,38 @@ public function hasBulkActionsThAttributes(): bool
8989
*/
9090
public function getBulkActionsThCheckboxAttributes(): array
9191
{
92-
return $this->getCustomAttributes('bulkActionsThCheckboxAttributes');
92+
return $this->getCustomAttributesNew('bulkActionsThCheckboxAttributes', true, true);
93+
9394
}
9495

9596
/**
9697
* Used to get attributes for the Bulk Actions TD
9798
*
9899
* @return array<mixed>
99100
*/
101+
#[Computed]
100102
public function getBulkActionsTdAttributes(): array
101103
{
102-
return $this->getCustomAttributes('bulkActionsTdAttributes');
104+
return $this->getCustomAttributesNew('bulkActionsTdAttributes', true, true);
103105
}
104106

105107
/**
106108
* Used to get attributes for the Bulk Actions TD
107109
*
108110
* @return array<mixed>
109111
*/
112+
#[Computed]
110113
public function getBulkActionsTdCheckboxAttributes(): array
111114
{
112-
return $this->getCustomAttributes('bulkActionsTdCheckboxAttributes');
113-
$array = array_merge(
115+
return array_merge(
114116
[
115-
'x-cloak',
116117
'x-show' => '!currentlyReorderingStatus',
117118
'x-model' => 'selectedItems',
118119
'wire:loading.attr.delay' => 'disabled',
119120
'type' => 'checkbox',
120121
],
121-
$this->getCustomAttributes('bulkActionsTdCheckboxAttributes')
122+
$this->getCustomAttributesNew('bulkActionsTdCheckboxAttributes', true, true)
122123
);
123-
ksort($array);
124-
125-
return $array;
126-
127124
}
128125

129126
/**
@@ -143,84 +140,69 @@ public function getBulkActionsRowButtonAttributesBag(): ComponentAttributeBag
143140
{
144141
return $this->getCustomAttributesBagFromArray($this->getBulkActionsRowButtonAttributes());
145142
}
146-
143+
147144
/**
148145
* Used to set attributes for the Bulk Actions Menu Button
149146
*/
150147
public function setBulkActionsButtonAttributes(array $bulkActionsButtonAttributes): self
151148
{
152-
$this->setCustomAttributes('bulkActionsButtonAttributes', $bulkActionsButtonAttributes);
153-
154-
return $this;
149+
return $this->setCustomAttributes('bulkActionsButtonAttributes', $bulkActionsButtonAttributes);
155150
}
156151

157152
/**
158153
* Used to set attributes for the Bulk Actions Menu
159154
*/
160155
public function setBulkActionsMenuAttributes(array $bulkActionsMenuAttributes): self
161156
{
162-
$this->setCustomAttributes('bulkActionsMenuAttributes', $bulkActionsMenuAttributes);
163-
164-
return $this;
157+
return $this->setCustomAttributes('bulkActionsMenuAttributes', $bulkActionsMenuAttributes);
165158
}
166159

167160
/**
168161
* Used to set attributes for the Bulk Actions Menu Items
169162
*/
170163
public function setBulkActionsMenuItemAttributes(array $bulkActionsMenuItemAttributes): self
171164
{
172-
$this->setCustomAttributes('bulkActionsMenuItemAttributes', $bulkActionsMenuItemAttributes);
173-
174-
return $this;
165+
return $this->setCustomAttributes('bulkActionsMenuItemAttributes', $bulkActionsMenuItemAttributes);
175166
}
176167

177168
/**
178169
* Used to set attributes for the Bulk Actions TD in the Row
179170
*/
180171
public function setBulkActionsTdAttributes(array $bulkActionsTdAttributes): self
181172
{
182-
$this->setCustomAttributes('bulkActionsTdAttributes', $bulkActionsTdAttributes);
173+
return $this->setCustomAttributesDefaults('bulkActionsTdAttributes', $bulkActionsTdAttributes);
183174

184-
return $this;
185175
}
186176

187177
/**
188178
* Used to set attributes for the Bulk Actions Checkbox in the Row
189179
*/
190180
public function setBulkActionsTdCheckboxAttributes(array $bulkActionsTdCheckboxAttributes): self
191181
{
192-
$this->setCustomAttributes('bulkActionsTdCheckboxAttributes', $bulkActionsTdCheckboxAttributes);
193-
194-
return $this;
182+
return $this->setCustomAttributesDefaults('bulkActionsTdCheckboxAttributes', $bulkActionsTdCheckboxAttributes);
195183
}
196184

197185
/**
198186
* Used to set attributes for the <th> for Bulk Actions
199187
*/
200188
public function setBulkActionsThAttributes(array $bulkActionsThAttributes): self
201189
{
202-
$this->setCustomAttributes('bulkActionsThAttributes', $bulkActionsThAttributes);
203-
204-
return $this;
190+
return $this->setCustomAttributesDefaults('bulkActionsThAttributes', $bulkActionsThAttributes);
205191
}
206192

207193
/**
208194
* Used to set attributes for the Bulk Actions Checkbox in the <th>
209195
*/
210196
public function setBulkActionsThCheckboxAttributes(array $bulkActionsThCheckboxAttributes): self
211197
{
212-
$this->setCustomAttributes('bulkActionsThCheckboxAttributes', $bulkActionsThCheckboxAttributes);
213-
214-
return $this;
198+
return $this->setCustomAttributesDefaults('bulkActionsThCheckboxAttributes', $bulkActionsThCheckboxAttributes);
215199
}
216200

217201
/**
218202
* Used to set attributes for the Bulk Actions Row Buttons
219203
*/
220204
public function setBulkActionsRowButtonAttributes(array $bulkActionsRowButtonAttributes): self
221205
{
222-
$this->setCustomAttributes('bulkActionsRowButtonAttributes', $bulkActionsRowButtonAttributes);
223-
224-
return $this;
206+
return $this->setCustomAttributes('bulkActionsRowButtonAttributes', $bulkActionsRowButtonAttributes);
225207
}
226208
}

0 commit comments

Comments
 (0)