Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/dist/filament-select-tree.js

Large diffs are not rendered by default.

37 changes: 29 additions & 8 deletions resources/js/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Treeselect from 'treeselectjs'

// Store treeselect instances globally
window.treeselectInstances = window.treeselectInstances || {};

export default function selectTree({
state,
name,
Expand All @@ -23,16 +26,19 @@ export default function selectTree({
}) {
return {
state,

/** @type Treeselect */
tree: null,
initialized: false,
instanceId: null,

init() {
this.tree = new Treeselect({
id: `tree-${name}-id`,
ariaLabel: `tree-${name}-label`,
// Generate a unique instance ID
this.instanceId = `${name}-${Math.random().toString(36).substring(2, 11)}`;

// Initialize treeselect
const tree = new Treeselect({
id: `tree-${this.instanceId}-id`,
ariaLabel: `tree-${this.instanceId}-label`,
parentHtmlContainer: this.$refs.tree,
value: this.state,
value: Array.isArray(this.state) ? this.state : [this.state],
options,
searchable,
showCount,
Expand All @@ -52,9 +58,24 @@ export default function selectTree({
rtl
});

this.tree.srcElement.addEventListener('input', (e) => {
window.treeselectInstances[this.instanceId] = tree;

tree.srcElement.addEventListener('input', (e) => {
this.state = e.detail;
});

this.initialized = true;

this.$watch('state', (newValue) => {
if (this.initialized && window.treeselectInstances[this.instanceId]) {
const tree = window.treeselectInstances[this.instanceId];
if (!newValue || (Array.isArray(newValue) && newValue.length === 0)) {
tree.updateValue([]);
} else {
tree.updateValue(Array.isArray(newValue) ? newValue : [newValue]);
}
}
});
}
}
}
2 changes: 1 addition & 1 deletion resources/views/select-tree.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<x-dynamic-component :component="$getFieldWrapperView()" :field="$field">
<div
wire:key="{{ rand() }}"
wire:key="{{ $getId() }}"
wire:ignore
x-ignore
@if (FilamentView::hasSpaMode())
Expand Down