Dynamic load component - Reactive does not work on Select

    public ?string $key = null;

    public function form(Schema $schema): Schema
    {
        return $schema
            ->schema([
                ...$this->getBaseComponents(),
                Action::make('make_new_item')
                    ->label('Make new Item')
                    ->action(function (): void {
                        $state = $this->form->getRawState(); // or ->getState()
                        $this->key = $state['component'] ?? null;
                    }),
                Group::make()->components(fn() => $this->getExtraComponents($this->key)),
            ]);
    }

    protected function getBaseComponents(): array
    {
        return [
            Components\TextInput::make('name')
                ->label('Name')
                ->required(),

            Components\Select::make('component')
                ->searchable()
                ->live()
                ->options([
                    'email' => 'Email',
                    'last_name' => 'Last name',
                ])
                ->label('Component')
                ->required(),
        ];
    }

    protected function getExtraComponents(?string $key = null): array
    {
        $mapping = [
            'email' => [
                Components\Textarea::make('email')
                    ->label('Email'),
            ],
            'last_name' => [
                Components\Textarea::make('last_name')
                    ->label('Last name'),

                Components\Select::make('country')
                    ->afterstateUpdated(function ($state, $set) {
                        dd($state);
                    })
                    ->live()
                    ->searchable()
                    ->options([
                        'test' => 'Test',
                        'test2' => 'Test2',
                    ])
                    ->label('Country'),
            ],
        ];

        return $key ? ($mapping[$key] ?? []) : [];
    }
}
image.png
Solution
try this code please

public ?string $key = null;

public function form(Schema $schema): Schema
{
    return $schema
        ->schema([
            TextInput::make('name'),
            Select::make('component')
                ->searchable()
                ->options([
                    'email' => 'Email',
                    'last_name' => 'Last name',
                ]),
            Action::make('makeNewItem')
                ->action(function (Action $action) {

                    $this->key = $action
                        ->getSchemaContainer('component')
                        ->getComponent('component')
                        ->getState();

                    $action
                        ->getSchemaContainer()
                        ->getComponent('dynamicFields')
                        ->getChildSchema()
                        ->fill();
                }),
            Group::make(fn (): array => match ($this->key) {
                default => [],
                'email' => [
                    Textarea::make('email'),
                ],
                'last_name' => [
                    Textarea::make('last_name'),
                    Select::make('country')
                        ->live()
                        ->afterStateUpdated(function (?string $state) {
                            // Handle the state update logic here
                        })
                        ->searchable()
                        ->options([
                            'option1' => 'Option 1',
                            'option2' => 'Option 2',
                        ]),
                ],
            })
                ->key('dynamicFields'),
        ]);
}
Was this page helpful?