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
207 stars 28 forks source link

[Bug]: Tree component doesn't reflect changes on the state of another field gets updated. #52

Closed omarabdelaz1z closed 9 months ago

omarabdelaz1z commented 9 months ago

What happened?

Code Sample

Forms\Components\Section::make('Store Info')->schema([
    Forms\Components\Select::make('store_code')
        ->options(Store::query()->pluck('name', 'code'))
        ->preload()
        ->live()
        ->required()
        ->native(false)
        ->afterStateUpdated(function (SelectTree $component, Set $set, Get $get, ?Model $record) {
            $storeCode = $get('store_code');

            if ($storeCode === null) {
                return $set('categories', null);
            }

            if ($record === null || $record->store_code !== $storeCode) {
                return $set('categories', null);
            }

            return $set('categories', $record->categories()->pluck('category_id'));
        }),
    SelectTree::make('categories')
        ->relationship('categories', 'name', 'parent_id')
        ->withCount()
        ->independent(false)
        ->enableBranchNode()
        ->direction('top')
        ->disabled(function (Get $get, Set $set): bool {
            if ($get('store_code') === null) {
                $set('categories', null);

                return true;
            }

            return false;
        })
        ->disabledOptions(function (Get $get, ?Model $record): array {
            // omitted
        })
        ->saveRelationshipsUsing(function (Model $record, ?array $state) {
            // omitted
        }),
])->columns()

How to reproduce the bug

https://github.com/CodeWithDennis/filament-select-tree/assets/59033999/e4436057-694e-4046-800a-a58352af50a7

Package Version

3.1.14

PHP Version

8.1

Laravel Version

10.10

Which operating systems does with happen with?

Linux

Notes

No response

CodeWithDennis commented 9 months ago

I think there is something wrong with your logic. Disabling tree items depending on other fields works perfectly fine.

You might also want to check your afterStateUpdated method:

->afterStateUpdated(function (?string $state, ?string $old) {
   // ...
})