Skip to content

Commit

Permalink
Merge pull request #20 from CodeWithDennis/feature/add-direction-option
Browse files Browse the repository at this point in the history
Added `direction` option
  • Loading branch information
CodeWithDennis authored Sep 22, 2023
2 parents 30e44aa + 3094f5e commit 210c3e2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ SelectTree::make('category_id')
// Expand the tree with selected values
->expandSelected(false)

// All groups will be opened to this level
// All groups will be opened to this level
->defaultOpenLevel(2)

// Specify the list's force direction. Options include: auto (default), top, and bottom.
->directon('top')

// Display individual leaf nodes instead of the main group when all leaf nodes are selected
->grouped(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 @@ -18,7 +18,8 @@ export default function tree(
emptyText,
expandSelected = true,
grouped = true,
openLevel = 0
openLevel = 0,
direction = 'auto'
}) {
return {
state,
Expand All @@ -43,7 +44,8 @@ export default function tree(
emptyText,
expandSelected,
grouped,
openLevel
openLevel,
direction
});

this.tree.srcElement.addEventListener('input', (e) => {
Expand Down
1 change: 1 addition & 0 deletions resources/views/select-tree.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
expandSelected: '{{ $getExpandSelected() }}',
grouped: '{{ $getGrouped() }}',
openLevel: '{{ $getDefaultOpenLevel() }}',
direction: '{{ $getDirection() }}',
})"
>
<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 @@ -47,6 +47,8 @@ class SelectTree extends Field

protected Closure|int $defaultOpenLevel;

protected string $direction = 'auto';

protected function setUp(): void
{
// Load the state from relationships using a callback function.
Expand Down Expand Up @@ -127,6 +129,13 @@ public function withCount(bool $withCount = true): static
return $this;
}

public function direction(string $direction): static
{
$this->direction = $direction;

return $this;
}

public function getRelationship(): BelongsToMany|BelongsTo
{
return $this->getModelInstance()->{$this->evaluate($this->relationship)}();
Expand Down Expand Up @@ -252,4 +261,9 @@ public function getEmptyLabel(): string
{
return $this->emptyLabel ? $this->evaluate($this->emptyLabel) : __('No results found');
}

public function getDirection(): string
{
return $this->evaluate($this->direction);
}
}

0 comments on commit 210c3e2

Please sign in to comment.