diff --git a/src/DataTransferObjects/Filters/FilterPillData.php b/src/DataTransferObjects/Filters/FilterPillData.php index 8ecc241a7..53a5d1c98 100644 --- a/src/DataTransferObjects/Filters/FilterPillData.php +++ b/src/DataTransferObjects/Filters/FilterPillData.php @@ -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 { @@ -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 @@ -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 diff --git a/src/DataTransferObjects/Filters/StandardFilterPillData.php b/src/DataTransferObjects/Filters/StandardFilterPillData.php index 60a8ff5f9..978837ac1 100644 --- a/src/DataTransferObjects/Filters/StandardFilterPillData.php +++ b/src/DataTransferObjects/Filters/StandardFilterPillData.php @@ -28,7 +28,7 @@ public function getPillValue(): string public function shouldUsePillsAsHtml(): bool { - return $this->renderPillsAsHtml ?? false; + return $this->renderPillsAsHtml; } public function toArray(): array diff --git a/src/Traits/Configuration/CollapsingColumnConfiguration.php b/src/Traits/Configuration/CollapsingColumnConfiguration.php index dada8b991..c1d2e51f3 100644 --- a/src/Traits/Configuration/CollapsingColumnConfiguration.php +++ b/src/Traits/Configuration/CollapsingColumnConfiguration.php @@ -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; + } } diff --git a/src/Views/Columns/ArrayColumn.php b/src/Views/Columns/ArrayColumn.php index 26d39dbfd..c1470aaab 100644 --- a/src/Views/Columns/ArrayColumn.php +++ b/src/Views/Columns/ArrayColumn.php @@ -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); @@ -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); } } diff --git a/src/Views/Columns/Traits/Configuration/ArrayColumnConfiguration.php b/src/Views/Columns/Traits/Configuration/ArrayColumnConfiguration.php index 3cab2b52d..19891bbf2 100644 --- a/src/Views/Columns/Traits/Configuration/ArrayColumnConfiguration.php +++ b/src/Views/Columns/Traits/Configuration/ArrayColumnConfiguration.php @@ -2,6 +2,8 @@ namespace Rappasoft\LaravelLivewireTables\Views\Columns\Traits\Configuration; +use Illuminate\View\ComponentAttributeBag; + trait ArrayColumnConfiguration { public function separator(string $value): self @@ -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('
merge($attribs).'>') + ->wrapperEnd('
') + ->separator(''); + } + + public function flexRow(array $attribs = []): self + { + $bag = new ComponentAttributeBag(['class' => $this->isTailwind() ? 'flex flex-row' : 'd-flex d-flex-row']); + + return $this->wrapperStart('
merge($attribs).'>') + ->wrapperEnd('
') + ->separator(''); + } } diff --git a/src/Views/Columns/Traits/Helpers/ArrayColumnHelpers.php b/src/Views/Columns/Traits/Helpers/ArrayColumnHelpers.php index 97b598a93..99939f5c5 100644 --- a/src/Views/Columns/Traits/Helpers/ArrayColumnHelpers.php +++ b/src/Views/Columns/Traits/Helpers/ArrayColumnHelpers.php @@ -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; + } } diff --git a/src/Views/Columns/Traits/Helpers/LivewireComponentColumnHelpers.php b/src/Views/Columns/Traits/Helpers/LivewireComponentColumnHelpers.php index 55bedf325..296b100b0 100644 --- a/src/Views/Columns/Traits/Helpers/LivewireComponentColumnHelpers.php +++ b/src/Views/Columns/Traits/Helpers/LivewireComponentColumnHelpers.php @@ -27,6 +27,8 @@ public function hasLivewireComponent(): bool /** * Retrieves attributes based on callback + * + * @return array */ protected function retrieveAttributes(Model $row): array { @@ -67,6 +69,8 @@ protected function runPreChecks(): bool /** * Implodes defined attributes to be used + * + * @param array $attributes */ protected function implodeAttributes(array $attributes): string { @@ -77,8 +81,10 @@ protected function implodeAttributes(array $attributes): string /** * getBlade Render + * + * @param array $attributes */ - protected function getBlade(array $attributes, string $key) + protected function getBlade(array $attributes, string $key): string { return Blade::render( 'implodeAttributes($attributes).' />', @@ -91,7 +97,9 @@ protected function getBlade(array $attributes, string $key) } /** - * Gets HTML STring + * Gets HTML String + * + * @param array $attributes */ protected function getHtmlString(array $attributes, string $key): HtmlString { diff --git a/tests/Unit/Views/Columns/ArrayColumnTest.php b/tests/Unit/Views/Columns/ArrayColumnTest.php index 6c6a55472..c31e44507 100644 --- a/tests/Unit/Views/Columns/ArrayColumnTest.php +++ b/tests/Unit/Views/Columns/ArrayColumnTest.php @@ -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')] @@ -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) => ''.$value->name.'') + ->flexCol(); + + $contents = self::$columnInstance->getContents(Veterinary::find(1)); + $this->assertSame('', $contents->toHtml()); + } + + public function test_can_use_flexcol_with_attributes(): void + { + self::$columnInstance + ->data(fn ($value, $row) => ($row->pets)) + ->outputFormat(fn ($index, $value) => ''.$value->name.'') + ->flexCol(['class' => 'bg-red-500']); + + $contents = self::$columnInstance->getContents(Veterinary::find(1)); + $this->assertSame('', $contents->toHtml()); + } + + public function test_can_use_flexrow(): void + { + self::$columnInstance + ->data(fn ($value, $row) => ($row->pets)) + ->outputFormat(fn ($index, $value) => ''.$value->name.'') + ->flexRow(); + + $contents = self::$columnInstance->getContents(Veterinary::find(1)); + $this->assertSame('', $contents->toHtml()); + } + + public function test_can_use_flexrow_with_attributes(): void + { + self::$columnInstance + ->data(fn ($value, $row) => ($row->pets)) + ->outputFormat(fn ($index, $value) => ''.$value->name.'') + ->flexRow(['class' => 'bg-blue-500']); + + $contents = self::$columnInstance->getContents(Veterinary::find(1)); + $this->assertSame('', $contents->toHtml()); + } }