CodeWithDennis / filament-select-tree

The multi-level select field lets you pick one or multiple options from a list that is neatly organized into different levels.
MIT License
225 stars 32 forks source link

[Bug]: $set not work #114

Closed mstfkhazaal closed 4 months ago

mstfkhazaal commented 5 months ago

What happened?

I want to change select after create new item

How to reproduce the bug

Example:

                                     SelectTree::make('zone')
                                            ->relationship(
                                                relationship: 'parent',
                                                titleAttribute: 'name',
                                                parentAttribute: 'parent_id',
                                            )
                                            ->live()
                                            ->createOptionForm([
                                                TextInput::make('name')
                                                    ->required()
                                                    ->maxLength(255),
                                            ])
                                            ->createOptionUsing(function ($data, $get, $set) {
                                                $zone = Zone::create($data);
                                                $set('zone', $zone->id);
                                            })
                                            ->afterStateUpdated(function (?string $state, ?string $old) {
                                                dd($state, $old);
                                            })
                                            ->expandSelected()
                                            ->searchable()
                                            ->enableBranchNode()
                                            ->withCount()
                                            ->required(),

Package Version

3.1.41

PHP Version

8.2.0

Laravel Version

11

Which operating systems does with happen with?

No response

Notes

No response

CodeWithDennis commented 5 months ago

Im not sure that you can do that in createOptionUsing can you confirm that works in a normal select field?

mstfkhazaal commented 5 months ago

yes is normally worked i do this way and it work but not professional way

                        Hidden::make('zone_hide'),
                        SelectTree::make('zone')
                            ->relationship(
                                relationship: 'parent',
                                titleAttribute: 'name',
                                parentAttribute: 'parent_id',
                            )
                            ->live()
                            ->createOptionForm([
                                TextInput::make('name')
                                    ->required()
                                    ->maxLength(255),
                            ])
                            ->createOptionUsing(function ($data, $set) {
                                $zone = Zone::create($data);
                                $set('item_hide', $zone->id);

                            })
                            ->afterStateUpdated(function (Set $set, ?string $state, ?string $old, $get) {
                                if ($get('zone_hide') != null) {
                                    $set('zone', $get('zone_hide'));
                                    $set('zone_hide', null);
                                }
                            })
                            ->expandSelected()
                            ->searchable()
                            ->enableBranchNode()
                            ->withCount()
                            ->required(),
CodeWithDennis commented 5 months ago

Im pretty sure it should be done in the afterStateUpdated and not in the createOptionUsing.

CodeWithDennis commented 5 months ago

@mstfkhazaal Did you figure it out?