Skip to content

Commit

Permalink
Enable/Disable Tools/Toolbar (#1896)
Browse files Browse the repository at this point in the history
* Customised Toolbar Approach


---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Aug 26, 2024
1 parent e1acab7 commit c14692b
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 13 deletions.
59 changes: 59 additions & 0 deletions docs/misc/tools.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
title: Tools
weight: 9
---

The Table offers additional configuration to show/hide the Tools/Toolbar sections:
## Tools
Contains:
- Filter Pills
- Sorting Pills
- The Toolbar

## Toolbar
Contains:
- Actions (if set to Toolbar)
- Column Select dropdown
- Configurable Areas for Toolbar
- Filters Button/Dropdown/Popover
- Pagination dropdown
- Reorder Button
- Search Input

## Component Available Methods

### setToolsEnabled
The Default Behaviour, Tools Are Enabled. But will only be rendered if there are available/enabled elements. If the Toolbar is enabled, this takes into account any Toolbar elements that are present.
```php
public function configure(): void
{
$this->setToolsEnabled();
}
```

### setToolsDisabled
Disables the Tools section, this includes the Toolbar, and Sort/Filter pills
```php
public function configure(): void
{
$this->setToolsDisabled();
}
```

### setToolBarEnabled
The Default Behaviour, ToolBar is Enabled. But will only be rendered if there are available/enabled elements
```php
public function configure(): void
{
$this->setToolBarEnabled();
}
```

### setToolBarDisabled
Disables the Toolbar, which contains the Reorder, Filters, Search, Column Select, Pagination buttons/options. Does not impact the Filter/Sort pills (if enabled)
```php
public function configure(): void
{
$this->setToolBarDisabled();
}
```
10 changes: 0 additions & 10 deletions resources/views/components/tools/toolbar.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
@aware(['component', 'tableName','isTailwind','isBootstrap'])
@props([])

@if ($this->hasConfigurableAreaFor('before-toolbar'))
@include($this->getConfigurableAreaFor('before-toolbar'), $this->getParametersForConfigurableArea('before-toolbar'))
@endif

<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,
Expand Down Expand Up @@ -90,9 +86,3 @@
<x-livewire-tables::tools.toolbar.items.filter-slidedown />
@endif


@if ($this->hasConfigurableAreaFor('after-toolbar'))
<div x-cloak x-show="!currentlyReorderingStatus" >
@include($this->getConfigurableAreaFor('after-toolbar'), $this->getParametersForConfigurableArea('after-toolbar'))
</div>
@endif
10 changes: 9 additions & 1 deletion resources/views/datatable.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@
@include($this->getConfigurableAreaFor('before-tools'), $this->getParametersForConfigurableArea('before-tools'))
@endif

@if($this->shouldShowTools)
<x-livewire-tables::tools>
@if ($this->showSortPillsSection)
<x-livewire-tables::tools.sorting-pills />
@endif
@if($this->showFilterPillsSection)
<x-livewire-tables::tools.filter-pills />
@endif
<x-livewire-tables::tools.toolbar />

@includeWhen($this->hasConfigurableAreaFor('before-toolbar'), $this->getConfigurableAreaFor('before-toolbar'), $this->getParametersForConfigurableArea('before-toolbar'))
@if($this->shouldShowToolBar)
<x-livewire-tables::tools.toolbar />
@endif
@includeWhen($this->hasConfigurableAreaFor('after-toolbar'), $this->getConfigurableAreaFor('after-toolbar'), $this->getParametersForConfigurableArea('after-toolbar'))

</x-livewire-tables::tools>
@endif

<x-livewire-tables::table>

Expand Down
40 changes: 40 additions & 0 deletions src/Traits/Configuration/ToolsConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Configuration;

trait ToolsConfiguration
{
public function setToolsStatus(bool $status): self
{
$this->toolsStatus = $status;

return $this;
}

public function setToolsEnabled(): self
{
return $this->setToolsStatus(true);
}

public function setToolsDisabled(): self
{
return $this->setToolsStatus(false);
}

public function setToolBarStatus(bool $status): self
{
$this->toolBarStatus = $status;

return $this;
}

public function setToolBarEnabled(): self
{
return $this->setToolBarStatus(true);
}

public function setToolBarDisabled(): self
{
return $this->setToolBarStatus(false);
}
}
3 changes: 2 additions & 1 deletion src/Traits/HasAllTraits.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ trait HasAllTraits
WithRefresh,
WithReordering,
WithSecondaryHeader,
WithTableAttributes;
WithTableAttributes,
WithTools;
}
2 changes: 1 addition & 1 deletion src/Traits/Helpers/SortingHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,6 @@ public function getDefaultSortingLabelDesc(): string
#[Computed]
public function showSortPillsSection(): bool
{
return $this->sortingPillsAreEnabled() && $this->hasSorts();
return $this->sortingIsEnabled() && $this->sortingPillsAreEnabled() && $this->hasSorts();
}
}
108 changes: 108 additions & 0 deletions src/Traits/Helpers/ToolsHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Helpers;

use Livewire\Attributes\Computed;

trait ToolsHelpers
{
public function getToolsStatus(): bool
{
return $this->toolsStatus;
}

public function getToolBarStatus(): bool
{
return $this->toolBarStatus;
}

#[Computed]
public function shouldShowTools(): bool
{
if ($this->getToolsStatus()) {
if ($this->shouldShowToolBar()) {
return true;
} else {
if ($this->showSortPillsSection()) { // Sort Pills Are Enabled
return true;
} elseif ($this->showFilterPillsSection()) { // Filter Pills Are Enable)
return true;
} else {
return false;
}
}
} else {
return false;
}
}

#[Computed]
public function shouldShowToolBar(): bool
{
if ($this->getToolsStatus() == false) {
return false;
}

if ($this->getToolBarStatus()) {
if (
$this->hasToolbarConfigurableAreas() || // Has Configured Toolbar Configurable Areas
$this->hasToolbarActions() || // Actions Exist In Toolbar
$this->hasToolbarReorder() || // If Reorder Is Enabled
$this->hasToolbarColumnSelect() || // Column Select Enabled
$this->displayToolbarSearch() || // If Search Is Enabled
$this->displayToolbarFilters() || // If Filters Are Enabled
$this->displayToolbarPagination() // Pagination Selection Is Enabled
) {
return true;
}

return false;
}

return false;
}

#[Computed]
public function displayToolbarPagination(): bool
{
return $this->paginationIsEnabled() && $this->perPageVisibilityIsEnabled();
}

#[Computed]
public function displayToolbarSearch(): bool
{
return $this->searchIsEnabled() && $this->searchVisibilityIsEnabled();
}

#[Computed]
public function displayToolbarFilters(): bool
{
if ($this->filtersAreEnabled() && $this->filtersVisibilityIsEnabled() && $this->hasVisibleFilters()) {
return true;
} elseif ($this->filtersAreEnabled() && $this->showBulkActionsDropdownAlpine() && $this->shouldAlwaysHideBulkActionsDropdownOption() != true) {
return true;
}

return false;
}

protected function hasToolbarColumnSelect(): bool
{
return $this->columnSelectIsEnabled();
}

protected function hasToolbarReorder(): bool
{
return $this->reorderIsEnabled();
}

protected function hasToolbarConfigurableAreas(): bool
{
return $this->hasConfigurableAreaFor('toolbar-left-end') || $this->hasConfigurableAreaFor('toolbar-left-start') || $this->hasConfigurableAreaFor('toolbar-right-start') || $this->hasConfigurableAreaFor('toolbar-right-end');
}

protected function hasToolbarActions(): bool
{
return $this->hasActions() && $this->showActionsInToolbar();
}
}
16 changes: 16 additions & 0 deletions src/Traits/WithTools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits;

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

trait WithTools
{
use ToolsConfiguration,
ToolsHelpers;

protected bool $toolsStatus = true;

protected bool $toolBarStatus = true;
}
103 changes: 103 additions & 0 deletions tests/Traits/Helpers/ToolsHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Traits\Helpers;

use Rappasoft\LaravelLivewireTables\Tests\TestCase;

final class ToolsHelpersTest extends TestCase
{
public function test_can_get_toolbar_status(): void
{
$this->assertTrue($this->basicTable->getToolBarStatus());
$this->assertTrue($this->basicTable->getToolsStatus());

$this->basicTable->setToolBarDisabled();

$this->assertFalse($this->basicTable->getToolBarStatus());
$this->assertTrue($this->basicTable->getToolsStatus());

}

public function test_can_get_tools_status(): void
{
$this->assertTrue($this->basicTable->getToolsStatus());
$this->assertTrue($this->basicTable->getToolBarStatus());

$this->basicTable->setToolsDisabled();

$this->assertFalse($this->basicTable->getToolsStatus());
$this->assertTrue($this->basicTable->getToolBarStatus());
}

public function test_can_get_tools_should_display(): void
{
$this->assertTrue($this->basicTable->getToolsStatus());
$this->assertTrue($this->basicTable->getToolBarStatus());
$this->assertTrue($this->basicTable->shouldShowTools());
$this->assertTrue($this->basicTable->shouldShowToolBar());

$this->basicTable->setToolsDisabled();

$this->assertFalse($this->basicTable->getToolsStatus());
$this->assertTrue($this->basicTable->getToolBarStatus());
$this->assertFalse($this->basicTable->shouldShowTools());
$this->assertFalse($this->basicTable->shouldShowToolBar());
}

public function test_can_get_toolbar_display(): void
{
$this->assertTrue($this->basicTable->getToolsStatus());
$this->assertTrue($this->basicTable->getToolBarStatus());
$this->basicTable->setFiltersDisabled();
$this->basicTable->setSingleSortingDisabled();
$this->basicTable->setSearchDisabled();
$this->basicTable->setColumnSelectDisabled();
$this->basicTable->setPerPageVisibilityDisabled();
$this->basicTable->setSortingDisabled();
$this->basicTable->setSortingPillsDisabled();

$this->assertTrue($this->basicTable->getToolsStatus());
$this->assertTrue($this->basicTable->getToolBarStatus());
$this->assertFalse($this->basicTable->shouldShowToolBar());

}

public function test_can_get_tools_display(): void
{
$this->assertTrue($this->basicTable->getToolsStatus());
$this->assertTrue($this->basicTable->getToolBarStatus());
$this->basicTable->setSearchDisabled()
->setColumnSelectDisabled()
->setPerPageVisibilityDisabled();
$this->basicTable->setSorts(['id' => 'asc', 'name' => 'desc']);

$this->assertTrue($this->basicTable->shouldShowToolBar());
$this->assertTrue($this->basicTable->shouldShowTools());

$this->basicTable->setFiltersDisabled();

$this->assertFalse($this->basicTable->shouldShowToolBar());
$this->assertTrue($this->basicTable->shouldShowTools());

$this->basicTable->setSortingDisabled();

$this->assertFalse($this->basicTable->shouldShowToolBar());
$this->assertFalse($this->basicTable->shouldShowTools());

$this->basicTable->setFiltersEnabled();

$this->assertTrue($this->basicTable->shouldShowToolBar());
$this->assertTrue($this->basicTable->shouldShowTools());

$this->basicTable->clearSorts();

$this->assertTrue($this->basicTable->shouldShowToolBar());
$this->assertTrue($this->basicTable->shouldShowTools());

$this->basicTable->setFiltersDisabled();

$this->assertFalse($this->basicTable->shouldShowToolBar());
$this->assertFalse($this->basicTable->shouldShowTools());

}
}

0 comments on commit c14692b

Please sign in to comment.