Skip to content

Commit 5daec80

Browse files
Akiyamkabsmth
andauthored
Update the “Expanding list” example (#81)
* Update main.js Unnecessary code removed. No need to declare the 'self' variable - we already have 'this' for this purpose No need to declare a constructor - JavaScript will call super automatically if no constructor is declared No need to convert the node list to an array - we have 'for' loops that can iterate over node lists directly * default prettier formating --------- Co-authored-by: Brian Smith <brian@smith.berlin>
1 parent de7c20d commit 5daec80

1 file changed

Lines changed: 21 additions & 23 deletions

File tree

  • expanding-list-web-component

expanding-list-web-component/main.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
// Create a class for the element
22
class ExpandingList extends HTMLUListElement {
3-
constructor() {
4-
// Always call super first in constructor
5-
// Return value from super() is a reference to this element
6-
self = super();
7-
}
8-
93
connectedCallback() {
104
// Get ul and li elements that are a child of this custom ul element
115
// li elements can be containers if they have uls within them
12-
const uls = Array.from(self.querySelectorAll("ul"));
13-
const lis = Array.from(self.querySelectorAll("li"));
6+
const uls = this.querySelectorAll("ul");
7+
const lis = this.querySelectorAll("li");
8+
149
// Hide all child uls
1510
// These lists will be shown when the user clicks a higher level container
16-
uls.forEach((ul) => {
11+
for (const ul of uls) {
1712
ul.style.display = "none";
18-
});
13+
}
1914

2015
// Look through each li element in the ul
21-
lis.forEach((li) => {
16+
for (const li of lis) {
2217
// If this li has a ul as a child, decorate it and add a click handler
2318
if (li.querySelectorAll("ul").length > 0) {
24-
// Add an attribute which can be used by the style
19+
// Add an attribute which can be used by the style
2520
// to show an open or closed icon
2621
li.setAttribute("class", "closed");
2722

@@ -35,24 +30,27 @@ class ExpandingList extends HTMLUListElement {
3530
newSpan.style.cursor = "pointer";
3631

3732
// Add click handler to this span
38-
newSpan.addEventListener("click", (e) => {
33+
const onClick = (e) => {
3934
// next sibling to the span should be the ul
40-
const nextul = e.target.nextElementSibling;
35+
const nextUl = e.target.nextElementSibling;
4136

4237
// Toggle visible state and update class attribute on ul
43-
if (nextul.style.display == "block") {
44-
nextul.style.display = "none";
45-
nextul.parentNode.setAttribute("class", "closed");
38+
if (nextUl.style.display === "block") {
39+
nextUl.style.display = "none";
40+
nextUl.parentNode.setAttribute("class", "closed");
4641
} else {
47-
nextul.style.display = "block";
48-
nextul.parentNode.setAttribute("class", "open");
42+
nextUl.style.display = "block";
43+
nextUl.parentNode.setAttribute("class", "open");
4944
}
50-
});
45+
};
46+
47+
newSpan.addEventListener("click", onClick);
48+
5149
// Add the span and remove the bare text node from the li
52-
childText.parentNode.insertBefore(newSpan, childText);
53-
childText.parentNode.removeChild(childText);
50+
childText.parentNode?.insertBefore(newSpan, childText);
51+
childText.parentNode?.removeChild(childText);
5452
}
55-
});
53+
}
5654
}
5755
}
5856

0 commit comments

Comments
 (0)