Skip to content

Commit c42e4c6

Browse files
authored
Add Wrapper Options to FlexCol
1 parent 6f25fcc commit c42e4c6

File tree

4 files changed

+147
-23
lines changed

4 files changed

+147
-23
lines changed

src/Views/Columns/ArrayColumn.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Views\Columns;
44

5-
use Illuminate\Database\Eloquent\Model;
6-
use Illuminate\Support\HtmlString;
7-
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
85
use Rappasoft\LaravelLivewireTables\Views\Column;
96
use Rappasoft\LaravelLivewireTables\Views\Columns\Traits\Configuration\ArrayColumnConfiguration;
107
use Rappasoft\LaravelLivewireTables\Views\Columns\Traits\Helpers\ArrayColumnHelpers;
@@ -24,6 +21,10 @@ class ArrayColumn extends Column
2421

2522
protected mixed $outputFormat = null;
2623

24+
public ?string $outputWrapperStart = null;
25+
26+
public ?string $outputWrapperEnd = null;
27+
2728
public function __construct(string $title, ?string $from = null)
2829
{
2930
parent::__construct($title, $from);
@@ -32,23 +33,4 @@ public function __construct(string $title, ?string $from = null)
3233
}
3334
}
3435

35-
public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
36-
{
37-
$outputValues = [];
38-
$value = $this->getValue($row);
39-
40-
if (! $this->hasDataCallback()) {
41-
throw new DataTableConfigurationException('You must set a data() method on an ArrayColumn');
42-
}
43-
44-
if (! $this->hasOutputFormatCallback()) {
45-
throw new DataTableConfigurationException('You must set an outputFormat() method on an ArrayColumn');
46-
}
47-
48-
foreach (call_user_func($this->getDataCallback(), $value, $row) as $i => $v) {
49-
$outputValues[] = call_user_func($this->getOutputFormatCallback(), $i, $v);
50-
}
51-
52-
return new HtmlString((! empty($outputValues) ? implode($this->getSeparator(), $outputValues) : $this->getEmptyValue()));
53-
}
5436
}

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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

5+
use Illuminate\View\ComponentAttributeBag;
6+
57
trait ArrayColumnConfiguration
68
{
79
public function separator(string $value): self
@@ -34,4 +36,48 @@ public function emptyValue(string $emptyValue): self
3436

3537
return $this;
3638
}
39+
40+
public function wrapperStart(string $value): self
41+
{
42+
$this->outputWrapperStart = $value;
43+
44+
return $this;
45+
}
46+
47+
public function wrapperEnd(string $value): self
48+
{
49+
$this->outputWrapperEnd = $value;
50+
51+
return $this;
52+
}
53+
54+
/**
55+
* Setup Flex Col Behaviour
56+
*
57+
* @param array<mixed> $attribs
58+
* @return self
59+
*/
60+
public function flexCol(array $attribs = []): self
61+
{
62+
$bag = new ComponentAttributeBag(['class' => $this->isTailwind() ? 'flex flex-col' : 'd-flex d-flex-col']);
63+
64+
return $this->wrapperStart('<div '.$bag->merge($attribs).'>')
65+
->wrapperEnd('</div>')
66+
->separator('');
67+
}
68+
69+
/**
70+
* Setup Flex Row Behaviour
71+
*
72+
* @param array<mixed> $attribs
73+
* @return self
74+
*/
75+
public function flexRow(array $attribs = []): self
76+
{
77+
$bag = new ComponentAttributeBag(['class' => $this->isTailwind() ? 'flex flex-row' : 'd-flex d-flex-row']);
78+
79+
return $this->wrapperStart('<div '.$bag->merge($attribs).'>')
80+
->wrapperEnd('</div>')
81+
->separator('');
82+
}
3783
}

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

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

5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\HtmlString;
7+
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
8+
59
trait ArrayColumnHelpers
610
{
711
public function hasSeparator(): bool
@@ -38,4 +42,51 @@ public function getOutputFormatCallback(): ?callable
3842
{
3943
return $this->outputFormat;
4044
}
45+
46+
public function hasOutputWrapperStart(): bool
47+
{
48+
return $this->outputWrapperStart !== null && is_string($this->outputWrapperStart);
49+
}
50+
51+
public function getOutputWrapperStart(): string
52+
{
53+
return $this->outputWrapperStart;
54+
}
55+
56+
public function hasOutputWrapperEnd(): bool
57+
{
58+
return $this->outputWrapperEnd !== null && is_string($this->outputWrapperEnd);
59+
}
60+
61+
public function getOutputWrapperEnd(): string
62+
{
63+
return $this->outputWrapperEnd;
64+
}
65+
66+
public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
67+
{
68+
$outputValues = [];
69+
$value = $this->getValue($row);
70+
71+
if (! $this->hasDataCallback()) {
72+
throw new DataTableConfigurationException('You must set a data() method on an ArrayColumn');
73+
}
74+
75+
if (! $this->hasOutputFormatCallback()) {
76+
throw new DataTableConfigurationException('You must set an outputFormat() method on an ArrayColumn');
77+
}
78+
79+
foreach (call_user_func($this->getDataCallback(), $value, $row) as $i => $v) {
80+
$outputValues[] = call_user_func($this->getOutputFormatCallback(), $i, $v);
81+
}
82+
83+
$returnedValue = (! empty($outputValues) ? implode($this->getSeparator(), $outputValues) : $this->getEmptyValue());
84+
85+
if ($this->hasOutputWrapperStart() && $this->hasOutputWrapperEnd()) {
86+
$returnedValue = $this->getOutputWrapperStart().$returnedValue.$this->getOutputWrapperEnd();
87+
}
88+
89+
return new HtmlString($returnedValue);
90+
}
91+
4192
}

tests/Unit/Views/Columns/ArrayColumnTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use PHPUnit\Framework\Attributes\Group;
66
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
7-
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
7+
use Rappasoft\LaravelLivewireTables\Tests\Models\{Pet,Veterinary};
88
use Rappasoft\LaravelLivewireTables\Views\Columns\ArrayColumn;
99

1010
#[Group('Columns')]
@@ -80,4 +80,49 @@ public function test_can_get_empty_value(): void
8080
$this->assertSame('Unknown', self::$columnInstance->getEmptyValue());
8181

8282
}
83+
84+
public function test_can_use_flexcol(): void
85+
{
86+
self::$columnInstance
87+
->data(fn ($value, $row) => ($row->pets))
88+
->outputFormat(fn ($index, $value) => '<a href="'.$value->id.'">'.$value->name.'</a>')
89+
->flexCol();
90+
91+
$contents = self::$columnInstance->getContents(Veterinary::find(1));
92+
$this->assertSame('<div class="flex flex-col"><a href="1">Cartman</a><a href="2">Tux</a></div>', $contents->toHtml());
93+
}
94+
95+
public function test_can_use_flexcol_with_attributes(): void
96+
{
97+
self::$columnInstance
98+
->data(fn ($value, $row) => ($row->pets))
99+
->outputFormat(fn ($index, $value) => '<a href="'.$value->id.'">'.$value->name.'</a>')
100+
->flexCol(['class' => 'bg-red-500']);
101+
102+
$contents = self::$columnInstance->getContents(Veterinary::find(1));
103+
$this->assertSame('<div class="bg-red-500 flex flex-col"><a href="1">Cartman</a><a href="2">Tux</a></div>', $contents->toHtml());
104+
}
105+
106+
public function test_can_use_flexrow(): void
107+
{
108+
self::$columnInstance
109+
->data(fn ($value, $row) => ($row->pets))
110+
->outputFormat(fn ($index, $value) => '<a href="'.$value->id.'">'.$value->name.'</a>')
111+
->flexRow();
112+
113+
$contents = self::$columnInstance->getContents(Veterinary::find(1));
114+
$this->assertSame('<div class="flex flex-row"><a href="1">Cartman</a><a href="2">Tux</a></div>', $contents->toHtml());
115+
}
116+
117+
public function test_can_use_flexrow_with_attributes(): void
118+
{
119+
self::$columnInstance
120+
->data(fn ($value, $row) => ($row->pets))
121+
->outputFormat(fn ($index, $value) => '<a href="'.$value->id.'">'.$value->name.'</a>')
122+
->flexRow(['class' => 'bg-blue-500']);
123+
124+
$contents = self::$columnInstance->getContents(Veterinary::find(1));
125+
$this->assertSame('<div class="bg-blue-500 flex flex-row"><a href="1">Cartman</a><a href="2">Tux</a></div>', $contents->toHtml());
126+
}
127+
83128
}

0 commit comments

Comments
 (0)