Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/DataTransferObjects/Filters/FilterPillData.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public function __construct(
protected string $filterKey,
protected string $filterPillTitle,
protected string|array|null $filterPillValue,
protected string $separator,
public bool $isAnExternalLivewireFilter,
public bool $hasCustomPillBlade,
protected ?string $customPillBlade,
protected array $filterPillsItemAttributes,
protected bool $renderPillsAsHtml,
protected bool $watchForEvents,
protected array $customResetButtonAttributes,
protected bool $renderPillsTitleAsHtml) {}
protected string $separator = ',',
public bool $isAnExternalLivewireFilter = false,
public bool $hasCustomPillBlade = false,
protected ?string $customPillBlade = null,
protected array $filterPillsItemAttributes = [],
protected bool $renderPillsAsHtml = false,
protected bool $watchForEvents = false,
protected array $customResetButtonAttributes = [],
protected bool $renderPillsTitleAsHtml = false) {}

public static function make(string $filterKey, string $filterPillTitle, string|array|null $filterPillValue, string $separator = ', ', bool $isAnExternalLivewireFilter = false, bool $hasCustomPillBlade = false, ?string $customPillBlade = null, array $filterPillsItemAttributes = [], bool $renderPillsAsHtml = false, bool $watchForEvents = false, array $customResetButtonAttributes = [], bool $renderPillsTitleAsHtml = false): FilterPillData
{
Expand All @@ -43,7 +43,7 @@ public function getPillValue(): array|string|null

public function getHasCustomPillBlade(): bool
{
return $this->hasCustomPillBlade ?? false;
return $this->hasCustomPillBlade;
}

public function getCustomPillBlade(): ?string
Expand All @@ -53,32 +53,32 @@ public function getCustomPillBlade(): ?string

public function getCustomResetButtonAttributes(): array
{
return $this->customResetButtonAttributes ?? [];
return $this->customResetButtonAttributes;
}

public function getIsAnExternalLivewireFilter(): int
{
return intval($this->isAnExternalLivewireFilter ?? 0);
return intval($this->isAnExternalLivewireFilter);
}

public function getSeparator(): string
{
return $this->separator ?? ', ';
return $this->separator;
}

public function shouldUsePillsAsHtml(): int
{
return intval($this->renderPillsAsHtml ?? 0);
return intval($this->renderPillsAsHtml);
}

public function shouldUsePillsTitleAsHtml(): int
{
return intval($this->renderPillsTitleAsHtml ?? 0);
return intval($this->renderPillsTitleAsHtml);
}

public function shouldWatchForEvents(): int
{
return intval($this->watchForEvents ?? 0);
return intval($this->watchForEvents);
}

public function isPillValueAnArray(): bool
Expand Down
2 changes: 1 addition & 1 deletion src/DataTransferObjects/Filters/StandardFilterPillData.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function getPillValue(): string

public function shouldUsePillsAsHtml(): bool
{
return $this->renderPillsAsHtml ?? false;
return $this->renderPillsAsHtml;
}

public function toArray(): array
Expand Down
5 changes: 2 additions & 3 deletions src/Traits/Configuration/CollapsingColumnConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public function setCollapsingColumnsDisabled(): self

public function unsetCollapsedStatuses(): void
{
unset($this->shouldAlwaysCollapse);
unset($this->shouldMobileCollapse);
unset($this->shouldTabletCollapse);
$this->shouldAlwaysCollapse = $this->shouldMobileCollapse = $this->shouldTabletCollapse = false;

}
}
12 changes: 11 additions & 1 deletion src/Views/Columns/ArrayColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class ArrayColumn extends Column

protected mixed $outputFormat = null;

public ?string $outputWrapperStart = null;

public ?string $outputWrapperEnd = null;

public function __construct(string $title, ?string $from = null)
{
parent::__construct($title, $from);
Expand All @@ -49,6 +53,12 @@ public function getContents(Model $row): null|string|\BackedEnum|HtmlString|Data
$outputValues[] = call_user_func($this->getOutputFormatCallback(), $i, $v);
}

return new HtmlString((! empty($outputValues) ? implode($this->getSeparator(), $outputValues) : $this->getEmptyValue()));
$returnedValue = (! empty($outputValues) ? implode($this->getSeparator(), $outputValues) : $this->getEmptyValue());

if ($this->hasOutputWrapperStart() && $this->hasOutputWrapperEnd()) {
$returnedValue = $this->getOutputWrapperStart().$returnedValue.$this->getOutputWrapperEnd();
}

return new HtmlString($returnedValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

use Illuminate\View\ComponentAttributeBag;

trait ArrayColumnConfiguration
{
public function separator(string $value): self
Expand Down Expand Up @@ -34,4 +36,36 @@ public function emptyValue(string $emptyValue): self

return $this;
}

public function wrapperStart(string $value): self
{
$this->outputWrapperStart = $value;

return $this;
}

public function wrapperEnd(string $value): self
{
$this->outputWrapperEnd = $value;

return $this;
}

public function flexCol(array $attribs = []): self
{
$bag = new ComponentAttributeBag(['class' => $this->isTailwind() ? 'flex flex-col' : 'd-flex d-flex-col']);

return $this->wrapperStart('<div '.$bag->merge($attribs).'>')
->wrapperEnd('</div>')
->separator('');
}

public function flexRow(array $attribs = []): self
{
$bag = new ComponentAttributeBag(['class' => $this->isTailwind() ? 'flex flex-row' : 'd-flex d-flex-row']);

return $this->wrapperStart('<div '.$bag->merge($attribs).'>')
->wrapperEnd('</div>')
->separator('');
}
}
20 changes: 20 additions & 0 deletions src/Views/Columns/Traits/Helpers/ArrayColumnHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,24 @@ public function getOutputFormatCallback(): ?callable
{
return $this->outputFormat;
}

public function hasOutputWrapperStart(): bool
{
return $this->outputWrapperStart !== null && is_string($this->outputWrapperStart);
}

public function getOutputWrapperStart(): string
{
return $this->outputWrapperStart;
}

public function hasOutputWrapperEnd(): bool
{
return $this->outputWrapperEnd !== null && is_string($this->outputWrapperEnd);
}

public function getOutputWrapperEnd(): string
{
return $this->outputWrapperEnd;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function hasLivewireComponent(): bool

/**
* Retrieves attributes based on callback
*
* @return array<mixed>
*/
protected function retrieveAttributes(Model $row): array
{
Expand Down Expand Up @@ -67,6 +69,8 @@ protected function runPreChecks(): bool

/**
* Implodes defined attributes to be used
*
* @param array<mixed> $attributes
*/
protected function implodeAttributes(array $attributes): string
{
Expand All @@ -77,8 +81,10 @@ protected function implodeAttributes(array $attributes): string

/**
* getBlade Render
*
* @param array<mixed> $attributes
*/
protected function getBlade(array $attributes, string $key)
protected function getBlade(array $attributes, string $key): string
{
return Blade::render(
'<livewire:dynamic-component :component="$component" :key="$key" '.$this->implodeAttributes($attributes).' />',
Expand All @@ -91,7 +97,9 @@ protected function getBlade(array $attributes, string $key)
}

/**
* Gets HTML STring
* Gets HTML String
*
* @param array<mixed> $attributes
*/
protected function getHtmlString(array $attributes, string $key): HtmlString
{
Expand Down
46 changes: 45 additions & 1 deletion tests/Unit/Views/Columns/ArrayColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PHPUnit\Framework\Attributes\Group;
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
use Rappasoft\LaravelLivewireTables\Tests\Models\{Pet,Veterinary};
use Rappasoft\LaravelLivewireTables\Views\Columns\ArrayColumn;

#[Group('Columns')]
Expand Down Expand Up @@ -80,4 +80,48 @@ public function test_can_get_empty_value(): void
$this->assertSame('Unknown', self::$columnInstance->getEmptyValue());

}

public function test_can_use_flexcol(): void
{
self::$columnInstance
->data(fn ($value, $row) => ($row->pets))
->outputFormat(fn ($index, $value) => '<a href="'.$value->id.'">'.$value->name.'</a>')
->flexCol();

$contents = self::$columnInstance->getContents(Veterinary::find(1));
$this->assertSame('<div class="flex flex-col"><a href="1">Cartman</a><a href="2">Tux</a></div>', $contents->toHtml());
}

public function test_can_use_flexcol_with_attributes(): void
{
self::$columnInstance
->data(fn ($value, $row) => ($row->pets))
->outputFormat(fn ($index, $value) => '<a href="'.$value->id.'">'.$value->name.'</a>')
->flexCol(['class' => 'bg-red-500']);

$contents = self::$columnInstance->getContents(Veterinary::find(1));
$this->assertSame('<div class="bg-red-500 flex flex-col"><a href="1">Cartman</a><a href="2">Tux</a></div>', $contents->toHtml());
}

public function test_can_use_flexrow(): void
{
self::$columnInstance
->data(fn ($value, $row) => ($row->pets))
->outputFormat(fn ($index, $value) => '<a href="'.$value->id.'">'.$value->name.'</a>')
->flexRow();

$contents = self::$columnInstance->getContents(Veterinary::find(1));
$this->assertSame('<div class="flex flex-row"><a href="1">Cartman</a><a href="2">Tux</a></div>', $contents->toHtml());
}

public function test_can_use_flexrow_with_attributes(): void
{
self::$columnInstance
->data(fn ($value, $row) => ($row->pets))
->outputFormat(fn ($index, $value) => '<a href="'.$value->id.'">'.$value->name.'</a>')
->flexRow(['class' => 'bg-blue-500']);

$contents = self::$columnInstance->getContents(Veterinary::find(1));
$this->assertSame('<div class="bg-blue-500 flex flex-row"><a href="1">Cartman</a><a href="2">Tux</a></div>', $contents->toHtml());
}
}