Switch
Switch is a boolean form input with switch-style presentation. Use it for enabled/disabled settings or other immediate yes/no choices.
Contract¶
| Property | Type | Notes |
|---|---|---|
label |
str |
Text shown next to the switch. |
checked |
bool |
Current boolean state. Supports literal, store binding, and data-model binding. |
action |
str or action object |
Optional action emitted on change. Action objects can include confirm. |
params |
dict |
Optional action context. |
Value collected by actions: bool.
Action Confirmation¶
Use an action object with confirm to require confirmation before the switch change action is dispatched. If the user cancels the dialog, no backend action is sent and the switch keeps its previous state.
The component test page exposes a switch action counter and the last received payload so dispatch can be checked directly after confirming.
field = sdk.ui.Switch(
"confirm_python_switch",
sdk.i18n.t("components.switch.confirm.python_label"),
checked=False,
)
field.set_prop(
"action",
{
"name": "components.switch_confirm_action",
"context": {"source": "python_switch"},
"confirm": {
"text": sdk.i18n.t("components.switch.confirm.prompt"),
"confirm_text": sdk.i18n.t("components.switch.confirm.accept"),
"cancel_text": sdk.i18n.t("components.switch.confirm.cancel"),
},
},
)- kind: Switch
id: confirm_yaml_switch
label: "@t/components.switch.confirm.yaml_label"
checked: false
action:
name: components.switch_confirm_action
context:
source: yaml_switch
confirm:
text: "@t/components.switch.confirm.prompt"
confirm_text: "@t/components.switch.confirm.accept"
cancel_text: "@t/components.switch.confirm.cancel"Example¶
field = sdk.ui.Switch(
"components_test_forms_switch_store",
"Switch",
checked=bound.store("/components_test/forms/switch/store", scope="page", default=False),
)- kind: Switch
id: components_test_forms_yaml_switch
label: "Switch YAML"
checked: {type: store, scope: page, path: /components_test/forms/switch/store, default: false}Runtime¶
Use collect_input_ids to read true or false. Use checked.set property updates, stateUpdate, or dataModelUpdate to change the value from an action.