From d2b2b56e62af366cd5d14e9e8be40ae1055d4d3a Mon Sep 17 00:00:00 2001 From: Ash Monsh Date: Thu, 17 Aug 2023 00:45:55 +0300 Subject: [PATCH 1/2] adding tiptap editor by default and improve forms UI in general --- composer.json | 2 + src/Filament/Resources/FaqResource.php | 31 ++++--- src/Filament/Resources/LibraryResource.php | 94 +++++++++++----------- src/Filament/Resources/PageResource.php | 8 +- src/Filament/Resources/PostResource.php | 9 +-- src/Filament/Resources/TagResource.php | 36 +++++---- 6 files changed, 97 insertions(+), 83 deletions(-) diff --git a/composer.json b/composer.json index cacdd56..ba9f946 100644 --- a/composer.json +++ b/composer.json @@ -37,8 +37,10 @@ "filament/spatie-laravel-media-library-plugin": "^3.0@beta", "filament/spatie-laravel-tags-plugin": "^3.0@beta", "spatie/laravel-medialibrary": "^10.0.0", + "awcodes/filament-tiptap-editor": "^3.0", "spatie/laravel-sluggable": "^3.3", "spatie/laravel-tags": "^4.4", + "andrewdwallo/filament-selectify": "^2.0", "lara-zeus/core": "^3.0" }, "require-dev": { diff --git a/src/Filament/Resources/FaqResource.php b/src/Filament/Resources/FaqResource.php index e04056c..5ef46b6 100644 --- a/src/Filament/Resources/FaqResource.php +++ b/src/Filament/Resources/FaqResource.php @@ -3,6 +3,7 @@ namespace LaraZeus\Sky\Filament\Resources; use Filament\Forms\Components\RichEditor; +use Filament\Forms\Components\Section; use Filament\Forms\Components\SpatieTagsInput; use Filament\Forms\Components\Textarea; use Filament\Forms\Form; @@ -46,21 +47,25 @@ public static function form(Form $form): Form { return $form ->schema([ - Textarea::make('question') - ->label(__('Question')) - ->required() - ->maxLength(65535) - ->columnSpan(2), + Section::make(__('Library File')) + ->columns(2) + ->schema([ + Textarea::make('question') + ->label(__('Question')) + ->required() + ->maxLength(65535) + ->columnSpan(2), - RichEditor::make('answer') - ->label(__('Answer')) - ->required() - ->maxLength(65535) - ->columnSpan(2), + RichEditor::make('answer') + ->label(__('Answer')) + ->required() + ->maxLength(65535) + ->columnSpan(2), - SpatieTagsInput::make('category') - ->type('faq') - ->label(__('FAQ Categories')), + SpatieTagsInput::make('category') + ->type('faq') + ->label(__('FAQ Categories')), + ]), ]); } diff --git a/src/Filament/Resources/LibraryResource.php b/src/Filament/Resources/LibraryResource.php index ca26c79..5035dd6 100644 --- a/src/Filament/Resources/LibraryResource.php +++ b/src/Filament/Resources/LibraryResource.php @@ -2,7 +2,6 @@ namespace LaraZeus\Sky\Filament\Resources; -use Filament\Forms\Components\Radio; use Filament\Forms\Components\Section; use Filament\Forms\Components\Select; use Filament\Forms\Components\SpatieMediaLibraryFileUpload; @@ -24,6 +23,7 @@ use LaraZeus\Sky\Filament\Resources\LibraryResource\Pages; use LaraZeus\Sky\Models\Library; use LaraZeus\Sky\SkyPlugin; +use Wallo\FilamentSelectify\Components\ButtonGroup; class LibraryResource extends SkyResource { @@ -42,69 +42,71 @@ public static function form(Form $form): Form { return $form ->schema([ - TextInput::make('title') - ->label(__('Library Title')) - ->required() - ->maxLength(255) - ->live(onBlur: true) - ->afterStateUpdated(function (Set $set, $state, $context) { - if ($context === 'edit') { - return; - } - - $set('slug', Str::slug($state)); - }), - - TextInput::make('slug') - ->unique(ignorable: fn (?Library $record): ?Library => $record) - ->required() - ->maxLength(255) - ->label(__('Library Slug')), - - Textarea::make('description') - ->maxLength(255) - ->label(__('Library Description')) - ->columnSpan(2), - - SpatieTagsInput::make('category') - ->type('library') - ->label(__('Library Categories')), - - Select::make('type') - ->label(__('Library Type')) - ->visible(SkyPlugin::get()->getLibraryTypes() !== null) - ->options(SkyPlugin::get()->getLibraryTypes()), + Section::make(__('Library File')) + ->columns(2) + ->schema([ + TextInput::make('title') + ->label(__('Library Title')) + ->required() + ->maxLength(255) + ->live(onBlur: true) + ->afterStateUpdated(function (Set $set, $state, $context) { + if ($context === 'edit') { + return; + } + + $set('slug', Str::slug($state)); + }), + + TextInput::make('slug') + ->unique(ignorable: fn(?Library $record): ?Library => $record) + ->required() + ->maxLength(255) + ->label(__('Library Slug')), + + Textarea::make('description') + ->maxLength(255) + ->label(__('Library Description')) + ->columnSpan(2), + + SpatieTagsInput::make('category') + ->type('library') + ->label(__('Library Categories')), + + Select::make('type') + ->label(__('Library Type')) + ->visible(SkyPlugin::get()->getLibraryTypes() !== null) + ->options(SkyPlugin::get()->getLibraryTypes()), + ]), Section::make(__('Library File')) + ->collapsible() + ->compact() ->schema([ - Radio::make('upload_or_url') - ->label('') + ButtonGroup::make('upload_or_url') ->live() ->dehydrated(false) ->afterStateHydrated(function (Set $set, Get $get) { $setVal = ($get('file_path') === null) ? 'upload' : 'url'; $set('upload_or_url', $setVal); }) - ->default('upload') ->options([ 'upload' => __('upload'), 'url' => __('url'), ]) - ->inline(), - + ->default('upload'), SpatieMediaLibraryFileUpload::make('file_path_upload') ->collection('library') ->multiple() ->reorderable() - ->visible(fn (Get $get) => $get('upload_or_url') === 'upload') + ->visible(fn(Get $get) => $get('upload_or_url') === 'upload') ->label(''), TextInput::make('file_path') ->label(__('file url')) - ->visible(fn (Get $get) => $get('upload_or_url') === 'url') + ->visible(fn(Get $get) => $get('upload_or_url') === 'url') ->url(), - ]) - ->collapsible(), + ]), ]); } @@ -120,15 +122,15 @@ public static function table(Table $table): Table ->searchable() ->sortable() ->visible(SkyPlugin::get()->getLibraryTypes() !== null) - ->formatStateUsing(fn (string $state): string => str($state)->title()) + ->formatStateUsing(fn(string $state): string => str($state)->title()) ->color('') - ->color(fn (string $state) => match ($state) { + ->color(fn(string $state) => match ($state) { 'IMAGE' => 'primary', 'FILE' => 'success', 'VIDEO' => 'warning', default => '', }) - ->icon(fn (string $state) => match ($state) { + ->icon(fn(string $state) => match ($state) { 'IMAGE' => 'heroicon-o-photo', 'FILE' => 'heroicon-o-document', 'VIDEO' => 'heroicon-o-film', @@ -148,7 +150,7 @@ public static function table(Table $table): Table ->color('warning') ->icon('heroicon-o-arrow-top-right-on-square') ->label(__('Open')) - ->url(fn (Library $record): string => route('library.item', ['slug' => $record->slug])) + ->url(fn(Library $record): string => route('library.item', ['slug' => $record->slug])) ->openUrlInNewTab(), DeleteAction::make('delete') ->label(__('Delete')), diff --git a/src/Filament/Resources/PageResource.php b/src/Filament/Resources/PageResource.php index ef8cbfe..a59cff7 100644 --- a/src/Filament/Resources/PageResource.php +++ b/src/Filament/Resources/PageResource.php @@ -34,6 +34,7 @@ use LaraZeus\Sky\Filament\Resources\PageResource\Pages; use LaraZeus\Sky\Models\Post; use LaraZeus\Sky\SkyPlugin; +use Wallo\FilamentSelectify\Components\ButtonGroup; class PageResource extends SkyResource { @@ -124,20 +125,19 @@ public static function form(Form $form): Form ]), Tabs\Tab::make(__('Image'))->schema([ Placeholder::make(__('Featured Image')), - Radio::make('featured_image_type') - ->label('') + ButtonGroup::make('featured_image_type') ->live() + ->label('') ->dehydrated(false) ->afterStateHydrated(function (Set $set, Get $get) { $setVal = ($get('featured_image') === null) ? 'upload' : 'url'; $set('featured_image_type', $setVal); }) - ->default('upload') ->options([ 'upload' => __('upload'), 'url' => __('url'), ]) - ->inline(), + ->default('upload'), SpatieMediaLibraryFileUpload::make('featured_image_upload') ->collection('pages') ->visible(fn (Get $get) => $get('featured_image_type') === 'upload') diff --git a/src/Filament/Resources/PostResource.php b/src/Filament/Resources/PostResource.php index 2017da8..a4fd254 100644 --- a/src/Filament/Resources/PostResource.php +++ b/src/Filament/Resources/PostResource.php @@ -35,6 +35,7 @@ use LaraZeus\Sky\Filament\Resources\PostResource\Pages; use LaraZeus\Sky\Models\Post; use LaraZeus\Sky\SkyPlugin; +use Wallo\FilamentSelectify\Components\ButtonGroup; // @mixin Builder class PostResource extends SkyResource @@ -126,21 +127,19 @@ public static function form(Form $form): Form Tabs\Tab::make(__('Image'))->schema([ Placeholder::make(__('Featured Image')), - Radio::make('featured_image_type') - ->label('') + ButtonGroup::make('featured_image_type') ->live() + ->label('') ->dehydrated(false) ->afterStateHydrated(function (Set $set, Get $get) { $setVal = ($get('featured_image') === null) ? 'upload' : 'url'; $set('featured_image_type', $setVal); }) - ->default('upload') ->options([ 'upload' => __('upload'), 'url' => __('url'), ]) - ->inline(), - + ->default('upload'), SpatieMediaLibraryFileUpload::make('featured_image_upload') ->collection('posts') ->visible(fn (Get $get) => $get('featured_image_type') === 'upload') diff --git a/src/Filament/Resources/TagResource.php b/src/Filament/Resources/TagResource.php index f196ac1..13340c8 100644 --- a/src/Filament/Resources/TagResource.php +++ b/src/Filament/Resources/TagResource.php @@ -2,6 +2,7 @@ namespace LaraZeus\Sky\Filament\Resources; +use Filament\Forms\Components\Section; use Filament\Forms\Components\Select; use Filament\Forms\Components\TextInput; use Filament\Forms\Form; @@ -34,20 +35,25 @@ public static function form(Form $form): Form { return $form ->schema([ - TextInput::make('name') - ->required() - ->maxLength(255) - ->label(__('Tag.Name')) - ->live(onBlur: true) - ->afterStateUpdated(function (Set $set, $state) { - $set('slug', Str::slug($state)); - }), - TextInput::make('slug') - ->unique(ignorable: fn (?Model $record): ?Model => $record) - ->required() - ->maxLength(255), - Select::make('type') - ->options(SkyPlugin::get()->getTagTypes()), + Section::make() + ->columns(2) + ->schema([ + TextInput::make('name') + ->required() + ->maxLength(255) + ->label(__('Tag.Name')) + ->live(onBlur: true) + ->afterStateUpdated(function (Set $set, $state) { + $set('slug', Str::slug($state)); + }), + TextInput::make('slug') + ->unique(ignorable: fn(?Model $record): ?Model => $record) + ->required() + ->maxLength(255), + Select::make('type') + ->columnSpan(2) + ->options(SkyPlugin::get()->getTagTypes()), + ]), ]); } @@ -61,7 +67,7 @@ public static function table(Table $table): Table TextColumn::make('items_count') ->toggleable() ->getStateUsing( - fn (Tag $record): int => method_exists($record, $record->type) + fn(Tag $record): int => method_exists($record, $record->type) ? $record->{$record->type}()->count() : 0 ), From 4929c82295f0c3f73996e908daab957fe4fe0fdf Mon Sep 17 00:00:00 2001 From: atmonshi Date: Wed, 16 Aug 2023 21:46:25 +0000 Subject: [PATCH 2/2] Fix styling --- src/Filament/Resources/LibraryResource.php | 14 +++++++------- src/Filament/Resources/PageResource.php | 1 - src/Filament/Resources/PostResource.php | 1 - src/Filament/Resources/TagResource.php | 4 ++-- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/Filament/Resources/LibraryResource.php b/src/Filament/Resources/LibraryResource.php index 5035dd6..4dc0434 100644 --- a/src/Filament/Resources/LibraryResource.php +++ b/src/Filament/Resources/LibraryResource.php @@ -59,7 +59,7 @@ public static function form(Form $form): Form }), TextInput::make('slug') - ->unique(ignorable: fn(?Library $record): ?Library => $record) + ->unique(ignorable: fn (?Library $record): ?Library => $record) ->required() ->maxLength(255) ->label(__('Library Slug')), @@ -99,12 +99,12 @@ public static function form(Form $form): Form ->collection('library') ->multiple() ->reorderable() - ->visible(fn(Get $get) => $get('upload_or_url') === 'upload') + ->visible(fn (Get $get) => $get('upload_or_url') === 'upload') ->label(''), TextInput::make('file_path') ->label(__('file url')) - ->visible(fn(Get $get) => $get('upload_or_url') === 'url') + ->visible(fn (Get $get) => $get('upload_or_url') === 'url') ->url(), ]), ]); @@ -122,15 +122,15 @@ public static function table(Table $table): Table ->searchable() ->sortable() ->visible(SkyPlugin::get()->getLibraryTypes() !== null) - ->formatStateUsing(fn(string $state): string => str($state)->title()) + ->formatStateUsing(fn (string $state): string => str($state)->title()) ->color('') - ->color(fn(string $state) => match ($state) { + ->color(fn (string $state) => match ($state) { 'IMAGE' => 'primary', 'FILE' => 'success', 'VIDEO' => 'warning', default => '', }) - ->icon(fn(string $state) => match ($state) { + ->icon(fn (string $state) => match ($state) { 'IMAGE' => 'heroicon-o-photo', 'FILE' => 'heroicon-o-document', 'VIDEO' => 'heroicon-o-film', @@ -150,7 +150,7 @@ public static function table(Table $table): Table ->color('warning') ->icon('heroicon-o-arrow-top-right-on-square') ->label(__('Open')) - ->url(fn(Library $record): string => route('library.item', ['slug' => $record->slug])) + ->url(fn (Library $record): string => route('library.item', ['slug' => $record->slug])) ->openUrlInNewTab(), DeleteAction::make('delete') ->label(__('Delete')), diff --git a/src/Filament/Resources/PageResource.php b/src/Filament/Resources/PageResource.php index a59cff7..58ae85e 100644 --- a/src/Filament/Resources/PageResource.php +++ b/src/Filament/Resources/PageResource.php @@ -5,7 +5,6 @@ use Filament\Forms\Components\DateTimePicker; use Filament\Forms\Components\Hidden; use Filament\Forms\Components\Placeholder; -use Filament\Forms\Components\Radio; use Filament\Forms\Components\Select; use Filament\Forms\Components\SpatieMediaLibraryFileUpload; use Filament\Forms\Components\Tabs; diff --git a/src/Filament/Resources/PostResource.php b/src/Filament/Resources/PostResource.php index a4fd254..fb1d162 100644 --- a/src/Filament/Resources/PostResource.php +++ b/src/Filament/Resources/PostResource.php @@ -5,7 +5,6 @@ use Filament\Forms\Components\DateTimePicker; use Filament\Forms\Components\Hidden; use Filament\Forms\Components\Placeholder; -use Filament\Forms\Components\Radio; use Filament\Forms\Components\Select; use Filament\Forms\Components\SpatieMediaLibraryFileUpload; use Filament\Forms\Components\SpatieTagsInput; diff --git a/src/Filament/Resources/TagResource.php b/src/Filament/Resources/TagResource.php index 13340c8..094ec86 100644 --- a/src/Filament/Resources/TagResource.php +++ b/src/Filament/Resources/TagResource.php @@ -47,7 +47,7 @@ public static function form(Form $form): Form $set('slug', Str::slug($state)); }), TextInput::make('slug') - ->unique(ignorable: fn(?Model $record): ?Model => $record) + ->unique(ignorable: fn (?Model $record): ?Model => $record) ->required() ->maxLength(255), Select::make('type') @@ -67,7 +67,7 @@ public static function table(Table $table): Table TextColumn::make('items_count') ->toggleable() ->getStateUsing( - fn(Tag $record): int => method_exists($record, $record->type) + fn (Tag $record): int => method_exists($record, $record->type) ? $record->{$record->type}()->count() : 0 ),