summaryrefslogtreecommitdiff
path: root/src/Columns/ElementColumn.php
diff options
context:
space:
mode:
authorSam Light <samlight1994@gmail.com>2024-12-27 23:04:40 +0000
committerSam Light <samlight1994@gmail.com>2024-12-27 23:04:40 +0000
commitb19d30c7ad5a041ccc42ca9542150c7b371fe4fe (patch)
treefcdba3b9681cef60e0e774e08c377d03c5d1defb /src/Columns/ElementColumn.php
parent3c33b9201ee02d8b73986e489b9637d2875413de (diff)
Lots of updates
Diffstat (limited to 'src/Columns/ElementColumn.php')
-rw-r--r--src/Columns/ElementColumn.php39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/Columns/ElementColumn.php b/src/Columns/ElementColumn.php
new file mode 100644
index 0000000..5ad7c7c
--- /dev/null
+++ b/src/Columns/ElementColumn.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Lightscale\LaralightTables\Columns;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\HtmlString;
+use Illuminate\View\ComponentAttributeBag;
+
+use Closure;
+
+abstract class ElementColumn extends Column
+{
+
+ protected string $element;
+ private ?Closure $elemAttributesFn = null;
+
+ public function attributes(callable $fn) : static
+ {
+ $this->elemAttributesFn = Closure::fromCallable($fn);
+ return $this;
+ }
+
+ protected function getElemAttributes(Model $row) : array
+ {
+ return $this->elemAttributesFn?->call($this, $row) ?? [];
+ }
+
+ protected function getContent(Model $row)
+ {
+ $content = parent::getContent($row);
+ $attributes = new ComponentAttributeBag($this->getElemAttributes($row));
+ $attributes = $attributes->toHtml();
+
+ return new HtmlString(
+ "<{$this->element} {$attributes}>{$content}</{$this->element}>"
+ );
+ }
+
+}