File tree Expand file tree Collapse file tree
elements/pf-v6-switch/demo Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ name: Cancelable change
3+ description: The change event is cancelable. Calling preventDefault() rejects the toggle and reverts the switch to its previous state.
4+ ---
5+ < pf-v6-switch id ="locked-switch " checked > Locked setting</ pf-v6-switch >
6+ < p id ="message "> This switch cannot be toggled off.</ p >
7+
8+ < script type ="module ">
9+ import '@patternfly/elements/pf-v6-switch/pf-v6-switch.js' ;
10+
11+ const sw = document . getElementById ( 'locked-switch' ) ;
12+ const message = document . getElementById ( 'message' ) ;
13+
14+ sw . addEventListener ( 'change' , function ( event ) {
15+ if ( ! sw . checked ) {
16+ event . preventDefault ( ) ;
17+ message . textContent = 'Toggle rejected — this setting is locked on.' ;
18+ } else {
19+ message . textContent = 'This switch cannot be toggled off.' ;
20+ }
21+ } ) ;
22+ </ script >
Original file line number Diff line number Diff line change 1+ ---
2+ name: Form association
3+ description: A switch can participate in forms. It submits "on" when checked and is omitted when unchecked. Use the reset button to restore the initial state.
4+ ---
5+ < form id ="settings-form ">
6+ < pf-v6-switch name ="notifications " checked > Notifications</ pf-v6-switch >
7+ < br > < br >
8+ < button type ="submit "> Submit</ button >
9+ < button type ="reset "> Reset</ button >
10+ < pre id ="output "> </ pre >
11+ </ form >
12+
13+ < script type ="module ">
14+ import '@patternfly/elements/pf-v6-switch/pf-v6-switch.js' ;
15+
16+ const form = document . getElementById ( 'settings-form' ) ;
17+ const output = document . getElementById ( 'output' ) ;
18+
19+ form . addEventListener ( 'submit' , function ( event ) {
20+ event . preventDefault ( ) ;
21+ const data = new FormData ( form ) ;
22+ const entries = [ ...data . entries ( ) ] ;
23+ output . textContent = entries . length
24+ ? entries . map ( ( [ k , v ] ) => `${ k } =${ v } ` ) . join ( '\n' )
25+ : '(no values submitted)' ;
26+ } ) ;
27+ </ script >
You can’t perform that action at this time.
0 commit comments