Skip to content

Commit

Permalink
Merge pull request #618 from dansysanalyst/feat_add_ShowHideToggleable
Browse files Browse the repository at this point in the history
[Feature] ActionRules Show/Hide Toggleable
  • Loading branch information
luanfreitasdev authored Sep 10, 2022
2 parents 5ed1b43 + 29e6f25 commit a4966be
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<div x-data="pgToggleable({
id: '{{ $row->{$primaryKey} }}',
isHidden: {{ !$showToggleable ? 'true' : 'false' }},
tableName: '{{ $tableName }}',
field: '{{ $column->field }}',
toggle: {{ (int) $row->{$column->field} }},
trueValue: '{{ $column->toggleable['default'][0] }}',
falseValue: '{{ $column->toggleable['default'][1] }}'
})">
@if($column->toggleable['enabled'])
@if($column->toggleable['enabled'] && $showToggleable === true)
<div class="form-check form-switch">
<label>
<input x-on:click="save()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
@endphp
<div x-data="pgToggleable({
id: '{{ $row->{$primaryKey} }}',
isHidden: {{ !$showToggleable ? 'true' : 'false' }},
tableName: '{{ $tableName }}',
field: '{{ $column->field }}',
toggle: {{ (int)$row->{$column->field} }},
trueValue: '{{ $column->toggleable['default'][0] }}',
falseValue: '{{ $column->toggleable['default'][1] }}',
})">
@if($column->toggleable['enabled'] && !$showDefaultToggle)
@if($column->toggleable['enabled'] && !$showDefaultToggle && $showToggleable === true)
<div class="flex justify-center">
<div class="relative rounded-full w-12 h-6 transition duration-200 ease-linear"
:class="[toggle === 1 ? 'bg-blue-400 dark:bg-blue-500' : 'bg-slate-400']">
Expand Down
5 changes: 5 additions & 0 deletions resources/views/components/row.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
@endif
</span>
@elseif(count($column->toggleable) > 0)
@php
$rules = $actionRulesClass->recoverFromAction('pg:rows', $row);
$toggleableRules = collect(data_get($rules, 'showHideToggleable', []));
$showToggleable = ($toggleableRules->isEmpty() || $toggleableRules->last() == 'show');
@endphp
@include($theme->toggleable->view, ['tableName' => $tableName])
@else
<span class="@if($column->clickToCopy) {{ $theme->clickToCopy->spanClass }} @endif">
Expand Down
1 change: 1 addition & 0 deletions src/Helpers/ActionRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ActionRules
'pg:column',
'detailView',
'bladeComponent',
'showHideToggleable',
];

public function resolveRules(array $rules, object|array $row): Collection
Expand Down
20 changes: 20 additions & 0 deletions src/Rules/RuleRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,24 @@ public function detailView(string $detailView = null, array $options = []): Rule

return $this;
}

/**
* Show the toggleable in current row.
*/
public function showToggleable(): RuleRows
{
$this->rule['showHideToggleable'] = 'show';

return $this;
}

/**
* Hide the toggleable in current row.
*/
public function hideToggleable(): RuleRows
{
$this->rule['showHideToggleable'] = 'hide';

return $this;
}
}
66 changes: 66 additions & 0 deletions tests/Feature/ActionRules/ShowHideToggleable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

use function Pest\Livewire\livewire;

use PowerComponents\LivewirePowerGrid\Rules\Rule;
use PowerComponents\LivewirePowerGrid\Tests\Models\Dish;
use PowerComponents\LivewirePowerGrid\Tests\RulesShowHideToggleable;

it('hides all Toggleables except for id #2', function (string $component, object $params) {
livewire($component, ['join' => $params->join])
->call($params->theme)
->set('testActionRules', [
Rule::rows()
->when(fn (Dish $dish) => $dish->id < 50)
->hideToggleable(),

Rule::rows()
->when(fn (Dish $dish) => $dish->id == 2)
->showToggleable(),
])
->assertSeeHtmlInOrder([
"id: '1',",
'isHidden: true,',
])
->assertSeeHtmlInOrder([
"id: '2',",
'isHidden: false,',
])
->assertSeeHtmlInOrder([
"id: '3',",
'isHidden: true,',
]);
})->with('ShowHideToggleable')->group('actionRules');

it('hides only toggleables on id #2', function (string $component, object $params) {
livewire($component, ['join' => $params->join])
->call($params->theme)
->set('testActionRules', [
Rule::rows()
->when(fn (Dish $dish) => $dish->id < 50)
->showToggleable(),

Rule::rows()
->when(fn (Dish $dish) => $dish->id == 2)
->hideToggleable(),
])
->assertSeeHtmlInOrder([
"id: '1',",
'isHidden: false,',
])
->assertSeeHtmlInOrder([
"id: '2',",
'isHidden: true,',
])
->assertSeeHtmlInOrder([
"id: '3',",
'isHidden: false,',
]);
})->with('ShowHideToggleable')->group('actionRules');

dataset('ShowHideToggleable', [
'tailwind' => [RulesShowHideToggleable::class, (object) ['theme' => 'tailwind', 'join' => false]],
'bootstrap' => [RulesShowHideToggleable::class, (object) ['theme' => 'bootstrap', 'join' => false]],
'tailwind join' => [RulesShowHideToggleable::class, (object) ['theme' => 'tailwind', 'join' => true]],
'bootstrap join' => [RulesShowHideToggleable::class, (object) ['theme' => 'bootstrap', 'join' => true]],
]);
60 changes: 60 additions & 0 deletions tests/RulesShowHideToggleable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace PowerComponents\LivewirePowerGrid\Tests;

use PowerComponents\LivewirePowerGrid\Tests\Models\Dish;
use PowerComponents\LivewirePowerGrid\{
Column,
Footer,
Header,
PowerGrid,
PowerGridEloquent};

class RulesShowHideToggleable extends DishesTable
{
public array $testActionRules = [];

public function setUp(): array
{
return [
Header::make()
->showToggleColumns()
->showSearchInput(),

Footer::make()
->showPerPage()
->showRecordCount(),

];
}

public function addColumns(): PowerGridEloquent
{
return PowerGrid::eloquent()
->addColumn('id')
->addColumn('in_stock')
->addColumn('in_stock_label', function (Dish $dish) {
return ($dish->in_stock ? 'sim' : 'não');
});
}

public function columns(): array
{
return [
Column::add()
->title(__('ID'))
->field('id'),

Column::add()
->title(__('Em Estoque'))
->toggleable(true, 'sim', 'não')
->makeBooleanFilter('in_stock', 'sim', 'não')
->field('in_stock'),
];
}

public function actionRules(): array
{
return $this->testActionRules;
}
}

0 comments on commit a4966be

Please sign in to comment.