blob: 0e785458b9b5407516a59f2efdb7a6d25c3614e1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
<?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;
/**
* @var ?Closure(Model): array<string, string>
*/
private ?Closure $elemAttributesFn = null;
public function attributes(callable $fn) : static
{
$this->elemAttributesFn = Closure::fromCallable($fn);
return $this;
}
/**
* @return array<string, string>
*/
protected function getElemAttributes(Model $row): array
{
return $this->elemAttributesFn?->call($this, $row) ?? [];
}
protected function getContent(Model $row): string
{
$content = parent::getContent($row);
$attributes = new ComponentAttributeBag($this->getElemAttributes($row));
$attributes = $attributes->toHtml();
return new HtmlString(
"<{$this->element} {$attributes}>{$content}</{$this->element}>"
);
}
}
|