Skip to content

Commit

Permalink
fix: max depth behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
saade committed Feb 15, 2024
1 parent a0611a9 commit 073b4b3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion resources/dist/filament-adjacency-list.js

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions resources/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default ({
}) => ({
statePath,
sortable: null,
maxDepth,

init() {
this.sortable = new Sortable(this.$el, {
Expand All @@ -21,21 +20,23 @@ export default ({
draggable: "[data-sortable-item]",
handle: "[data-sortable-handle]",
onMove: (evt) => {
if (this.maxDepth >= 0 && this.getDepth(evt.related) > this.maxDepth) {
return false; // Prevent sorting
if (maxDepth && maxDepth >= 0 && this.getDepth(evt.related) > maxDepth) {
return false; // Prevent dragging items to a depth greater than maxDepth
}
},
onSort: (evt) => {
onSort: () => {
this.$wire.dispatchFormEvent('builder::sort', this.statePath, this.sortable.toArray())
}
})
},

getDepth(el, depth = 0) {
let parentEl = el.parentElement.closest('[data-sortable-item]');
if (parentEl) {
return this.getDepth(parentEl, ++depth);
const parentElement = el.parentElement.closest('[data-sortable-item]');

if (parentElement) {
return this.getDepth(parentElement, ++depth);
}

return depth;
},
})
6 changes: 4 additions & 2 deletions resources/views/components/item.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class="pb-3 rounded-lg"
$hasChildren = count($item[$childrenKey] ?? []) > 0;
$hitDepthLimit = $maxDepth && substr_count($itemStatePath, $childrenKey) >= $maxDepth;
$mountArgs = [
'statePath' => $itemStatePath,
'cachedRecordKey' => $uuid,
Expand Down Expand Up @@ -60,7 +62,7 @@ class="px-2 text-gray-500 appearance-none"
</div>

<div class="items-center flex-shrink-0 hidden px-2 space-x-2 rtl:space-x-reverse group-hover:flex">
@if ($addable)
@if ($addable && !$hitDepthLimit)
{{ $addChildAction($mountArgs) }}
@endif
@if ($dedentable)
Expand All @@ -72,7 +74,7 @@ class="px-2 text-gray-500 appearance-none"
@if ($descendable)
{{ $moveDownAction($mountArgs) }}
@endif
@if ($indentable)
@if ($indentable && !$hitDepthLimit)
{{ $indentAction($mountArgs) }}
@endif
@if ($deletable)
Expand Down
4 changes: 2 additions & 2 deletions src/Forms/Components/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class Component extends Forms\Components\Field

protected string | Closure $childrenKey = 'children';

protected int $maxDepth = -1;
protected int | Closure | null $maxDepth = null;

protected bool | Closure $hasRulers = false;

Expand Down Expand Up @@ -110,7 +110,7 @@ public function maxDepth(int | Closure $maxDepth): static
return $this;
}

public function getMaxDepth(): int
public function getMaxDepth(): ?int
{
return $this->evaluate($this->maxDepth);
}
Expand Down

0 comments on commit 073b4b3

Please sign in to comment.