Skip to content

Commit 89bb2f8

Browse files
committed
Fix session state set, add prepend and multiple options
1 parent 73c72a8 commit 89bb2f8

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

resources/dist/filament-select-tree.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ export default function selectTree({
2626

2727
/** @type Treeselect */
2828
tree: null,
29-
3029
init() {
30+
31+
if (typeof this.state === 'string') {
32+
this.state = Number(this.state);
33+
}
34+
3135
this.tree = new Treeselect({
3236
id: `tree-${name}-id`,
3337
ariaLabel: `tree-${name}-label`,

resources/views/select-tree.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
@else
2121
ax-load
2222
@endif
23-
ax-load-css="{{ FilamentAsset::getStyleHref('filament-select-tree-styles', package: 'codewithdennis/filament-select-tree') }}"
24-
ax-load-src="{{ FilamentAsset::getAlpineComponentSrc('filament-select-tree', package: 'codewithdennis/filament-select-tree') }}"
23+
ax-load-css="{{ FilamentAsset::getStyleHref('filament-select-tree-styles', package: 'codewithdennis/filament-select-tree') }}?v={{ time() }}"
24+
ax-load-src="{{ FilamentAsset::getAlpineComponentSrc('filament-select-tree', package: 'codewithdennis/filament-select-tree') }}?v={{ time() }}"
2525
x-data="selectTree({
2626
name: @js($getName()),
2727
state: $wire.{{ $applyStateBindingModifiers("\$entangle('{$getStatePath()}')") }},

src/SelectTree.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ class SelectTree extends Field implements HasAffixActions
8484

8585
protected Collection|array|null $results = null;
8686

87+
protected Closure|bool|null $multiple = null;
88+
89+
protected Closure|array|null $prepend = null;
90+
91+
8792
protected function setUp(): void
8893
{
8994
// Load the state from relationships using a callback function.
@@ -143,7 +148,7 @@ protected function setUp(): void
143148
]);
144149
}
145150

146-
private function buildTree(): Collection
151+
protected function buildTree(): Collection
147152
{
148153
// Start with two separate query builders
149154
$nullParentQuery = $this->getRelationship()->getRelated()->query()->where($this->getParentAttribute(), $this->getParentNullValue());
@@ -288,6 +293,19 @@ public function parentNullValue(int|string|null $parentNullValue = null): static
288293
return $this;
289294
}
290295

296+
public function multiple(Closure|bool $multiple = true): static
297+
{
298+
$this->multiple =$this->evaluate($multiple);
299+
return $this;
300+
}
301+
302+
public function prepend(Closure|array|null $prepend = null): static
303+
{
304+
$this->prepend = $this->evaluate($prepend);
305+
return $this;
306+
}
307+
308+
291309
public function getRelationship(): BelongsToMany|BelongsTo
292310
{
293311
return $this->getModelInstance()->{$this->evaluate($this->relationship)}();
@@ -394,7 +412,7 @@ public function storeResults(bool $storeResults = true): static
394412

395413
public function getTree(): Collection|array
396414
{
397-
return $this->evaluate($this->buildTree());
415+
return $this->evaluate($this->buildTree()->when($this->prepend, fn(Collection $tree) => $tree->prepend($this->prepend)));
398416
}
399417

400418
public function getResults(): Collection|array|null
@@ -434,7 +452,9 @@ public function getWithCount(): bool
434452

435453
public function getMultiple(): bool
436454
{
437-
return $this->evaluate($this->getRelationship() instanceof BelongsToMany);
455+
return $this->evaluate(
456+
is_null($this->multiple) ? $this->getRelationship() instanceof BelongsToMany : $this->multiple
457+
);
438458
}
439459

440460
public function getClearable(): bool

0 commit comments

Comments
 (0)