Skip to content

Commit 4e51c3b

Browse files
committed
docs(switch): additional demos showing FACE and cancellable events
1 parent 9ae672a commit 4e51c3b

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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>

0 commit comments

Comments
 (0)