forked from CodeWithDennis/filament-select-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (73 loc) · 2.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import Treeselect from 'treeselectjs'
// Store treeselect instances globally
window.treeselectInstances = window.treeselectInstances || {};
export default function selectTree({
state,
name,
options,
searchable,
showCount,
placeholder,
rtl,
disabledBranchNode = true,
disabled = false,
isSingleSelect = true,
showTags = true,
clearable = true,
isIndependentNodes = true,
alwaysOpen = false,
emptyText,
expandSelected = true,
grouped = true,
openLevel = 0,
direction = 'auto'
}) {
return {
state,
initialized: false,
instanceId: null,
init() {
// 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: Array.isArray(this.state) ? this.state : [this.state],
options,
searchable,
showCount,
placeholder,
disabledBranchNode,
disabled,
isSingleSelect,
showTags,
clearable,
isIndependentNodes,
alwaysOpen,
emptyText,
expandSelected,
grouped,
openLevel,
direction,
rtl
});
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]);
}
}
});
}
}
}