Skip to content

Commit 7e4e88e

Browse files
committed
Address PR feedback
1 parent 4b3e213 commit 7e4e88e

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,66 @@ If you need to append an item to the tree menu, use the `append` method. This me
275275
])
276276
```
277277

278+
If you need full control over the tree structure, you can use `getTreeUsing` to provide a custom tree array, or a closure that resolves to an array.
279+
280+
Using an array:
281+
282+
```php
283+
SelectTree::make('categories')
284+
->getTreeUsing([
285+
[
286+
'name' => 'Parent Category',
287+
'value' => '1',
288+
'children' => [
289+
[
290+
'name' => 'Child Category',
291+
'value' => '2',
292+
'children' => [],
293+
],
294+
],
295+
],
296+
[
297+
'name' => 'Another Category',
298+
'value' => '3',
299+
'children' => [],
300+
],
301+
])
302+
```
303+
304+
Using a closure for dynamic data:
305+
306+
```php
307+
SelectTree::make('categories')
308+
->getTreeUsing(function () {
309+
return Category::query()
310+
->get()
311+
->map(fn ($category) => [
312+
'name' => $category->name,
313+
'value' => $category->id,
314+
'children' => $category->children->map(fn ($child) => [
315+
'name' => $child->name,
316+
'value' => $child->id,
317+
'children' => [],
318+
])->toArray(),
319+
])
320+
->toArray();
321+
})
322+
```
323+
324+
The tree structure should follow this format:
325+
326+
```php
327+
[
328+
[
329+
'name' => 'Display Name',
330+
'value' => 'option_value',
331+
'disabled' => false, // optional
332+
'hidden' => false, // optional
333+
'children' => [], // optional
334+
],
335+
]
336+
```
337+
278338
## Filters
279339

280340
Use the tree in your table filters. Here's an example to show you how.

src/SelectTree.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class SelectTree extends Field implements HasAffixActions
9494

9595
protected bool $storeResults = false;
9696

97-
protected Closure|null $getTreeUsing = null;
97+
protected Closure|array|null $getTreeUsing = null;
9898

9999
protected LazyCollection|array|null $results = null;
100100

@@ -544,7 +544,7 @@ public function storeResults(bool $storeResults = true): static
544544
return $this;
545545
}
546546

547-
public function getTreeUsing(Closure $value): static
547+
public function getTreeUsing(Closure|array $value): static
548548
{
549549
$this->getTreeUsing = $value;
550550

0 commit comments

Comments
 (0)