Skip to content

Commit

Permalink
BooleanColumn - Toggleable Callback (#1892)
Browse files Browse the repository at this point in the history
* Add Docs, Code for Toggleable Columns

* Add Confirmation Option

* Tweak Blade - Standardise

* Add Tests for Toggleable

---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Aug 25, 2024
1 parent d0da3b1 commit 2a977ec
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 50 deletions.
40 changes: 40 additions & 0 deletions docs/column-types/boolean_columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,46 @@ BooleanColumn::make('Active')
->yesNo()
```

### Toggleable

You may call a defined public function, which should live within your Table Component, to allow "toggling" against your database:

```php
BooleanColumn::make('Active', 'status')
->toggleable('changeStatus'),
```

Then your "changeStatus" method may look like
```php
public function changeStatus(int $id)
{
$item = $this->model::find($id);
$item->status = !$item->status;
$item->save();
}
```

### Toggleable Confirmation Message

You may define a confirmation message prior to executing your toggleable() method. The method will only be executed upon confirming.
```php
BooleanColumn::make('Active', 'status')
->confirmMessage('Are you sure that you want to change the status?')
->toggleable('changeStatus'),
```

Then your "changeStatus" method may look like
```php
public function changeStatus(int $id)
{
$item = $this->model::find($id);
$item->status = !$item->status;
$item->save();
}
```


### Additional Methods
Please also see the following for other available methods:
<ul>
<li>
Expand Down
75 changes: 38 additions & 37 deletions resources/views/includes/columns/boolean.blade.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,27 @@
@if ($isTailwind)
@if ($status)
@if ($type === 'icons')
@if ($successValue === true)
<x-heroicon-o-check-circle class="inline-block h-5 w-5 text-green-500" />
@else
<x-heroicon-o-check-circle class="inline-block h-5 w-5 text-red-500" />
@endif
@elseif ($type === 'yes-no')
@if ($successValue === true)
<span>Yes</span>
@else
<span>No</span>
@endif
@endif
@else
@if ($type === 'icons')
@if ($successValue === false)
<x-heroicon-o-x-circle class="inline-block h-5 w-5 text-green-500" />
@else
<x-heroicon-o-x-circle class="inline-block h-5 w-5 text-red-500" />
@endif
@elseif ($type === 'yes-no')
@if ($successValue === false)
<span>Yes</span>
@else
<span>No</span>
@endif
@endif
@endif
@elseif ($isBootstrap)
@if($isToggleable && $toggleMethod != '')
<button wire:click="{{ $toggleMethod }}('{{ $rowPrimaryKey }}')"
@if($hasConfirmMessage) wire:confirm="{{ $confirmMessage }}" @endif
>
@endif
@if ($status)
@if ($type === 'icons')
@if ($successValue === true)
<x-heroicon-o-check-circle class="d-inline-block text-success laravel-livewire-tables-btn-small" />
@else
<x-heroicon-o-check-circle class="d-inline-block text-danger laravel-livewire-tables-btn-small" />
<x-heroicon-o-check-circle
@class(
[
"inline-block h-5 w-5 text-green-500" => $isTailwind,
"d-inline-block text-success laravel-livewire-tables-btn-small" => $isBootstrap
]
)
/>
@else
<x-heroicon-o-check-circle @class(
[
"inline-block h-5 w-5 text-red-500" => $isTailwind,
"d-inline-block text-danger laravel-livewire-tables-btn-small" => $isBootstrap
]
)
/>
@endif
@elseif ($type === 'yes-no')
@if ($successValue === true)
Expand All @@ -46,9 +33,21 @@
@else
@if ($type === 'icons')
@if ($successValue === false)
<x-heroicon-o-x-circle class="d-inline-block text-success laravel-livewire-tables-btn-small" />
@else
<x-heroicon-o-x-circle class="d-inline-block text-danger laravel-livewire-tables-btn-small" />
<x-heroicon-o-x-circle @class(
[
"inline-block h-5 w-5 text-green-500" => $isTailwind,
"d-inline-block text-success laravel-livewire-tables-btn-small" => $isBootstrap
]
)
/>
@else
<x-heroicon-o-x-circle @class(
[
"inline-block h-5 w-5 text-red-500" => $isTailwind,
"d-inline-block text-danger laravel-livewire-tables-btn-small" => $isBootstrap
]
)
/>
@endif
@elseif ($type === 'yes-no')
@if ($successValue === false)
Expand All @@ -58,4 +57,6 @@
@endif
@endif
@endif
@if($isToggleable && $toggleMethod != '')
</button>
@endif
11 changes: 11 additions & 0 deletions src/Views/Columns/BooleanColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\BooleanColumnConfiguration;
use Rappasoft\LaravelLivewireTables\Views\Traits\Core\HasCallback;
use Rappasoft\LaravelLivewireTables\Views\Traits\Core\HasConfirmation;
use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\BooleanColumnHelpers;

class BooleanColumn extends Column
{
use BooleanColumnConfiguration,
BooleanColumnHelpers,
HasConfirmation,
HasCallback;

protected string $type = 'icons';
Expand All @@ -21,6 +23,10 @@ class BooleanColumn extends Column

protected string $view = 'livewire-tables::includes.columns.boolean';

protected bool $isToggleable = false;

protected ?string $toggleMethod;

public function getContents(Model $row): null|string|\Illuminate\Support\HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
{
if ($this->isLabel()) {
Expand All @@ -30,6 +36,11 @@ public function getContents(Model $row): null|string|\Illuminate\Support\HtmlStr
$value = $this->getValue($row);

return view($this->getView())
->withRowPrimaryKey($row->{$row->getKeyName()})
->withIsToggleable($this->getIsToggleable())
->withToggleMethod($this->getIsToggleable() ? $this->getToggleMethod() : '')
->withHasConfirmMessage($this->hasConfirmMessage())
->withConfirmMessage($this->hasConfirmMessage() ? $this->getConfirmMessage() : '')
->withIsTailwind($this->isTailwind())
->withIsBootstrap($this->isBootstrap())
->withSuccessValue($this->getSuccessValue())
Expand Down
22 changes: 9 additions & 13 deletions src/Views/Traits/Configuration/BooleanColumnConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,39 @@

trait BooleanColumnConfiguration
{
/**
* @return $this
*/
public function setSuccessValue(bool $value): self
{
$this->successValue = $value;

return $this;
}

/**
* @return $this
*/
public function setView(string $view): self
{
$this->view = $view;

return $this;
}

/**
* @return $this
*/
public function icons(): self
{
$this->type = 'icons';

return $this;
}

/**
* @return $this
*/
public function yesNo()
public function yesNo(): self
{
$this->type = 'yes-no';

return $this;
}

public function toggleable(string $toggleMethod): self
{
$this->isToggleable = true;
$this->toggleMethod = $toggleMethod;

return $this;
}
}
10 changes: 10 additions & 0 deletions src/Views/Traits/Helpers/BooleanColumnHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ public function getType(): string
{
return $this->type;
}

public function getIsToggleable(): bool
{
return $this->isToggleable ?? false;
}

public function getToggleMethod(): ?string
{
return $this->toggleMethod ?? null;
}
}
29 changes: 29 additions & 0 deletions tests/Views/Columns/BooleanColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,33 @@ public function test_can_return_status_false(): void
$curVal = $column->hasCallback() ? call_user_func($column->getCallback(), $value, $row) : (bool) $value === true;
$this->assertSame($curVal, false);
}

public function test_can_set_toggleable(): void
{
$column = BooleanColumn::make('Name', 'name');

$this->assertFalse($column->getIsToggleable());
$this->assertNull($column->getToggleMethod());
$column->toggleable('changeStatus');
$this->assertTrue($column->getIsToggleable());
$this->assertSame('changeStatus', $column->getToggleMethod());
}

public function test_can_set_toggleable_with_confirm_message(): void
{
$column = BooleanColumn::make('Name', 'name')
->toggleable('changeStatus');

$this->assertTrue($column->getIsToggleable());
$this->assertSame('changeStatus', $column->getToggleMethod());

$this->assertFalse($column->hasConfirmMessage());

$column->confirmMessage('Are you sure?');

$this->assertTrue($column->hasConfirmMessage());

$this->assertSame('Are you sure?', $column->getConfirmMessage());

}
}

0 comments on commit 2a977ec

Please sign in to comment.