Skip to content

Commit 19b3723

Browse files
authored
Clean up Column Component View and Slot behaviours (rappasoft#2169)
* Clean up Column Component View and Slot behaviours * Fix styling * Restore Trait --------- Co-authored-by: lrljoe <[email protected]>
1 parent 1d83e0e commit 19b3723

File tree

8 files changed

+71
-85
lines changed

8 files changed

+71
-85
lines changed

src/Views/Columns/ComponentColumn.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@
77
use Illuminate\View\ComponentAttributeBag;
88
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
99
use Rappasoft\LaravelLivewireTables\Views\Column;
10+
use Rappasoft\LaravelLivewireTables\Views\Traits\Columns\{HasComponentView, HasSlot};
1011
use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\ComponentColumnConfiguration;
1112
use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\ComponentColumnHelpers;
1213

1314
class ComponentColumn extends Column
1415
{
15-
use ComponentColumnConfiguration,
16-
ComponentColumnHelpers;
17-
18-
protected string $componentView;
19-
20-
protected mixed $slotCallback = null;
16+
use HasComponentView,
17+
HasSlot;
2118

2219
public function __construct(string $title, ?string $from = null)
2320
{
@@ -46,10 +43,7 @@ public function getContents(Model $row): null|string|HtmlString|DataTableConfigu
4643
}
4744
}
4845
if ($this->hasSlotCallback()) {
49-
$slotContent = call_user_func($this->getSlotCallback(), $value, $row, $this);
50-
if (is_string($slotContent)) {
51-
$slotContent = new HtmlString($slotContent);
52-
}
46+
$slotContent = $this->getSlotContent($row, $value);
5347
}
5448

5549
return view($this->getComponentView(), [

src/Views/Columns/ViewComponentColumn.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
use Illuminate\Support\HtmlString;
77
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
88
use Rappasoft\LaravelLivewireTables\Views\Column;
9+
use Rappasoft\LaravelLivewireTables\Views\Traits\Columns\HasComponentView;
910
use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\ViewComponentColumnConfiguration;
1011
use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\ViewComponentColumnHelpers;
1112
use ReflectionClass;
1213

1314
class ViewComponentColumn extends Column
1415
{
1516
use ViewComponentColumnConfiguration,
16-
ViewComponentColumnHelpers;
17-
18-
protected ?string $componentView;
17+
ViewComponentColumnHelpers,
18+
HasComponentView;
1919

2020
protected ?string $customComponentView;
2121

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Views\Traits\Columns;
4+
5+
trait HasComponentView
6+
{
7+
protected string $componentView;
8+
9+
public function component(string $component): self
10+
{
11+
$this->componentView = 'components.'.$component;
12+
13+
return $this;
14+
}
15+
16+
public function getComponentView(): string
17+
{
18+
return $this->componentView;
19+
}
20+
21+
public function hasComponentView(): bool
22+
{
23+
return isset($this->componentView);
24+
}
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Views\Traits\Columns;
4+
5+
use Closure;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Support\HtmlString;
8+
9+
trait HasSlot
10+
{
11+
protected ?Closure $slotCallback = null;
12+
13+
public function getSlotContent(Model $row, mixed $value): string|HtmlString
14+
{
15+
$slotContent = call_user_func($this->getSlotCallback(), $value, $row, $this);
16+
if (is_string($slotContent)) {
17+
$slotContent = new HtmlString($slotContent);
18+
}
19+
20+
return $slotContent;
21+
}
22+
23+
public function getSlotCallback(): ?callable
24+
{
25+
return $this->slotCallback;
26+
}
27+
28+
public function hasSlotCallback(): bool
29+
{
30+
return isset($this->slotCallback) && $this->slotCallback !== null;
31+
}
32+
33+
public function slot(callable $callback): self
34+
{
35+
$this->slotCallback = $callback;
36+
37+
return $this;
38+
}
39+
}

src/Views/Traits/Configuration/ComponentColumnConfiguration.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,6 @@
44

55
trait ViewComponentColumnConfiguration
66
{
7-
/**
8-
* Defines the View Component to be used for the Column
9-
*/
10-
public function component(string $component): self
11-
{
12-
$this->componentView = $component;
13-
14-
return $this;
15-
}
16-
177
public function customComponent(string $customComponentView): self
188
{
199
$this->customComponentView = $customComponentView;

src/Views/Traits/Helpers/ComponentColumnHelpers.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/Views/Traits/Helpers/ViewComponentColumnHelpers.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@
44

55
trait ViewComponentColumnHelpers
66
{
7-
/**
8-
* Retrieves the defined Component View
9-
*/
10-
public function getComponentView(): string
11-
{
12-
return $this->componentView;
13-
}
14-
15-
/**
16-
* Determines whether a Component View has been set
17-
*/
18-
public function hasComponentView(): bool
19-
{
20-
return isset($this->componentView);
21-
}
22-
237
public function hasCustomComponent(): bool
248
{
259
return isset($this->customComponentView);

0 commit comments

Comments
 (0)