Skip to content

Commit aa977de

Browse files
committed
fixed dropdown select bug
1 parent 635d187 commit aa977de

File tree

1 file changed

+18
-5
lines changed
  • twe-design-system/docs/javascript

1 file changed

+18
-5
lines changed
Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1+
const selectTriggers = document.querySelectorAll(".select__trigger");
12

2-
for (const dropdown of document.querySelectorAll(".select-wrapper")) {
3-
dropdown.addEventListener('click', function() {
4-
this.querySelector('.select').classList.toggle('open');
5-
})
3+
for (const trigger of selectTriggers) {
4+
trigger.addEventListener('click', function(e) {
5+
e.stopPropagation(); // Prevent the click event from propagating to the window
6+
const dropdown = this.closest('.select-wrapper').querySelector('.select');
7+
dropdown.classList.toggle('open');
8+
});
69
}
710

11+
// Add this event listener to handle custom option selection
812
window.addEventListener('click', function(e) {
13+
const allCustomOptions = document.querySelectorAll('.custom-option');
14+
for (const customOption of allCustomOptions) {
15+
customOption.classList.remove('selected'); // Remove the class from all options
16+
}
17+
918
for (const select of document.querySelectorAll('.select')) {
1019
if (!select.contains(e.target)) {
1120
select.classList.remove('open');
1221
}
1322
}
14-
});
1523

24+
// Check if the clicked element is a custom option and add the class
25+
if (e.target.classList.contains('custom-option')) {
26+
e.target.classList.add('selected');
27+
}
28+
});

0 commit comments

Comments
 (0)