Created
September 15, 2023 21:18
-
-
Save awcodes/9784de531aea8e3bdba772a55958a999 to your computer and use it in GitHub Desktop.
Meta component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Forms\Components; | |
use Filament\Forms; | |
use Illuminate\Contracts\Support\Htmlable; | |
use Illuminate\Support\HtmlString; | |
class Meta | |
{ | |
public static function make(): Forms\Components\Section | |
{ | |
return Forms\Components\Section::make('SEO') | |
->collapsible() | |
->relationship('seo') | |
->schema([ | |
Forms\Components\TextInput::make('title') | |
->label('Title') | |
->live(debounce: 500) | |
->helperText(function (?string $state): Htmlable { | |
$count = strlen($state); | |
$color = $count > 60 ? 'rgb(var(--warning-500))' : 'inherit'; | |
return new HtmlString('<span style="color: ' . $color . ';">' . $count . ' / 60 characters</span>'); | |
}), | |
Forms\Components\Textarea::make('description') | |
->label('Description') | |
->rows(3) | |
->live(debounce: 500) | |
->helperText(function (?string $state): Htmlable { | |
$count = strlen($state); | |
$color = $count > 160 ? 'rgb(var(--warning-500))' : 'inherit'; | |
return new HtmlString('<span style="color: ' . $color . ';">' . $count . ' / 160 characters</span>'); | |
}), | |
Forms\Components\CheckboxList::make('robots') | |
->options([ | |
'noindex' => 'No Index', | |
'nofollow' => 'No Follow', | |
]) | |
->gridDirection('row') | |
->columns(2) | |
->afterStateHydrated(function ($component, $state) { | |
if ($state && ! is_array($state)) { | |
$state = explode(',', $state); | |
} | |
$component->state($state ?? []); | |
}) | |
->dehydrateStateUsing(function (?array $state): ?string { | |
return filled($state) ? implode(',', $state) : null; | |
}), | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yet another way of firing updated status messages on inputs, but enforcing validation is something I used like this: