Skip to content

Commit b2d36c5

Browse files
authored
BulkActionTweaks - Adding default checkbox, customising attribute behaviours (rappasoft#2203)
* Tweaks for BulkActions * Fix styling * Tweaks for Bulk Actions * Fix styling
1 parent c5b63fc commit b2d36c5

File tree

6 files changed

+168
-59
lines changed

6 files changed

+168
-59
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: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,20 @@
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
16-
x-cloak x-show="!currentlyReorderingStatus"
17-
x-model="selectedItems"
18-
wire:key="{{ $tableName . 'selectedItems-'.$row->{$primaryKey} }}"
19-
wire:loading.attr.delay="disabled"
15+
<x-livewire-tables::forms.checkbox
16+
wire:key="{{ $tableName . 'selectedItems-'.$row->{$primaryKey} }}"
2017
value="{{ $row->{$primaryKey} }}"
21-
type="checkbox"
22-
{{
23-
$attributes->merge($bulkActionsTdCheckboxAttributes)->class([
24-
'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),
25-
'form-check-input' => ($isBootstrap5) && ($bulkActionsTdCheckboxAttributes['default'] ?? true),
26-
])->except(['default','default-styling','default-colors'])
27-
}}
18+
:checkboxAttributes=$tdCheckboxAttributes
2819
/>
2920
</div>
3021
</x-livewire-tables::table.td.plain>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
@aware(['isTailwind', 'isBootstrap'])
1+
@aware(['tableName','isTailwind', 'isBootstrap'])
22
@php
33
$customAttributes = $this->hasBulkActionsThAttributes ? $this->getBulkActionsThAttributes : $this->getAllThAttributes($this->getBulkActionsColumn())['customAttributes'];
44
$bulkActionsThCheckboxAttributes = $this->getBulkActionsThCheckboxAttributes();
55
@endphp
66

77
@if ($this->bulkActionsAreEnabled() && $this->hasBulkActions())
8-
<x-livewire-tables::table.th.plain :displayMinimisedOnReorder="true" wire:key="{{ $this->getTableName }}-thead-bulk-actions" :$customAttributes>
8+
<x-livewire-tables::table.th.plain :displayMinimisedOnReorder="true" wire:key="{{ $tableName }}-thead-bulk-actions" :$customAttributes>
99
<div
1010
x-data="{newSelectCount: 0, indeterminateCheckbox: false, bulkActionHeaderChecked: false}"
1111
x-init="$watch('selectedItems', value => indeterminateCheckbox = (value.length > 0 && value.length < paginationTotalItemCount))"
@@ -30,4 +30,4 @@
3030
/>
3131
</div>
3232
</x-livewire-tables::table.th.plain>
33-
@endif
33+
@endif

src/Traits/Core/HasCustomAttributes.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,54 @@ 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+
if (! array_key_exists($defaultItem, $vals) || is_null($vals[$defaultItem])) {
91+
$vals[$defaultItem] = $default;
92+
}
93+
}
94+
95+
ksort($vals);
96+
97+
return $vals;
98+
}
99+
100+
return ['default' => $default, 'default-colors' => $default, 'default-styling' => $default];
101+
} else {
102+
$defaultItems = ['default-colors', 'default-styling'];
103+
104+
if ($this->hasCustomAttributes($propertyName)) {
105+
$vals = $this->{$propertyName};
106+
foreach ($defaultItems as $defaultItem) {
107+
if (! array_key_exists($defaultItem, $vals) || is_null($vals[$defaultItem])) {
108+
$vals[$defaultItem] = $default;
109+
}
110+
}
111+
112+
ksort($vals);
113+
114+
return $vals;
115+
}
116+
117+
return ['default-colors' => $default, 'default-styling' => $default];
118+
119+
}
120+
}
121+
122+
public function setCustomAttributesDefaults(string $propertyName, array $customAttributes, bool $default = false, bool $classicMode = true): self
123+
{
124+
125+
$this->{$propertyName} = array_merge($this->getCustomAttributesNew(propertyName: $propertyName, default: $default, classicMode: $classicMode), $customAttributes);
126+
127+
return $this;
128+
}
79129
}

src/Traits/Styling/HasBulkActionsStyling.php

Lines changed: 28 additions & 33 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];
16+
protected array $bulkActionsTdAttributes = ['default' => null, 'default-colors' => null, 'default-styling' => null];
1717

18-
protected array $bulkActionsTdCheckboxAttributes = ['default' => true];
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,28 +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-
115+
return array_merge(
116+
[
117+
'x-show' => '!currentlyReorderingStatus',
118+
'x-model' => 'selectedItems',
119+
'wire:loading.attr.delay' => 'disabled',
120+
'type' => 'checkbox',
121+
],
122+
$this->getCustomAttributesNew('bulkActionsTdCheckboxAttributes', true, true)
123+
);
114124
}
115125

116126
/**
@@ -136,78 +146,63 @@ public function getBulkActionsRowButtonAttributesBag(): ComponentAttributeBag
136146
*/
137147
public function setBulkActionsButtonAttributes(array $bulkActionsButtonAttributes): self
138148
{
139-
$this->setCustomAttributes('bulkActionsButtonAttributes', $bulkActionsButtonAttributes);
140-
141-
return $this;
149+
return $this->setCustomAttributes('bulkActionsButtonAttributes', $bulkActionsButtonAttributes);
142150
}
143151

144152
/**
145153
* Used to set attributes for the Bulk Actions Menu
146154
*/
147155
public function setBulkActionsMenuAttributes(array $bulkActionsMenuAttributes): self
148156
{
149-
$this->setCustomAttributes('bulkActionsMenuAttributes', $bulkActionsMenuAttributes);
150-
151-
return $this;
157+
return $this->setCustomAttributes('bulkActionsMenuAttributes', $bulkActionsMenuAttributes);
152158
}
153159

154160
/**
155161
* Used to set attributes for the Bulk Actions Menu Items
156162
*/
157163
public function setBulkActionsMenuItemAttributes(array $bulkActionsMenuItemAttributes): self
158164
{
159-
$this->setCustomAttributes('bulkActionsMenuItemAttributes', $bulkActionsMenuItemAttributes);
160-
161-
return $this;
165+
return $this->setCustomAttributes('bulkActionsMenuItemAttributes', $bulkActionsMenuItemAttributes);
162166
}
163167

164168
/**
165169
* Used to set attributes for the Bulk Actions TD in the Row
166170
*/
167171
public function setBulkActionsTdAttributes(array $bulkActionsTdAttributes): self
168172
{
169-
$this->setCustomAttributes('bulkActionsTdAttributes', $bulkActionsTdAttributes);
173+
return $this->setCustomAttributesDefaults('bulkActionsTdAttributes', $bulkActionsTdAttributes);
170174

171-
return $this;
172175
}
173176

174177
/**
175178
* Used to set attributes for the Bulk Actions Checkbox in the Row
176179
*/
177180
public function setBulkActionsTdCheckboxAttributes(array $bulkActionsTdCheckboxAttributes): self
178181
{
179-
$this->setCustomAttributes('bulkActionsTdCheckboxAttributes', $bulkActionsTdCheckboxAttributes);
180-
181-
return $this;
182+
return $this->setCustomAttributesDefaults('bulkActionsTdCheckboxAttributes', $bulkActionsTdCheckboxAttributes);
182183
}
183184

184185
/**
185186
* Used to set attributes for the <th> for Bulk Actions
186187
*/
187188
public function setBulkActionsThAttributes(array $bulkActionsThAttributes): self
188189
{
189-
$this->setCustomAttributes('bulkActionsThAttributes', $bulkActionsThAttributes);
190-
191-
return $this;
190+
return $this->setCustomAttributesDefaults('bulkActionsThAttributes', $bulkActionsThAttributes);
192191
}
193192

194193
/**
195194
* Used to set attributes for the Bulk Actions Checkbox in the <th>
196195
*/
197196
public function setBulkActionsThCheckboxAttributes(array $bulkActionsThCheckboxAttributes): self
198197
{
199-
$this->setCustomAttributes('bulkActionsThCheckboxAttributes', $bulkActionsThCheckboxAttributes);
200-
201-
return $this;
198+
return $this->setCustomAttributesDefaults('bulkActionsThCheckboxAttributes', $bulkActionsThCheckboxAttributes);
202199
}
203200

204201
/**
205202
* Used to set attributes for the Bulk Actions Row Buttons
206203
*/
207204
public function setBulkActionsRowButtonAttributes(array $bulkActionsRowButtonAttributes): self
208205
{
209-
$this->setCustomAttributes('bulkActionsRowButtonAttributes', $bulkActionsRowButtonAttributes);
210-
211-
return $this;
206+
return $this->setCustomAttributes('bulkActionsRowButtonAttributes', $bulkActionsRowButtonAttributes);
212207
}
213208
}

0 commit comments

Comments
 (0)