summaryrefslogtreecommitdiff
path: root/src
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
parent3c33b9201ee02d8b73986e489b9637d2875413de (diff)
Lots of updates
Diffstat (limited to 'src')
-rw-r--r--src/Columns/ButtonColumn.php18
-rw-r--r--src/Columns/Column.php20
-rw-r--r--src/Columns/ElementColumn.php39
-rw-r--r--src/Columns/LinkColumn.php24
-rw-r--r--src/TableComponent.php13
5 files changed, 91 insertions, 23 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),
+ ];
}
}
diff --git a/src/TableComponent.php b/src/TableComponent.php
index 04d457e..da31a90 100644
--- a/src/TableComponent.php
+++ b/src/TableComponent.php
@@ -4,6 +4,7 @@ namespace Lightscale\LaralightTables;
use Livewire\Component;
use Livewire\WithPagination;
+use Livewire\Attributes\Url;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
@@ -28,6 +29,8 @@ abstract class TableComponent extends Component
// Properties
public string $search = '';
+
+ #[Url]
public int $pageSize = 0;
public array $activeColumns = [];
@@ -72,17 +75,17 @@ abstract class TableComponent extends Component
return $query;
}
+ private $columnsCache = null;
+
protected function getColumns()
{
- static $columns = null;
-
- if($columns === null) {
- $columns = collect($this->columns())->each(
+ if($this->columnsCache === null) {
+ $this->columnsCache = collect($this->columns())->each(
fn($c) => $c->setTable($this)
);
}
- return $columns;
+ return $this->columnsCache;
}
public function render()