Skip to content

Commit 467b123

Browse files
committed
Add definitions to LivewireComponentColumn - use str random
1 parent 2584eb9 commit 467b123

File tree

4 files changed

+126
-33
lines changed

4 files changed

+126
-33
lines changed

src/Traits/Helpers/TableAttributeHelpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function getTopLevelAttributesArray(): array
148148
return [
149149
'x-data' => 'laravellivewiretable($wire)',
150150
'x-init' => "setTableId('".$this->getTableAttributes()['id']."'); setAlpineBulkActions('".$this->showBulkActionsDropdownAlpine()."'); setPrimaryKeyName('".$this->getPrimaryKey()."');",
151-
'x-cloak',
151+
'x-cloak' => '',
152152
'x-show' => 'shouldBeDisplayed',
153153
'x-on:show-table.window' => 'showTable(event)',
154154
'x-on:hide-table.window' => 'hideTable(event)',

src/Views/Columns/LivewireComponentColumn.php

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,24 @@ class LivewireComponentColumn extends Column
1616
use LivewireComponentColumnConfiguration,
1717
LivewireComponentColumnHelpers;
1818

19+
/**
20+
* The Livewire Component assigned to this Column
21+
*/
1922
protected ?string $livewireComponent;
2023

24+
/**
25+
* Gets the contents for current row
26+
*
27+
* @param Model $row
28+
* @return null|string|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
29+
*/
2130
public function getContents(Model $row): null|string|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
2231
{
23-
if (! $this->hasLivewireComponent()) {
24-
throw new DataTableConfigurationException('You must define a Livewire Component for this column');
25-
}
26-
27-
if ($this->isLabel()) {
28-
throw new DataTableConfigurationException('You can not use a label column with a Livewire Component column');
29-
}
30-
31-
$attributes = [];
32-
$value = $this->getValue($row);
33-
34-
if ($this->hasAttributesCallback()) {
35-
$attributes = call_user_func($this->getAttributesCallback(), $value, $row, $this);
36-
37-
if (! is_array($attributes)) {
38-
throw new DataTableConfigurationException('The return type of callback must be an array');
39-
}
40-
}
41-
42-
$implodedAttributes = collect($attributes)->map(function ($value, $key) {
43-
return ':'.$key.'="$'.$key.'"';
44-
})->implode(' ');
45-
46-
return new HtmlString(Blade::render(
47-
'<livewire:dynamic-component :component="$component" '.$implodedAttributes.' :wire:key="'.$row->{$row->getKeyName()}.'" />',
48-
[
49-
'component' => $this->getLivewireComponent(),
50-
...$attributes,
51-
],
52-
));
32+
$this->runPreChecks();
33+
34+
$attributes = $this->retrieveAttributes($row);
35+
36+
return $this->getHtmlString($attributes, $this->getTable().'-'.$row->{$row->getKeyName()});
5337

5438
}
5539
}

src/Views/Columns/Traits/Configuration/LivewireComponentColumnConfiguration.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
trait LivewireComponentColumnConfiguration
88
{
9+
10+
/**
11+
* Defines which component to use
12+
*
13+
* @param string $livewireComponent
14+
* @return self
15+
*/
916
public function component(string $livewireComponent): self
1017
{
1118
$this->livewireComponent = (Str::startsWith($livewireComponent, 'livewire:')) ? substr($livewireComponent, 9) : $livewireComponent;

src/Views/Columns/Traits/Helpers/LivewireComponentColumnHelpers.php

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,123 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Views\Columns\Traits\Helpers;
44

5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Facades\Blade;
7+
use Illuminate\Support\HtmlString;
8+
use Illuminate\Support\Str;
9+
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
10+
511
trait LivewireComponentColumnHelpers
612
{
13+
714
/**
815
* Retrieves the defined Component View
9-
*/
16+
*
17+
* @return string|null
18+
*/
1019
public function getLivewireComponent(): ?string
1120
{
1221
return $this->livewireComponent ?? null;
1322
}
1423

1524
/**
16-
* Determines whether a Component View has been set
25+
* Determines whether a Livewire Component has been set
26+
*
27+
* @return boolean
1728
*/
1829
public function hasLivewireComponent(): bool
1930
{
2031
return isset($this->livewireComponent);
2132
}
33+
34+
/**
35+
* Retrieves attributes based on callback
36+
*
37+
* @param Model $row
38+
* @return array
39+
*/
40+
protected function retrieveAttributes(Model $row): array
41+
{
42+
$value = $this->getValue($row);
43+
44+
$attributes = ['value' => $value];
45+
46+
if ($this->hasAttributesCallback()) {
47+
$attributes = call_user_func($this->getAttributesCallback(), $value, $row, $this);
48+
49+
if (! is_array($attributes)) {
50+
throw new DataTableConfigurationException('The return type of callback must be an array');
51+
}
52+
}
53+
54+
return $attributes;
55+
}
56+
57+
/**
58+
* Runs pre-checks
59+
*
60+
* @return boolean
61+
*/
62+
protected function runPreChecks(): bool
63+
{
64+
if (! $this->hasLivewireComponent()) {
65+
throw new DataTableConfigurationException('You must define a Livewire Component for this column');
66+
67+
return false;
68+
}
69+
70+
if ($this->isLabel()) {
71+
throw new DataTableConfigurationException('You can not use a label column with a Livewire Component column');
72+
73+
return false;
74+
}
75+
76+
return true;
77+
}
78+
79+
80+
/**
81+
* Implodes defined attributes to be used
82+
*
83+
* @param array $attributes
84+
* @return string
85+
*/
86+
protected function implodeAttributes(array $attributes): string
87+
{
88+
return collect($attributes)->map(function ($value, $key) {
89+
return ':'.$key.'="$'.$key.'"';
90+
})->implode(' ');
91+
}
92+
93+
/**
94+
* getBlade Render
95+
*
96+
* @param array $attributes
97+
* @param string $key
98+
* @return string
99+
*/
100+
protected function getBlade(array $attributes, string $key): string
101+
{
102+
return Blade::render(
103+
'<livewire:dynamic-component :component="$component" :key="$key" '.$this->implodeAttributes($attributes).' />',
104+
[
105+
'component' => $this->getLivewireComponent(),
106+
'key' => $key.Str::random(),
107+
...$attributes,
108+
],
109+
);
110+
}
111+
112+
/**
113+
* Gets HTML String
114+
*
115+
* @param array $attributes
116+
* @param string $key
117+
* @return HtmlString
118+
*/
119+
protected function getHtmlString(array $attributes, string $key): HtmlString
120+
{
121+
return new HtmlString($this->getBlade($attributes, $key));
122+
123+
}
22124
}

0 commit comments

Comments
 (0)