diff --git a/src/Views/Columns/ComponentColumn.php b/src/Views/Columns/ComponentColumn.php index 82eccf622..ad4b4ee60 100644 --- a/src/Views/Columns/ComponentColumn.php +++ b/src/Views/Columns/ComponentColumn.php @@ -7,17 +7,14 @@ use Illuminate\View\ComponentAttributeBag; use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Views\Column; +use Rappasoft\LaravelLivewireTables\Views\Traits\Columns\{HasComponentView, HasSlot}; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\ComponentColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\ComponentColumnHelpers; class ComponentColumn extends Column { - use ComponentColumnConfiguration, - ComponentColumnHelpers; - - protected string $componentView; - - protected mixed $slotCallback = null; + use HasComponentView, + HasSlot; public function __construct(string $title, ?string $from = null) { @@ -46,10 +43,7 @@ public function getContents(Model $row): null|string|HtmlString|DataTableConfigu } } if ($this->hasSlotCallback()) { - $slotContent = call_user_func($this->getSlotCallback(), $value, $row, $this); - if (is_string($slotContent)) { - $slotContent = new HtmlString($slotContent); - } + $slotContent = $this->getSlotContent($row, $value); } return view($this->getComponentView(), [ diff --git a/src/Views/Columns/ViewComponentColumn.php b/src/Views/Columns/ViewComponentColumn.php index 17b012130..78a070678 100644 --- a/src/Views/Columns/ViewComponentColumn.php +++ b/src/Views/Columns/ViewComponentColumn.php @@ -6,6 +6,7 @@ use Illuminate\Support\HtmlString; use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; use Rappasoft\LaravelLivewireTables\Views\Column; +use Rappasoft\LaravelLivewireTables\Views\Traits\Columns\HasComponentView; use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\ViewComponentColumnConfiguration; use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\ViewComponentColumnHelpers; use ReflectionClass; @@ -13,9 +14,8 @@ class ViewComponentColumn extends Column { use ViewComponentColumnConfiguration, - ViewComponentColumnHelpers; - - protected ?string $componentView; + ViewComponentColumnHelpers, + HasComponentView; protected ?string $customComponentView; diff --git a/src/Views/Traits/Columns/HasComponentView.php b/src/Views/Traits/Columns/HasComponentView.php new file mode 100644 index 000000000..27fccc623 --- /dev/null +++ b/src/Views/Traits/Columns/HasComponentView.php @@ -0,0 +1,25 @@ +componentView = 'components.'.$component; + + return $this; + } + + public function getComponentView(): string + { + return $this->componentView; + } + + public function hasComponentView(): bool + { + return isset($this->componentView); + } +} diff --git a/src/Views/Traits/Columns/HasSlot.php b/src/Views/Traits/Columns/HasSlot.php new file mode 100644 index 000000000..aecfffc77 --- /dev/null +++ b/src/Views/Traits/Columns/HasSlot.php @@ -0,0 +1,39 @@ +getSlotCallback(), $value, $row, $this); + if (is_string($slotContent)) { + $slotContent = new HtmlString($slotContent); + } + + return $slotContent; + } + + public function getSlotCallback(): ?callable + { + return $this->slotCallback; + } + + public function hasSlotCallback(): bool + { + return isset($this->slotCallback) && $this->slotCallback !== null; + } + + public function slot(callable $callback): self + { + $this->slotCallback = $callback; + + return $this; + } +} diff --git a/src/Views/Traits/Configuration/ComponentColumnConfiguration.php b/src/Views/Traits/Configuration/ComponentColumnConfiguration.php deleted file mode 100644 index 2f627ff15..000000000 --- a/src/Views/Traits/Configuration/ComponentColumnConfiguration.php +++ /dev/null @@ -1,20 +0,0 @@ -componentView = 'components.'.$component; - - return $this; - } - - public function slot(callable $callback): self - { - $this->slotCallback = $callback; - - return $this; - } -} diff --git a/src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php b/src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php index 17a630742..87c4f7995 100644 --- a/src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php +++ b/src/Views/Traits/Configuration/ViewComponentColumnConfiguration.php @@ -4,16 +4,6 @@ trait ViewComponentColumnConfiguration { - /** - * Defines the View Component to be used for the Column - */ - public function component(string $component): self - { - $this->componentView = $component; - - return $this; - } - public function customComponent(string $customComponentView): self { $this->customComponentView = $customComponentView; diff --git a/src/Views/Traits/Helpers/ComponentColumnHelpers.php b/src/Views/Traits/Helpers/ComponentColumnHelpers.php deleted file mode 100644 index eebd83d43..000000000 --- a/src/Views/Traits/Helpers/ComponentColumnHelpers.php +++ /dev/null @@ -1,26 +0,0 @@ -slotCallback; - } - - public function hasSlotCallback(): bool - { - return $this->slotCallback !== null; - } - - public function getComponentView(): string - { - return $this->componentView; - } - - public function hasComponentView(): bool - { - return isset($this->componentView); - } -} diff --git a/src/Views/Traits/Helpers/ViewComponentColumnHelpers.php b/src/Views/Traits/Helpers/ViewComponentColumnHelpers.php index 0dcddc278..f1c4d9bb7 100644 --- a/src/Views/Traits/Helpers/ViewComponentColumnHelpers.php +++ b/src/Views/Traits/Helpers/ViewComponentColumnHelpers.php @@ -4,22 +4,6 @@ trait ViewComponentColumnHelpers { - /** - * Retrieves the defined Component View - */ - public function getComponentView(): string - { - return $this->componentView; - } - - /** - * Determines whether a Component View has been set - */ - public function hasComponentView(): bool - { - return isset($this->componentView); - } - public function hasCustomComponent(): bool { return isset($this->customComponentView);