playbook_ui 12.26.0.pre.alpha.multiselectfixes825 → 12.26.0.pre.alpha.railsmultilevelimprovements805

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 371b190965297927aba9f26c26fe3383d9c6ce959adca7e0a128027d1b0f2255
4
- data.tar.gz: baae24f5458e96ac76e0ce2b6f63ba34e0e1b91613095f789352a9f4cc3b3bc2
3
+ metadata.gz: 79c732fa4dc676050bdbc6dda57f9a180670d812ffeeb73ba5d410a683a2c6bf
4
+ data.tar.gz: 332c5b9b341d69f47d83af65dfd5936918ee3b7e4149884892a8c4926b7ec3e7
5
5
  SHA512:
6
- metadata.gz: bf19511362763021d0f7e77dfba5c1e47239692412c7076e377042b1573393f603efc84f7d8bd09d8f29946c4a0d9135b79ea4d90d83b50eed62835f89d3c733
7
- data.tar.gz: 24a916ccc779f9d9c585117d8582c94c1240305e3a12c4cb2382755206e24ecf776827914b1ee33be8125470b361a58eb3adb8078d48e51ccb68c4b459981f9c
6
+ metadata.gz: bd51e6d5feffbe630c6e315e17e108d3f2c2d55a55b2ba12fbe14894950f61e9d457cb29899993a7f990564140e95e78c8251e675cfa7f3048dacfb7cb24d72f
7
+ data.tar.gz: fd53b4b633ea48c3b6db1f2f87c017509db18cf1d7732d9a1992ee3147e8df41d6e0b05fa1bf5f876c2304c3ae71ddb8627deb1e5abd65de8d8f14ccac9bc139
@@ -1,16 +1,58 @@
1
+ //function for unchecking items in formattedData
2
+ export const unCheckIt = (
3
+ formattedData: { [key: string]: any }[],
4
+ id: string
5
+ ) => {
6
+ formattedData.map((item: { [key: string]: any }) => {
7
+ if (item.id === id && item.checked) {
8
+ item.checked = false;
9
+ }
10
+ if (item.children && item.children.length > 0) {
11
+ unCheckIt(item.children, id);
12
+ }
13
+ return item;
14
+ });
15
+ };
16
+
1
17
  //function to retrieve all ancestors of unchecked item and set checked to false
2
18
  export const getAncestorsOfUnchecked = (
3
- data: { [key: string]: any }[],
19
+ formattedData: { [key: string]: any }[],
4
20
  item: { [key: string]: any }
5
21
  ) => {
6
22
  if (item.parent_id) {
7
- const ancestor = filterFormattedDataById(data, item.parent_id);
8
- ancestor[0].checked = false;
9
- ancestor[0].parent_id && getAncestorsOfUnchecked(data, ancestor[0])
23
+ const ancestors = filterFormattedDataById(formattedData, item.parent_id);
24
+ ancestors[0].checked = false;
25
+
26
+ if (ancestors[0].parent_id) {
27
+ getAncestorsOfUnchecked(formattedData, ancestors[0]);
28
+ }
10
29
  }
11
- return data;
12
30
  };
13
-
31
+
32
+ //recursively check all child and grandchild items if parent checked
33
+ export const checkedRecursive = (item: { [key: string]: any }) => {
34
+ if (!item.checked) {
35
+ item.checked = true;
36
+ }
37
+ if (item.children && item.children.length > 0) {
38
+ item.children.forEach((childItem: { [key: string]: any }) => {
39
+ checkedRecursive(childItem);
40
+ });
41
+ }
42
+ };
43
+
44
+ //recursively uncheck all child and grandchild items if parent unchecked
45
+ export const unCheckedRecursive = (item: { [key: string]: any }) => {
46
+ if (item.checked) {
47
+ item.checked = false;
48
+ }
49
+ if (item.children && item.children.length > 0) {
50
+ item.children.forEach((childItem: { [key: string]: any }) => {
51
+ unCheckedRecursive(childItem);
52
+ });
53
+ }
54
+ };
55
+
14
56
  //function is going over formattedData and returning all objects that match the
15
57
  //id of the clicked item from the dropdown
16
58
  export const filterFormattedDataById = (
@@ -22,7 +64,6 @@ export const filterFormattedDataById = (
22
64
  for (const item of data) {
23
65
  if (item.id.toLowerCase() === (term.toLowerCase())) {
24
66
  matched.push(item);
25
- return
26
67
  }
27
68
 
28
69
  if (item.children && item.children.length > 0) {
@@ -75,60 +116,97 @@ export const getCheckedItems = (
75
116
  });
76
117
  return checkedItems;
77
118
  };
78
-
79
- export const getDefaultCheckedItems = (treeData:{ [key: string]: any }[]) => {
80
- const checkedDefault: { [key: string]: any }[] = [];
81
119
 
82
- const traverseTree = (items:{ [key: string]: any }[]) => {
83
- if (!Array.isArray(items)) {
84
- return;
120
+ export const getChildIds = (
121
+ item: { [key: string]: any },
122
+ defaultArray: { [key: string]: any }[]
123
+ ) => {
124
+ let childIds: string[] = [];
125
+ item.children.forEach((child: { [key: string]: any }) => {
126
+ childIds.push(child.id);
127
+ if (child.children && child.children.length > 0) {
128
+ const childChildIds = getChildIds(child, defaultArray);
129
+ childIds.push(...childChildIds);
85
130
  }
86
- items.forEach((item:{ [key: string]: any }) => {
87
- if (item.checked) {
88
- if (item.children && item.children.length > 0) {
89
- const uncheckedChildren = item.children.filter((child:{ [key: string]: any }) => !child.checked);
90
- if (uncheckedChildren.length === 0) {
91
- checkedDefault.push(item);
92
- return;
93
- }
94
- } else {
95
- const parent = items.find((parentItem:{ [key: string]: any }) => parentItem.id === item.parentId);
96
- if (!parent || !parent.checked) {
97
- checkedDefault.push(item);
98
- }
99
- }
100
- }
131
+ });
132
+ return childIds;
133
+ };
101
134
 
102
- if (item.children && item.children.length > 0) {
103
- traverseTree(item.children);
135
+ export const updateReturnItems = (newChecked: { [key: string]: any }[]) => {
136
+ const updatedCheckedItems: { [key: string]: any }[] = [];
137
+ for (const item of newChecked) {
138
+ if (item.children && item.children.length > 0) {
139
+ const allChildrenChecked = item.children.every(
140
+ (child: { [key: string]: any }) => child.checked
141
+ );
142
+ if (allChildrenChecked) {
143
+ updatedCheckedItems.push(item);
104
144
  }
105
- });
106
- };
107
-
108
- traverseTree(treeData);
109
-
110
- return checkedDefault;
145
+ }
146
+ const childItem = updatedCheckedItems.some((x) => x.id === item?.parent_id);
147
+ if (!childItem) {
148
+ updatedCheckedItems.push(item);
149
+ }
150
+ }
151
+ const filteredReturn = updatedCheckedItems.filter((item) => {
152
+ return !updatedCheckedItems.find(
153
+ (otherItem) => otherItem.id === item.parent_id
154
+ );
155
+ });
156
+ return filteredReturn;
111
157
  };
112
158
 
113
- export const recursiveCheckParent = (
114
- item: { [key: string]: any },
115
- data:any
159
+ export const recursiveReturnOnlyParent = (
160
+ items: { [key: string]: any },
161
+ formattedData: { [key: string]: any }[],
162
+ defaultReturn: { [key: string]: any }[],
163
+ setDefaultReturn: any
116
164
  ) => {
117
- if (item.parent_id !== null) {
118
- const parent = filterFormattedDataById(data, item.parent_id);
165
+ const parent = filterFormattedDataById(formattedData, items.parent_id);
119
166
  const allChildrenChecked = parent[0].children.every(
120
167
  (child: { [key: string]: any }) => child.checked
121
168
  );
122
169
  if (allChildrenChecked) {
170
+ // Only return the parent and remove its children from defaultReturn
123
171
  parent[0].checked = true;
172
+ const filteredDefaultReturn = defaultReturn.filter((item) => {
173
+ // Remove children of the specific parent
174
+ if (
175
+ parent[0].children.find(
176
+ (child: { [key: string]: any }) => child.id === item.id
177
+ )
178
+ ) {
179
+ return false;
180
+ }
181
+ });
182
+ setDefaultReturn([...filteredDefaultReturn, parent[0]]);
183
+ // Check if the parent has a parent and its children are all checked
124
184
  const parentHasParent = parent[0].parent_id !== null;
125
185
  if (parentHasParent) {
126
- recursiveCheckParent(
186
+ recursiveReturnOnlyParent(
127
187
  parent[0],
128
- data
188
+ formattedData,
189
+ filteredDefaultReturn,
190
+ setDefaultReturn
129
191
  );
130
192
  }
131
- }
132
- }
133
- return data;
134
- }
193
+ } else {
194
+ const checkedChildren = parent[0].children.filter(
195
+ (child: { [key: string]: any }) => child.checked
196
+ );
197
+ const updatedDefaultReturn = [...defaultReturn, ...checkedChildren];
198
+ setDefaultReturn(updatedDefaultReturn);
199
+ }
200
+ };
201
+
202
+ export const removeChildrenIfParentChecked = (
203
+ items: { [key: string]: any },
204
+ defaultReturn: { [key: string]: any }[],
205
+ setDefaultReturn: any
206
+ ) => {
207
+ const childIds = getChildIds(items, defaultReturn);
208
+ const filteredDefaultArray = defaultReturn.filter(
209
+ (item: { [key: string]: any }) => childIds !== item.id
210
+ );
211
+ setDefaultReturn([...filteredDefaultArray, items]);
212
+ };