Skip to content

Commit cd89e9e

Browse files
authored
Add ToolsAttributes and ToolbarAttributes (rappasoft#1982)
* Initial Commit * Fix styling * Add getCustomAttributesBagFromArray * Fix styling * Reorder Array * Reorder Initial Array - Add Additional Test * Fix styling * Add More Tests --------- Co-authored-by: lrljoe <[email protected]>
1 parent a7b1579 commit cd89e9e

File tree

8 files changed

+161
-15
lines changed

8 files changed

+161
-15
lines changed
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
@aware(['component','isTailwind','isBootstrap'])
2+
@php($toolsAttributes = $this->getToolsAttributesBag())
23

3-
<div @class([
4-
'flex-col' => $isTailwind,
5-
'd-flex flex-column ' => ($isBootstrap),
6-
])>
4+
<div {{
5+
$toolsAttributes->merge()
6+
->class(['flex-col' => $isTailwind && ($toolsAttributes['default-styling'] ?? true)])
7+
->class(['d-flex flex-column' => $isBootstrap && ($toolsAttributes['default-styling'] ?? true)])
8+
->except(['default','default-styling','default-colors'])
9+
}}
10+
>
711
{{ $slot }}
812
</div>

resources/views/components/tools/toolbar.blade.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
@aware(['component', 'tableName','isTailwind','isBootstrap'])
22
@props([])
3+
@php($toolBarAttributes = $this->getToolBarAttributesBag())
34

4-
<div @class([
5-
'd-md-flex justify-content-between mb-3' => $this->isBootstrap,
6-
'md:flex md:justify-between mb-4 px-4 md:p-0' => $this->isTailwind,
7-
])
5+
<div
6+
{{
7+
$toolBarAttributes->merge()
8+
->class(['md:flex md:justify-between mb-4 px-4 md:p-0' => $isTailwind && ($toolBarAttributes['default-styling'] ?? true)])
9+
->class(['d-md-flex justify-content-between mb-3' => $isBootstrap && ($toolBarAttributes['default-styling'] ?? true)])
10+
->except(['default','default-styling','default-colors'])
11+
}}
812
>
913
<div @class([
1014
'd-md-flex' => $this->isBootstrap,
@@ -52,9 +56,7 @@
5256
'md:flex md:items-center space-y-4 md:space-y-0 md:space-x-2' => $this->isTailwind,
5357
])
5458
>
55-
@if ($this->hasConfigurableAreaFor('toolbar-right-start'))
56-
@include($this->getConfigurableAreaFor('toolbar-right-start'), $this->getParametersForConfigurableArea('toolbar-right-start'))
57-
@endif
59+
@includeWhen($this->hasConfigurableAreaFor('toolbar-right-start'), $this->getConfigurableAreaFor('toolbar-right-start'), $this->getParametersForConfigurableArea('toolbar-right-start'))
5860

5961
@if($this->hasActions && $this->showActionsInToolbar && $this->getActionsPosition == 'right')
6062
<x-livewire-tables::includes.actions/>
@@ -72,9 +74,7 @@
7274
<x-livewire-tables::tools.toolbar.items.pagination-dropdown />
7375
@endif
7476

75-
@if ($this->hasConfigurableAreaFor('toolbar-right-end'))
76-
@include($this->getConfigurableAreaFor('toolbar-right-end'), $this->getParametersForConfigurableArea('toolbar-right-end'))
77-
@endif
77+
@includeWhen($this->hasConfigurableAreaFor('toolbar-right-end'), $this->getConfigurableAreaFor('toolbar-right-end'), $this->getParametersForConfigurableArea('toolbar-right-end'))
7878
</div>
7979
</div>
8080
@if (

src/Traits/Core/HasCustomAttributes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ public function setCustomAttributes(string $propertyName, array $customAttribute
4646

4747
return $this;
4848
}
49+
50+
public function getCustomAttributesBagFromArray(array $attributesArray): ComponentAttributeBag
51+
{
52+
return new ComponentAttributeBag($attributesArray);
53+
}
4954
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Styling\Configuration;
4+
5+
trait ToolsStylingConfiguration
6+
{
7+
public function setToolsAttributes(array $toolsAttributes = []): self
8+
{
9+
$this->setCustomAttributes(propertyName: 'toolsAttributes', customAttributes: $toolsAttributes);
10+
11+
return $this;
12+
}
13+
14+
public function setToolBarAttributes(array $toolBarAttributes = []): self
15+
{
16+
$this->setCustomAttributes(propertyName: 'toolBarAttributes', customAttributes: $toolBarAttributes);
17+
18+
return $this;
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Styling;
4+
5+
use Rappasoft\LaravelLivewireTables\Traits\Styling\Configuration\ToolsStylingConfiguration;
6+
use Rappasoft\LaravelLivewireTables\Traits\Styling\Helpers\ToolsStylingHelpers;
7+
8+
trait HasToolsStyling
9+
{
10+
use ToolsStylingConfiguration,
11+
ToolsStylingHelpers;
12+
13+
protected array $toolsAttributes = ['class' => '', 'default-colors' => true, 'default-styling' => true];
14+
15+
protected array $toolBarAttributes = ['class' => '', 'default-colors' => true, 'default-styling' => true];
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Styling\Helpers;
4+
5+
use Illuminate\View\ComponentAttributeBag;
6+
use Livewire\Attributes\Computed;
7+
8+
trait ToolsStylingHelpers
9+
{
10+
protected function getToolsAttributes(): array
11+
{
12+
return $this->getCustomAttributes(propertyName: 'toolsAttributes', default: false, classicMode: false);
13+
}
14+
15+
#[Computed]
16+
public function getToolsAttributesBag(): ComponentAttributeBag
17+
{
18+
return $this->getCustomAttributesBagFromArray($this->getToolsAttributes());
19+
}
20+
21+
protected function getToolBarAttributes(): array
22+
{
23+
return $this->getCustomAttributes(propertyName: 'toolBarAttributes', default: false, classicMode: false);
24+
}
25+
26+
#[Computed]
27+
public function getToolBarAttributesBag(): ComponentAttributeBag
28+
{
29+
return $this->getCustomAttributesBagFromArray($this->getToolBarAttributes());
30+
31+
}
32+
}

src/Traits/WithTools.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use Rappasoft\LaravelLivewireTables\Traits\Configuration\ToolsConfiguration;
66
use Rappasoft\LaravelLivewireTables\Traits\Helpers\ToolsHelpers;
7+
use Rappasoft\LaravelLivewireTables\Traits\Styling\HasToolsStyling;
78

89
trait WithTools
910
{
1011
use ToolsConfiguration,
11-
ToolsHelpers;
12+
ToolsHelpers,
13+
HasToolsStyling;
1214

1315
protected bool $toolsStatus = true;
1416

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Tests\Traits\Helpers;
4+
5+
use Rappasoft\LaravelLivewireTables\Tests\TestCase;
6+
7+
final class ToolsStylingHelpersTest extends TestCase
8+
{
9+
public function test_can_get_tools_attributes_initial_status(): void
10+
{
11+
$this->assertTrue($this->basicTable->hasCustomAttributes('toolsAttributes'));
12+
}
13+
14+
public function test_can_get_tools_attributes_initial_values(): void
15+
{
16+
$this->assertSame(['class' => '', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolsAttributesBag()->getAttributes());
17+
}
18+
19+
public function test_can_change_tools_attributes_initial_values(): void
20+
{
21+
$this->basicTable->setToolsAttributes(['class' => 'bg-red-500', 'default-colors' => true, 'default-styling' => true]);
22+
$this->assertSame(['class' => 'bg-red-500', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolsAttributesBag()->getAttributes());
23+
$this->assertSame(['class' => '', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolBarAttributesBag()->getAttributes());
24+
}
25+
26+
public function test_can_change_tools_attributes_initial_values_no_defaults(): void
27+
{
28+
$this->basicTable->setToolsAttributes(['class' => 'bg-amber-500']);
29+
$this->assertSame(['class' => 'bg-amber-500', 'default-colors' => false, 'default-styling' => false], $this->basicTable->getToolsAttributesBag()->getAttributes());
30+
$this->assertSame(['class' => '', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolBarAttributesBag()->getAttributes());
31+
32+
}
33+
34+
public function test_can_get_toolbar_attributes_initial_status(): void
35+
{
36+
$this->assertTrue($this->basicTable->hasCustomAttributes('toolBarAttributes'));
37+
}
38+
39+
public function test_can_get_toolbar_attributes_initial_values(): void
40+
{
41+
$this->assertSame(['class' => '', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolBarAttributesBag()->getAttributes());
42+
}
43+
44+
public function test_can_change_toolbar_attributes_initial_values(): void
45+
{
46+
$this->basicTable->setToolBarAttributes(['class' => 'bg-blue-500', 'default-colors' => true, 'default-styling' => true]);
47+
$this->assertSame(['class' => 'bg-blue-500', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolBarAttributesBag()->getAttributes());
48+
$this->assertSame(['class' => '', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolsAttributesBag()->getAttributes());
49+
50+
}
51+
52+
public function test_can_change_toolbar_attributes_initial_values_no_defaults(): void
53+
{
54+
$this->basicTable->setToolBarAttributes(['class' => 'bg-green-500']);
55+
$this->assertSame(['class' => 'bg-green-500', 'default-colors' => false, 'default-styling' => false], $this->basicTable->getToolBarAttributesBag()->getAttributes());
56+
$this->assertSame(['class' => '', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolsAttributesBag()->getAttributes());
57+
}
58+
59+
public function test_can_change_tools_and_toolbar_attributes_initial_values_no_defaults(): void
60+
{
61+
$this->basicTable->setToolsAttributes(['class' => 'bg-amber-500'])->setToolBarAttributes(['class' => 'bg-green-500']);
62+
63+
$this->assertSame(['class' => 'bg-amber-500', 'default-colors' => false, 'default-styling' => false], $this->basicTable->getToolsAttributesBag()->getAttributes());
64+
65+
$this->assertSame(['class' => 'bg-green-500', 'default-colors' => false, 'default-styling' => false], $this->basicTable->getToolBarAttributesBag()->getAttributes());
66+
}
67+
}

0 commit comments

Comments
 (0)