diff options
Diffstat (limited to 'src/Columns')
-rw-r--r-- | src/Columns/ButtonColumn.php | 18 | ||||
-rw-r--r-- | src/Columns/Column.php | 20 | ||||
-rw-r--r-- | src/Columns/ElementColumn.php | 39 | ||||
-rw-r--r-- | src/Columns/LinkColumn.php | 24 |
4 files changed, 83 insertions, 18 deletions
diff --git a/src/Columns/ButtonColumn.php b/src/Columns/ButtonColumn.php new file mode 100644 index 0000000..794b175 --- /dev/null +++ b/src/Columns/ButtonColumn.php @@ -0,0 +1,18 @@ +<?php + +namespace Lightscale\LaralightTables\Columns; + +use Illuminate\Database\Eloquent\Model; + +class ButtonColumn extends ElementColumn +{ + protected string $element = 'button'; + + protected function getElemAttributes(Model $row) : array + { + return parent::getElemAttributes($row) + [ + 'type' => 'button' + ]; + } + +} diff --git a/src/Columns/Column.php b/src/Columns/Column.php index f3fe811..94e386f 100644 --- a/src/Columns/Column.php +++ b/src/Columns/Column.php @@ -19,9 +19,11 @@ class Column { private ?Closure $sortFn = null; private ?Closure $tdAttributesFn = null; + private ?string $colClass = null; + public function __construct( public string $name, - public ?string $title = null + private ?string $title = null ) { $this->showInSelect = $this->title !== null; } @@ -36,6 +38,11 @@ class Column { $this->table = $table; } + public function getTitle() + { + return $this->title; + } + private function defaultSlot(Model $row) { return $row->{$this->name}; @@ -53,6 +60,17 @@ class Column { return $this; } + public function colClass(string $v) : static + { + $this->colClass = $v; + return $this; + } + + public function getColClass() : ?string + { + return $this->colClass; + } + public function tdAttributes(callable $fn) : static { $this->tdAttributesFn = Closure::fromCallable($fn); 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}>" + ); + } + +} diff --git a/src/Columns/LinkColumn.php b/src/Columns/LinkColumn.php index 7ba3a95..edb1140 100644 --- a/src/Columns/LinkColumn.php +++ b/src/Columns/LinkColumn.php @@ -3,15 +3,14 @@ namespace Lightscale\LaralightTables\Columns; use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\HtmlString; -use Illuminate\View\ComponentAttributeBag; use Closure; -class LinkColumn extends Column +class LinkColumn extends ElementColumn { + protected string $element = 'a'; + private Closure $urlFn; - private ?Closure $linkAttributesFn = null; public function url(callable $fn) : static { @@ -19,20 +18,11 @@ class LinkColumn extends Column return $this; } - public function attributes(callable $fn) : static - { - $this->linkAttributesFn = $fn; - return $this; - } - - protected function getContent(Model $row) + public function getElemAttributes(Model $row) : array { - $content = parent::getContent($row); - $url = $this->urlFn->call($this, $row); - $attributes = $this->linkAttributesFn?->call($this, $row) ?? []; - $attributes = (new ComponentAttributeBag(['href' => $url] + $attributes))->toHtml(); - - return new HtmlString("<a {$attributes}>{$content}</a>"); + return parent::getElemAttributes($row) + [ + 'href' => $this->urlFn->call($this, $row), + ]; } } |