Skip to content

Commit

Permalink
Add ToolsAttributes and ToolbarAttributes (#1982)
Browse files Browse the repository at this point in the history
* 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 <lrljoe@users.noreply.github.com>
  • Loading branch information
lrljoe and lrljoe authored Sep 29, 2024
1 parent a7b1579 commit cd89e9e
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 15 deletions.
12 changes: 8 additions & 4 deletions resources/views/components/tools.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
@aware(['component','isTailwind','isBootstrap'])
@php($toolsAttributes = $this->getToolsAttributesBag())

<div @class([
'flex-col' => $isTailwind,
'd-flex flex-column ' => ($isBootstrap),
])>
<div {{
$toolsAttributes->merge()
->class(['flex-col' => $isTailwind && ($toolsAttributes['default-styling'] ?? true)])
->class(['d-flex flex-column' => $isBootstrap && ($toolsAttributes['default-styling'] ?? true)])
->except(['default','default-styling','default-colors'])
}}
>
{{ $slot }}
</div>
20 changes: 10 additions & 10 deletions resources/views/components/tools/toolbar.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
@aware(['component', 'tableName','isTailwind','isBootstrap'])
@props([])
@php($toolBarAttributes = $this->getToolBarAttributesBag())

<div @class([
'd-md-flex justify-content-between mb-3' => $this->isBootstrap,
'md:flex md:justify-between mb-4 px-4 md:p-0' => $this->isTailwind,
])
<div
{{
$toolBarAttributes->merge()
->class(['md:flex md:justify-between mb-4 px-4 md:p-0' => $isTailwind && ($toolBarAttributes['default-styling'] ?? true)])
->class(['d-md-flex justify-content-between mb-3' => $isBootstrap && ($toolBarAttributes['default-styling'] ?? true)])
->except(['default','default-styling','default-colors'])
}}
>
<div @class([
'd-md-flex' => $this->isBootstrap,
Expand Down Expand Up @@ -52,9 +56,7 @@
'md:flex md:items-center space-y-4 md:space-y-0 md:space-x-2' => $this->isTailwind,
])
>
@if ($this->hasConfigurableAreaFor('toolbar-right-start'))
@include($this->getConfigurableAreaFor('toolbar-right-start'), $this->getParametersForConfigurableArea('toolbar-right-start'))
@endif
@includeWhen($this->hasConfigurableAreaFor('toolbar-right-start'), $this->getConfigurableAreaFor('toolbar-right-start'), $this->getParametersForConfigurableArea('toolbar-right-start'))

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

@if ($this->hasConfigurableAreaFor('toolbar-right-end'))
@include($this->getConfigurableAreaFor('toolbar-right-end'), $this->getParametersForConfigurableArea('toolbar-right-end'))
@endif
@includeWhen($this->hasConfigurableAreaFor('toolbar-right-end'), $this->getConfigurableAreaFor('toolbar-right-end'), $this->getParametersForConfigurableArea('toolbar-right-end'))
</div>
</div>
@if (
Expand Down
5 changes: 5 additions & 0 deletions src/Traits/Core/HasCustomAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ public function setCustomAttributes(string $propertyName, array $customAttribute

return $this;
}

public function getCustomAttributesBagFromArray(array $attributesArray): ComponentAttributeBag
{
return new ComponentAttributeBag($attributesArray);
}
}
20 changes: 20 additions & 0 deletions src/Traits/Styling/Configuration/ToolsStylingConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Styling\Configuration;

trait ToolsStylingConfiguration
{
public function setToolsAttributes(array $toolsAttributes = []): self
{
$this->setCustomAttributes(propertyName: 'toolsAttributes', customAttributes: $toolsAttributes);

return $this;
}

public function setToolBarAttributes(array $toolBarAttributes = []): self
{
$this->setCustomAttributes(propertyName: 'toolBarAttributes', customAttributes: $toolBarAttributes);

return $this;
}
}
16 changes: 16 additions & 0 deletions src/Traits/Styling/HasToolsStyling.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Styling;

use Rappasoft\LaravelLivewireTables\Traits\Styling\Configuration\ToolsStylingConfiguration;
use Rappasoft\LaravelLivewireTables\Traits\Styling\Helpers\ToolsStylingHelpers;

trait HasToolsStyling
{
use ToolsStylingConfiguration,
ToolsStylingHelpers;

protected array $toolsAttributes = ['class' => '', 'default-colors' => true, 'default-styling' => true];

protected array $toolBarAttributes = ['class' => '', 'default-colors' => true, 'default-styling' => true];
}
32 changes: 32 additions & 0 deletions src/Traits/Styling/Helpers/ToolsStylingHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Styling\Helpers;

use Illuminate\View\ComponentAttributeBag;
use Livewire\Attributes\Computed;

trait ToolsStylingHelpers
{
protected function getToolsAttributes(): array
{
return $this->getCustomAttributes(propertyName: 'toolsAttributes', default: false, classicMode: false);
}

#[Computed]
public function getToolsAttributesBag(): ComponentAttributeBag
{
return $this->getCustomAttributesBagFromArray($this->getToolsAttributes());
}

protected function getToolBarAttributes(): array
{
return $this->getCustomAttributes(propertyName: 'toolBarAttributes', default: false, classicMode: false);
}

#[Computed]
public function getToolBarAttributesBag(): ComponentAttributeBag
{
return $this->getCustomAttributesBagFromArray($this->getToolBarAttributes());

}
}
4 changes: 3 additions & 1 deletion src/Traits/WithTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

use Rappasoft\LaravelLivewireTables\Traits\Configuration\ToolsConfiguration;
use Rappasoft\LaravelLivewireTables\Traits\Helpers\ToolsHelpers;
use Rappasoft\LaravelLivewireTables\Traits\Styling\HasToolsStyling;

trait WithTools
{
use ToolsConfiguration,
ToolsHelpers;
ToolsHelpers,
HasToolsStyling;

protected bool $toolsStatus = true;

Expand Down
67 changes: 67 additions & 0 deletions tests/Traits/Helpers/ToolsStylingHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Traits\Helpers;

use Rappasoft\LaravelLivewireTables\Tests\TestCase;

final class ToolsStylingHelpersTest extends TestCase
{
public function test_can_get_tools_attributes_initial_status(): void
{
$this->assertTrue($this->basicTable->hasCustomAttributes('toolsAttributes'));
}

public function test_can_get_tools_attributes_initial_values(): void
{
$this->assertSame(['class' => '', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolsAttributesBag()->getAttributes());
}

public function test_can_change_tools_attributes_initial_values(): void
{
$this->basicTable->setToolsAttributes(['class' => 'bg-red-500', 'default-colors' => true, 'default-styling' => true]);
$this->assertSame(['class' => 'bg-red-500', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolsAttributesBag()->getAttributes());
$this->assertSame(['class' => '', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolBarAttributesBag()->getAttributes());
}

public function test_can_change_tools_attributes_initial_values_no_defaults(): void
{
$this->basicTable->setToolsAttributes(['class' => 'bg-amber-500']);
$this->assertSame(['class' => 'bg-amber-500', 'default-colors' => false, 'default-styling' => false], $this->basicTable->getToolsAttributesBag()->getAttributes());
$this->assertSame(['class' => '', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolBarAttributesBag()->getAttributes());

}

public function test_can_get_toolbar_attributes_initial_status(): void
{
$this->assertTrue($this->basicTable->hasCustomAttributes('toolBarAttributes'));
}

public function test_can_get_toolbar_attributes_initial_values(): void
{
$this->assertSame(['class' => '', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolBarAttributesBag()->getAttributes());
}

public function test_can_change_toolbar_attributes_initial_values(): void
{
$this->basicTable->setToolBarAttributes(['class' => 'bg-blue-500', 'default-colors' => true, 'default-styling' => true]);
$this->assertSame(['class' => 'bg-blue-500', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolBarAttributesBag()->getAttributes());
$this->assertSame(['class' => '', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolsAttributesBag()->getAttributes());

}

public function test_can_change_toolbar_attributes_initial_values_no_defaults(): void
{
$this->basicTable->setToolBarAttributes(['class' => 'bg-green-500']);
$this->assertSame(['class' => 'bg-green-500', 'default-colors' => false, 'default-styling' => false], $this->basicTable->getToolBarAttributesBag()->getAttributes());
$this->assertSame(['class' => '', 'default-colors' => true, 'default-styling' => true], $this->basicTable->getToolsAttributesBag()->getAttributes());
}

public function test_can_change_tools_and_toolbar_attributes_initial_values_no_defaults(): void
{
$this->basicTable->setToolsAttributes(['class' => 'bg-amber-500'])->setToolBarAttributes(['class' => 'bg-green-500']);

$this->assertSame(['class' => 'bg-amber-500', 'default-colors' => false, 'default-styling' => false], $this->basicTable->getToolsAttributesBag()->getAttributes());

$this->assertSame(['class' => 'bg-green-500', 'default-colors' => false, 'default-styling' => false], $this->basicTable->getToolBarAttributesBag()->getAttributes());
}
}

0 comments on commit cd89e9e

Please sign in to comment.