From dfcd4a8763643b0fc1cb0d4e970ae879a8175e09 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 4 Jan 2024 22:11:28 +0100 Subject: [PATCH 1/3] Update table column affix helpers to support Htmlables Fixes https://github.com/filamentphp/filament/discussions/9729 and https://www.answeroverflow.com/m/1154022222118256700 by making so that the `->prefix()` and `->suffix()` helpers support Htmlables like HtmlStrings. --- .../src/Columns/Concerns/CanFormatState.php | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/tables/src/Columns/Concerns/CanFormatState.php b/packages/tables/src/Columns/Concerns/CanFormatState.php index 78a8893d12..d3080fcda7 100644 --- a/packages/tables/src/Columns/Concerns/CanFormatState.php +++ b/packages/tables/src/Columns/Concerns/CanFormatState.php @@ -6,6 +6,7 @@ use Closure; use Filament\Support\Contracts\HasLabel as LabelInterface; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Table; +use Illuminate\Contracts\Support\Htmlable; use Illuminate\Support\Carbon; use Illuminate\Support\HtmlString; use Illuminate\Support\Str; @@ -26,9 +27,9 @@ trait CanFormatState protected string | Closure | null $wordLimitEnd = null; - protected string | Closure | null $prefix = null; + protected string | Htmlable | Closure | null $prefix = null; - protected string | Closure | null $suffix = null; + protected string | Htmlable | Closure | null $suffix = null; protected string | Closure | null $timezone = null; @@ -179,14 +180,14 @@ trait CanFormatState return $this; } - public function prefix(string | Closure | null $prefix): static + public function prefix(string | Htmlable | Closure | null $prefix): static { $this->prefix = $prefix; return $this; } - public function suffix(string | Closure | null $suffix): static + public function suffix(string | Htmlable | Closure | null $suffix): static { $this->suffix = $suffix; @@ -227,10 +228,18 @@ trait CanFormatState if (filled($prefix = $this->getPrefix())) { $state = $prefix . $state; + + if ($prefix instanceof Htmlable) { + $this->isHtml = true; + } } if (filled($suffix = $this->getSuffix())) { $state = $state . $suffix; + + if ($suffix instanceof Htmlable) { + $this->isHtml = true; + } } if ($state instanceof HtmlString) { @@ -277,12 +286,12 @@ trait CanFormatState return $this->evaluate($this->isHtml) || $this->isMarkdown(); } - public function getPrefix(): ?string + public function getPrefix(): null | string | Htmlable { return $this->evaluate($this->prefix); } - public function getSuffix(): ?string + public function getSuffix(): null | string | Htmlable { return $this->evaluate($this->suffix); } From d2136d710ef99365c9e31dba389c1fc346d8433d Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Fri, 5 Jan 2024 14:05:26 +0000 Subject: [PATCH 2/3] Update CanFormatState.php --- packages/tables/src/Columns/Concerns/CanFormatState.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/tables/src/Columns/Concerns/CanFormatState.php b/packages/tables/src/Columns/Concerns/CanFormatState.php index d3080fcda7..2a058f68d1 100644 --- a/packages/tables/src/Columns/Concerns/CanFormatState.php +++ b/packages/tables/src/Columns/Concerns/CanFormatState.php @@ -286,12 +286,12 @@ trait CanFormatState return $this->evaluate($this->isHtml) || $this->isMarkdown(); } - public function getPrefix(): null | string | Htmlable + public function getPrefix(): string | Htmlable | null { return $this->evaluate($this->prefix); } - public function getSuffix(): null | string | Htmlable + public function getSuffix(): string | Htmlable | null { return $this->evaluate($this->suffix); } From 7dc0f97cb348700d609c44305b59c043e18115ad Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Mon, 8 Jan 2024 10:37:28 +0000 Subject: [PATCH 3/3] wip --- .../Components/Concerns/CanFormatState.php | 60 +++++++++++++------ .../src/Columns/Concerns/CanFormatState.php | 49 +++++++++------ 2 files changed, 72 insertions(+), 37 deletions(-) diff --git a/packages/infolists/src/Components/Concerns/CanFormatState.php b/packages/infolists/src/Components/Concerns/CanFormatState.php index b052d4f359..979f68ffe1 100644 --- a/packages/infolists/src/Components/Concerns/CanFormatState.php +++ b/packages/infolists/src/Components/Concerns/CanFormatState.php @@ -6,10 +6,9 @@ use Closure; use Filament\Infolists\Components\TextEntry; use Filament\Infolists\Infolist; use Filament\Support\Contracts\HasLabel as LabelInterface; +use Illuminate\Contracts\Support\Htmlable; use Illuminate\Support\Carbon; -use Illuminate\Support\HtmlString; use Illuminate\Support\Str; -use Illuminate\Support\Stringable; use function Filament\Support\format_money; use function Filament\Support\format_number; @@ -26,9 +25,9 @@ trait CanFormatState protected string | Closure | null $wordLimitEnd = null; - protected string | Closure | null $prefix = null; + protected string | Htmlable | Closure | null $prefix = null; - protected string | Closure | null $suffix = null; + protected string | Htmlable | Closure | null $suffix = null; protected string | Closure | null $timezone = null; @@ -179,14 +178,14 @@ trait CanFormatState return $this; } - public function prefix(string | Closure | null $prefix): static + public function prefix(string | Htmlable | Closure | null $prefix): static { $this->prefix = $prefix; return $this; } - public function suffix(string | Closure | null $suffix): static + public function suffix(string | Htmlable | Closure | null $suffix): static { $this->suffix = $suffix; @@ -209,10 +208,17 @@ trait CanFormatState public function formatState(mixed $state): mixed { + $isHtml = $this->isHtml(); + $state = $this->evaluate($this->formatStateUsing ?? $state, [ 'state' => $state, ]); + if ($state instanceof Htmlable) { + $isHtml = true; + $state = $state->toHtml(); + } + if ($state instanceof LabelInterface) { $state = $state->getLabel(); } @@ -225,23 +231,39 @@ trait CanFormatState $state = Str::words($state, $wordLimit, $this->getWordLimitEnd()); } - if (filled($prefix = $this->getPrefix())) { + if ($isHtml && $this->isMarkdown()) { + $state = Str::markdown($state); + } + + $prefix = $this->getPrefix(); + $suffix = $this->getSuffix(); + + if ( + (($prefix instanceof Htmlable) || ($suffix instanceof Htmlable)) && + (! $isHtml) + ) { + $isHtml = true; + $state = e($state); + } + + if (filled($prefix)) { + if ($prefix instanceof Htmlable) { + $prefix = $prefix->toHtml(); + } + $state = $prefix . $state; } - if (filled($suffix = $this->getSuffix())) { + if (filled($suffix)) { + if ($suffix instanceof Htmlable) { + $suffix = $suffix->toHtml(); + } + $state = $state . $suffix; } - if ($state instanceof HtmlString) { - return $state; - } - - if ($this->isHtml()) { - return str($state) - ->when($this->isMarkdown(), fn (Stringable $stringable) => $stringable->markdown()) - ->sanitizeHtml() - ->toHtmlString(); + if ($isHtml) { + return str($state)->sanitizeHtml()->toHtmlString(); } return $state; @@ -277,12 +299,12 @@ trait CanFormatState return $this->evaluate($this->isHtml) || $this->isMarkdown() || $this->isProse(); } - public function getPrefix(): ?string + public function getPrefix(): string | Htmlable | null { return $this->evaluate($this->prefix); } - public function getSuffix(): ?string + public function getSuffix(): string | Htmlable | null { return $this->evaluate($this->suffix); } diff --git a/packages/tables/src/Columns/Concerns/CanFormatState.php b/packages/tables/src/Columns/Concerns/CanFormatState.php index 2a058f68d1..4c00edb989 100644 --- a/packages/tables/src/Columns/Concerns/CanFormatState.php +++ b/packages/tables/src/Columns/Concerns/CanFormatState.php @@ -8,9 +8,7 @@ use Filament\Tables\Columns\TextColumn; use Filament\Tables\Table; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Support\Carbon; -use Illuminate\Support\HtmlString; use Illuminate\Support\Str; -use Illuminate\Support\Stringable; use function Filament\Support\format_money; use function Filament\Support\format_number; @@ -210,10 +208,17 @@ trait CanFormatState public function formatState(mixed $state): mixed { + $isHtml = $this->isHtml(); + $state = $this->evaluate($this->formatStateUsing ?? $state, [ 'state' => $state, ]); + if ($state instanceof Htmlable) { + $isHtml = true; + $state = $state->toHtml(); + } + if ($state instanceof LabelInterface) { $state = $state->getLabel(); } @@ -226,31 +231,39 @@ trait CanFormatState $state = Str::words($state, $wordLimit, $this->getWordLimitEnd()); } - if (filled($prefix = $this->getPrefix())) { - $state = $prefix . $state; + if ($isHtml && $this->isMarkdown()) { + $state = Str::markdown($state); + } + $prefix = $this->getPrefix(); + $suffix = $this->getSuffix(); + + if ( + (($prefix instanceof Htmlable) || ($suffix instanceof Htmlable)) && + (! $isHtml) + ) { + $isHtml = true; + $state = e($state); + } + + if (filled($prefix)) { if ($prefix instanceof Htmlable) { - $this->isHtml = true; + $prefix = $prefix->toHtml(); } + + $state = $prefix . $state; } - if (filled($suffix = $this->getSuffix())) { - $state = $state . $suffix; - + if (filled($suffix)) { if ($suffix instanceof Htmlable) { - $this->isHtml = true; + $suffix = $suffix->toHtml(); } + + $state = $state . $suffix; } - if ($state instanceof HtmlString) { - return $state; - } - - if ($this->isHtml()) { - return str($state) - ->when($this->isMarkdown(), fn (Stringable $stringable) => $stringable->markdown()) - ->sanitizeHtml() - ->toHtmlString(); + if ($isHtml) { + return str($state)->sanitizeHtml()->toHtmlString(); } return $state;