From b19d30c7ad5a041ccc42ca9542150c7b371fe4fe Mon Sep 17 00:00:00 2001 From: Sam Light Date: Fri, 27 Dec 2024 23:04:40 +0000 Subject: Lots of updates --- src/Columns/ButtonColumn.php | 18 ++++++++++++++++++ src/Columns/Column.php | 20 +++++++++++++++++++- src/Columns/ElementColumn.php | 39 +++++++++++++++++++++++++++++++++++++++ src/Columns/LinkColumn.php | 24 +++++++----------------- 4 files changed, 83 insertions(+), 18 deletions(-) create mode 100644 src/Columns/ButtonColumn.php create mode 100644 src/Columns/ElementColumn.php (limited to 'src/Columns') 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 @@ + '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 @@ +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}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("{$content}"); + return parent::getElemAttributes($row) + [ + 'href' => $this->urlFn->call($this, $row), + ]; } } -- cgit v1.2.3