Skip to content

Commit

Permalink
Merge pull request #14 from CodeWithDennis/feature/expand-selected-op…
Browse files Browse the repository at this point in the history
…tion

[Feature] Expand selected options by default
  • Loading branch information
CodeWithDennis authored Sep 17, 2023
2 parents 3ef4561 + a13e894 commit 7819993
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ SelectTree::make('category_id')

// By default, all nodes are independent.
->independent(false)

// When 'independent' is set to false, the tree will open with the selected values by default.
->expandSelected(false)

// By default, the clearable icon is enabled, but you can hide it with:
->clearable(false)
Expand Down
2 changes: 1 addition & 1 deletion resources/dist/tree.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions resources/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export default function tree({
clearable = true,
isIndependentNodes = true,
alwaysOpen = false,
emptyText
emptyText,
expandSelected = true
}) {
return {
state,
Expand All @@ -40,7 +41,8 @@ export default function tree({
clearable,
isIndependentNodes,
alwaysOpen,
emptyText
emptyText,
expandSelected
});

this.tree.srcElement.addEventListener('input', (e) => {
Expand Down
3 changes: 2 additions & 1 deletion resources/views/select-tree.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
showTags: '{{ $getMultiple() }}',
alwaysOpen: '{{ $getAlwaysOpen() }}',
clearable: '{{ $getClearable() }}',
emptyText: '{{ $getEmptyLabel() }}'
emptyText: '{{ $getEmptyLabel() }}',
expandSelected: '{{ $getExpandSelected() }}'
})"
>
<div x-ref="tree"></div>
Expand Down
14 changes: 14 additions & 0 deletions src/SelectTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class SelectTree extends Field

protected bool $clearable = true;

protected bool $expandSelected = true;

protected bool $disabledBranchNode = false;

protected ?string $treeModel = null;
Expand All @@ -55,6 +57,13 @@ public function clearable(bool $clearable = true): static
return $this;
}

public function expandSelected(bool $expandSelected = true): static
{
$this->expandSelected = $expandSelected;

return $this;
}

public function emptyLabel(string $emptyLabel): static
{
$this->emptyLabel = $emptyLabel;
Expand Down Expand Up @@ -102,6 +111,11 @@ public function getTree(): Collection|array
return $this->evaluate($this->buildTree());
}

public function getExpandSelected(): bool
{
return $this->evaluate($this->expandSelected);
}

public function getIndependent(): bool
{
return $this->evaluate($this->independent);
Expand Down

0 comments on commit 7819993

Please sign in to comment.