Skip to content

Commit bd6606c

Browse files
committed
Add boolean column yes/no configuration
1 parent ab019a9 commit bd6606c

File tree

7 files changed

+101
-12
lines changed

7 files changed

+101
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
44

55
## [Unreleased]
66

7+
### Added
8+
9+
- Added functionality to display BooleanColumn as Yes/No instead of icons.
10+
711
## [2.5.0] - 2022-05-03
812

913
### Added

docs/columns/other-column-types.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ BooleanColumn::make('Active')
7171
}),
7272
```
7373

74+
### Different types of boolean display
75+
76+
By default, the BooleanColumn displays icons.
77+
78+
If you would like the BooleanColumn to display a plain Yes/No, you can set:
79+
80+
```php
81+
BooleanColumn::make('Active')
82+
->yesNo()
83+
```
84+
7485
## Image Columns
7586

7687
Image columns provide a way to display images in your table without having to use `format()` or partial views:

resources/views/includes/columns/boolean.blade.php

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,54 @@
44

55
@if ($theme === 'tailwind')
66
@if ($status)
7-
<svg xmlns="http://www.w3.org/2000/svg" class="inline-block h-5 w-5 @if ($successValue === true) text-green-500 @else text-red-500 @endif" fill="none" viewBox="0 0 24 24" stroke="currentColor">
8-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
9-
</svg>
7+
@if ($type === 'icons')
8+
<svg xmlns="http://www.w3.org/2000/svg" class="inline-block h-5 w-5 @if ($successValue === true) text-green-500 @else text-red-500 @endif" fill="none" viewBox="0 0 24 24" stroke="currentColor">
9+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
10+
</svg>
11+
@elseif ($type === 'yes-no')
12+
@if ($successValue === true)
13+
<span>Yes</span>
14+
@else
15+
<span>No</span>
16+
@endif
17+
@endif
1018
@else
11-
<svg xmlns="http://www.w3.org/2000/svg" class="inline-block h-5 w-5 @if ($successValue === false) text-green-500 @else text-red-500 @endif" fill="none" viewBox="0 0 24 24" stroke="currentColor">
12-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z" />
13-
</svg>
19+
@if ($type === 'icons')
20+
<svg xmlns="http://www.w3.org/2000/svg" class="inline-block h-5 w-5 @if ($successValue === false) text-green-500 @else text-red-500 @endif" fill="none" viewBox="0 0 24 24" stroke="currentColor">
21+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z" />
22+
</svg>
23+
@elseif ($type === 'yes-no')
24+
@if ($successValue === false)
25+
<span>Yes</span>
26+
@else
27+
<span>No</span>
28+
@endif
29+
@endif
1430
@endif
1531
@elseif ($theme === 'bootstrap-4' || $theme === 'bootstrap-5')
1632
@if ($status)
17-
<svg xmlns="http://www.w3.org/2000/svg" style="width:1.2em;height:1.2em;" class="d-inline-block @if ($successValue === true) text-success @else text-danger @endif" fill="none" viewBox="0 0 24 24" stroke="currentColor">
18-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
19-
</svg>
33+
@if ($type === 'icons')
34+
<svg xmlns="http://www.w3.org/2000/svg" style="width:1.2em;height:1.2em;" class="d-inline-block @if ($successValue === true) text-success @else text-danger @endif" fill="none" viewBox="0 0 24 24" stroke="currentColor">
35+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
36+
</svg>
37+
@elseif ($type === 'yes-no')
38+
@if ($successValue === true)
39+
<span>Yes</span>
40+
@else
41+
<span>No</span>
42+
@endif
43+
@endif
2044
@else
21-
<svg xmlns="http://www.w3.org/2000/svg" style="width:1.2em;height:1.2em;" class="d-inline-block @if ($successValue === false) text-success @else text-danger @endif" fill="none" viewBox="0 0 24 24" stroke="currentColor">
22-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z" />
23-
</svg>
45+
@if ($type === 'icons')
46+
<svg xmlns="http://www.w3.org/2000/svg" style="width:1.2em;height:1.2em;" class="d-inline-block @if ($successValue === false) text-success @else text-danger @endif" fill="none" viewBox="0 0 24 24" stroke="currentColor">
47+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z" />
48+
</svg>
49+
@elseif ($type === 'yes-no')
50+
@if ($successValue === false)
51+
<span>Yes</span>
52+
@else
53+
<span>No</span>
54+
@endif
55+
@endif
2456
@endif
2557
@endif

src/Views/Columns/BooleanColumn.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class BooleanColumn extends Column
1313
use BooleanColumnConfiguration,
1414
BooleanColumnHelpers;
1515

16+
protected string $type = 'icons';
1617
protected bool $successValue = true;
1718
protected string $view = 'livewire-tables::includes.columns.boolean';
1819
protected $callback;
@@ -28,6 +29,7 @@ public function getContents(Model $row)
2829
return view($this->getView())
2930
->withComponent($this->getComponent())
3031
->withSuccessValue($this->getSuccessValue())
32+
->withType($this->getType())
3133
->withStatus($this->hasCallback() ? call_user_func($this->getCallback(), $value, $row) : (bool)$value === true);
3234
}
3335
}

src/Views/Traits/Configuration/BooleanColumnConfiguration.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,24 @@ public function setView(string $view): self
4141

4242
return $this;
4343
}
44+
45+
/**
46+
* @return $this
47+
*/
48+
public function icons(): self
49+
{
50+
$this->type = 'icons';
51+
52+
return $this;
53+
}
54+
55+
/**
56+
* @return $this
57+
*/
58+
public function yesNo()
59+
{
60+
$this->type = 'yes-no';
61+
62+
return $this;
63+
}
4464
}

src/Views/Traits/Helpers/BooleanColumnHelpers.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,12 @@ public function getView(): string
3737
{
3838
return $this->view;
3939
}
40+
41+
/**
42+
* @return string
43+
*/
44+
public function getType(): string
45+
{
46+
return $this->type;
47+
}
4048
}

tests/Views/BooleanColumnTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,16 @@ public function boolean_column_can_not_be_a_label(): void
1616

1717
BooleanColumn::make('Name')->label(fn () => 'My Label')->getContents(Pet::find(1));
1818
}
19+
20+
/** @test */
21+
public function boolean_column_can_be_yes_no(): void
22+
{
23+
$column = BooleanColumn::make('Name');
24+
25+
$this->assertEquals('icons', $column->getType());
26+
27+
$column->yesNo();
28+
29+
$this->assertEquals('yes-no', $column->getType());
30+
}
1931
}

0 commit comments

Comments
 (0)