forked from ToolJet/ToolJet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeSelect.jsx
More file actions
134 lines (123 loc) · 3.94 KB
/
TreeSelect.jsx
File metadata and controls
134 lines (123 loc) · 3.94 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import React, { useState, useEffect, useMemo } from 'react';
// eslint-disable-next-line import/no-unresolved
import CheckboxTree from 'react-checkbox-tree';
// eslint-disable-next-line import/no-unresolved
import 'react-checkbox-tree/lib/react-checkbox-tree.css';
import { isExpectedDataType } from '@/_helpers/utils.js';
export const TreeSelect = ({
height,
properties,
styles,
setExposedVariable,
setExposedVariables,
fireEvent,
darkMode,
dataCy,
}) => {
const { label } = properties;
const { visibility, disabledState, checkboxColor, boxShadow } = styles;
const textColor = darkMode && styles.textColor === '#000' ? '#fff' : styles.textColor;
const [checked, setChecked] = useState(checkedData);
const [expanded, setExpanded] = useState(expandedData);
const data = isExpectedDataType(properties.data, 'array');
const checkedData = isExpectedDataType(properties.checkedData, 'array');
const expandedData = isExpectedDataType(properties.expandedData, 'array');
let pathObj = {};
useEffect(() => {
const checkedArr = [],
checkedPathArray = [],
checkedPathString = [];
const updateCheckedArr = (array = [], selected, isSelected = false) => {
array.forEach((node) => {
if (isSelected || selected.includes(node.value)) {
checkedArr.push(node.value);
updateCheckedArr(node.children, selected, true);
} else {
updateCheckedArr(node.children, selected);
}
});
};
updateCheckedArr(data, checkedData);
setChecked(checkedArr);
checkedArr.forEach((item) => {
checkedPathArray.push(pathObj[item]);
checkedPathString.push(pathObj[item].join('-'));
});
const exposedVariables = {
checkedPathArray: checkedPathArray,
checkedPathStrings: checkedPathString,
checked: checkedArr,
};
setExposedVariables(exposedVariables);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [JSON.stringify(checkedData), JSON.stringify(data)]);
useEffect(() => {
setExposedVariable('expanded', expandedData);
setExpanded(expandedData);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [JSON.stringify(expandedData)]);
pathObj = useMemo(() => {
let nodePath = {};
function checkedPath(nodes, arr = []) {
for (const node of nodes) {
nodePath[node.value] = [...arr, node.value];
if (node?.children?.length > 0) {
checkedPath(node.children, [...arr, node.value]);
}
}
}
checkedPath(data, []);
return nodePath;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [JSON.stringify(data)]);
const onCheck = (checked, updatedNode) => {
const checkedPathArray = [],
checkedPathString = [];
checked.forEach((item) => {
checkedPathArray.push(pathObj[item]);
checkedPathString.push(pathObj[item].join('-'));
});
const exposedVariables = {
checkedPathArray: checkedPathArray,
checkedPathStrings: checkedPathString,
checked: checked,
};
setExposedVariables(exposedVariables);
updatedNode.checked ? fireEvent('onCheck') : fireEvent('onUnCheck');
fireEvent('onChange');
setChecked(checked);
};
const onExpand = (expanded) => {
setExposedVariable('expanded', expanded);
setExpanded(expanded);
};
return (
<div
className="custom-checkbox-tree"
data-disabled={disabledState}
style={{
maxHeight: height,
display: visibility ? '' : 'none',
color: textColor,
accentColor: checkboxColor,
boxShadow,
}}
data-cy={dataCy}
>
<div className="card-title" style={{ marginBottom: '0.5rem' }}>
{label}
</div>
<CheckboxTree
nodes={data}
checked={checked}
expanded={expanded}
showNodeIcon={false}
onCheck={onCheck}
onExpand={onExpand}
nativeCheckboxes
checkModel="all"
disabled={disabledState}
/>
</div>
);
};