Skip to content

Commit 0d12fc7

Browse files
committed
Enable manually controlling tree node collapsed state.
1 parent 7975274 commit 0d12fc7

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,15 @@ public function getTreeRecordIcon(?\Illuminate\Database\Eloquent\Model $record =
317317
```
318318
![tree-icon](https://github.com/solutionforest/filament-tree/assets/68525320/6a1ef719-9029-4e91-a20a-515a514c4326)
319319

320+
#### Node collapsed state
321+
You can customize a collapsed state of the node. If you would like to show your tree initially collapsed you can use:
322+
323+
```php
324+
public function getNodeCollapsedState(?\Illuminate\Database\Eloquent\Model $record = null): bool
325+
{
326+
return false;
327+
}
328+
```
320329

321330
### Pages
322331
This plugin enables you to create tree pages in the admin panel. To create a tree page for a model, use the `make:filament-tree-page` command. For example, to create a tree page for the ProductCategory model, you can run:

resources/views/components/tree/item.blade.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
$parentKey = $tree->getParentKey($record);
1515
1616
$children = $record->children;
17+
$collapsed = $this->getNodeCollapsedState($record);
1718
1819
$actions = $tree->getActions();
1920
@endphp
@@ -48,10 +49,10 @@
4849
'dd-item-btns',
4950
'hidden' => ! count($children),
5051
])>
51-
<button data-action="expand" class="hidden">
52+
<button data-action="expand" @class(['hidden' => ! $collapsed])>
5253
<x-heroicon-o-chevron-down class="text-gray-400 w-4 h-4"/>
5354
</button>
54-
<button data-action="collapse">
55+
<button data-action="collapse" @class(['hidden' => $collapsed])>
5556
<x-heroicon-o-chevron-up class="text-gray-400 w-4 h-4"/>
5657
</button>
5758
</div>
@@ -64,7 +65,7 @@
6465
@endif
6566
</div>
6667
@if (count($children))
67-
<x-filament-tree::tree.list :records="$children" :containerKey="$containerKey" :tree="$tree" />
68+
<x-filament-tree::tree.list :records="$children" :containerKey="$containerKey" :tree="$tree" :collapsed="$collapsed"/>
6869
@endif
6970
<div class="rounded-lg border border-gray-300 mb-2 w-full px-4 py-4 animate-pulse hidden"
7071
wire:loading.class.remove.delay="hidden"

resources/views/components/tree/list.blade.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
@props(['records', 'containerKey', 'tree'])
2-
<ol class="filament-tree-list dd-list">
1+
@props([
2+
'records',
3+
'containerKey',
4+
'tree',
5+
'collapsed' => null,
6+
])
7+
<ol @class([
8+
'filament-tree-list',
9+
'dd-list',
10+
'hidden' => $collapsed,
11+
])>
312
@foreach ($records ?? [] as $record)
413
@php
514
$title = $this->getTreeRecordTitle($record);

src/Concern/InteractWithTree.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public function getParentKey(?Model $record):?string
8181
return $this->getCachedTree()->getParentKey($record);
8282
}
8383

84+
public function getNodeCollapsedState(?Model $record = null): bool
85+
{
86+
return false;
87+
}
88+
8489
/**
8590
* Update the tree list.
8691
*/

0 commit comments

Comments
 (0)