saade / filament-adjacency-list

A Filament package to manage adjacency lists (aka trees).
https://filamentphp.com/plugins/saade-adjacency-list
MIT License
76 stars 13 forks source link

Adding AdjacencyListWidget #32

Closed cheesegrits closed 2 months ago

cheesegrits commented 4 months ago

Adding a widget, plus a few bug fixes.

saade commented 2 months ago

Hey! Thanks for the PR and sorry for the delay on reviewing it.

I've made some changes so the code became more cleaner and customizable, please, lmk wyt.

1. A new customization aproach.

Instead of defining each method again and hard coding the component on the form, users can now use the adjacencyList method to customize the widget. The usage follows the same way that Filament uses to configure its form, table and infolist.

class MyWidget extends AdjacencyListWidget {

    protected function adjacencyList(AdjacencyList $adjacencyList): AdjacencyList {
        return $adjacencyList
            ->label('Foo')
            ->editable()
            ->labelKey('nombre')
            ->childrenKey('hijos');
    }

}

2. Item actions and url

Instead of using the isClickable aproach, users can now customize which action is triggered when clicking on an item, or instead define a url to be opened. This follows the same aproach as Filament's table ->recordAction and ->recordUrl methods.

class MyWidget extends AdjacencyListWidget {

    protected function adjacencyList(AdjacencyList $adjacencyList): AdjacencyList {
        return $adjacencyList
            ->itemAction('edit') // or view, delete, moveUp, indent etc ...
            ->itemUrl(
               fn (array $item) => YourResource::getUrl('view', ['record' => $item['id']]) // for example
           )
    }

}

3. Miscellaneous

Let me know what you think!