Skip to content

Commit

Permalink
Move item form schema into separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
howdu committed Oct 28, 2023
1 parent 99068d0 commit b2ac028
Showing 1 changed file with 45 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}),
];
}
}

0 comments on commit b2ac028

Please sign in to comment.