File tree Expand file tree Collapse file tree 1 file changed +18
-5
lines changed
twe-design-system/docs/javascript Expand file tree Collapse file tree 1 file changed +18
-5
lines changed Original file line number Diff line number Diff line change
1
+ const selectTriggers = document . querySelectorAll ( ".select__trigger" ) ;
1
2
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
+ } ) ;
6
9
}
7
10
11
+ // Add this event listener to handle custom option selection
8
12
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
+
9
18
for ( const select of document . querySelectorAll ( '.select' ) ) {
10
19
if ( ! select . contains ( e . target ) ) {
11
20
select . classList . remove ( 'open' ) ;
12
21
}
13
22
}
14
- } ) ;
15
23
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments