From b2ac028674ad96026139d546c87a394e995e90d1 Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 28 Oct 2023 11:48:55 +0100 Subject: [PATCH] Move item form schema into separate method --- .../Concerns/HandlesNavigationBuilder.php | 86 ++++++++++--------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/src/Filament/Resources/NavigationResource/Pages/Concerns/HandlesNavigationBuilder.php b/src/Filament/Resources/NavigationResource/Pages/Concerns/HandlesNavigationBuilder.php index 5f2e2d2..9847d48 100644 --- a/src/Filament/Resources/NavigationResource/Pages/Concerns/HandlesNavigationBuilder.php +++ b/src/Filament/Resources/NavigationResource/Pages/Concerns/HandlesNavigationBuilder.php @@ -81,47 +81,7 @@ protected function getActions(): array $form->fill($this->mountedItemData); }) ->view('filament-navigation::hidden-action') - ->form([ - TextInput::make('label') - ->label(__('filament-navigation::filament-navigation.items-modal.label')) - ->required(), - Select::make('type') - ->label(__('filament-navigation::filament-navigation.items-modal.type')) - ->options(function () { - $types = FilamentNavigation::get()->getItemTypes(); - - return array_combine(array_keys($types), Arr::pluck($types, 'name')); - }) - ->afterStateUpdated(function ($state, Select $component): void { - if (! $state) { - return; - } - - // NOTE: This chunk of code is a workaround for Livewire not letting - // you entangle to non-existent array keys, which wire:model - // would normally let you do. - $component - ->getContainer() - ->getComponent(fn (Component $component) => $component instanceof Group) - ->getChildComponentContainer() - ->fill(); - }) - ->reactive(), - Group::make() - ->statePath('data') - ->whenTruthy('type') - ->schema(function (Get $get) { - $type = $get('type'); - - return FilamentNavigation::get()->getItemTypes()[$type]['fields'] ?? []; - }), - Group::make() - ->statePath('data') - ->visible(fn (Component $component) => $component->evaluate(FilamentNavigation::get()->getExtraFields()) !== []) - ->schema(function (Component $component) { - return FilamentNavigation::get()->getExtraFields(); - }), - ]) + ->form(static::getItemFormSchema()) ->modalWidth('md') ->action(function (array $data) { if ($this->mountedItem) { @@ -153,4 +113,48 @@ protected function getActions(): array ->label(__('filament-navigation::filament-navigation.items-modal.title')), ]; } + + protected static function getItemFormSchema(): array + { + return [ + TextInput::make('label') + ->label(__('filament-navigation::filament-navigation.items-modal.label')) + ->required(), + Select::make('type') + ->label(__('filament-navigation::filament-navigation.items-modal.type')) + ->options(function () { + $types = FilamentNavigation::get()->getItemTypes(); + + return array_combine(array_keys($types), Arr::pluck($types, 'name')); + }) + ->afterStateUpdated(function ($state, Select $component): void { + if (! $state) { + return; + } + + // NOTE: This chunk of code is a workaround for Livewire not letting you entangle to non-existent array + // keys, which wire:model would normally let you do. + $component + ->getContainer() + ->getComponent(fn (Component $component) => $component instanceof Group) + ->getChildComponentContainer() + ->fill(); + }) + ->reactive(), + Group::make() + ->statePath('data') + ->whenTruthy('type') + ->schema(function (Get $get) { + $type = $get('type'); + + return FilamentNavigation::get()->getItemTypes()[$type]['fields'] ?? []; + }), + Group::make() + ->statePath('data') + ->visible(fn (Component $component) => $component->evaluate(FilamentNavigation::get()->getExtraFields()) !== []) + ->schema(function (Component $component) { + return FilamentNavigation::get()->getExtraFields(); + }), + ]; + } }