Creates an Element Plus switch with Vue instance.
Usage
el_switch(
id = NULL,
value = FALSE,
disabled = FALSE,
label = NULL,
label_position = c("top", "left", "right"),
label_width = NULL,
label_suffix = NULL,
required = FALSE,
error = NULL,
show_message = TRUE,
inline_message = FALSE,
width = NULL,
active_text = NULL,
inactive_text = NULL,
active_color = NULL,
inactive_color = NULL,
active_value = TRUE,
inactive_value = FALSE,
name = NULL,
validate_event = NULL,
active_action_icon = NULL,
active_icon = NULL,
aria_label = NULL,
before_change = NULL,
border_color = NULL,
inactive_action_icon = NULL,
inactive_icon = NULL,
inline_prompt = NULL,
loading = NULL,
size = NULL,
tabindex = NULL,
slots = NULL,
on = NULL,
session = NULL
)
update_el_switch(
session = shiny::getDefaultReactiveDomain(),
id,
value = NULL,
disabled = NULL,
active_text = NULL,
inactive_text = NULL,
active_color = NULL,
inactive_color = NULL,
label = NULL,
error = NULL,
active_value = NULL,
inactive_value = NULL,
name = NULL,
validate_event = NULL,
active_action_icon = NULL,
active_icon = NULL,
aria_label = NULL,
before_change = NULL,
border_color = NULL,
inactive_action_icon = NULL,
inactive_icon = NULL,
inline_prompt = NULL,
loading = NULL,
size = NULL,
tabindex = NULL
)Arguments
- id
Switch ID. Auto-generated UUID if
NULL.- value
Initial switch state. Default
FALSE.- disabled
Whether the switch is disabled. Default
FALSE.- label
A label shown with the component, as Shiny's inputs have: text or a tag.
NULL, the default, shows none. It is the component's accessible name too – tied to it withforwhere the component has a native input that takes the id<id>-input, else witharia-labelledby.- label_position
Where the label sits, as
el_form()'slabel_position:"top"(the default, as Shiny's labels sit), or beside the component, its text aligned"left"or"right"– which shows oncelabel_widthgives the labels a common width.- label_width
Width of a label beside the component, as a CSS unit, so that several line up. Element's
label-width.- label_suffix
Text after the label, such as
":". Element'slabel-suffix.- required
Draw Element's red asterisk before the label. It marks the field; it does not check it – shinyvalidate or
el_form()does that.- error
An error message shown under the component in Element's style, the field framed in red. Element's
error.- show_message, inline_message
Whether
error's message is shown, and whether beside the component rather than under it. Element'sshow-messageandinline-message.- width
Switch width in pixels (integer).
- active_text
Text displayed when switch is on.
- inactive_text
Text displayed when switch is off.
- active_color
Background color when switch is on (e.g.
"#409EFF"): Element Plus's CSS variable--el-switch-on-color, which replaced its deprecatedactive-color.- inactive_color
Background color when switch is off: the CSS variable
--el-switch-off-color, which replaced the deprecatedinactive-color.- active_value
Value reported to Shiny when switch is on. Default
TRUE.- inactive_value
Value reported to Shiny when switch is off. Default
FALSE.- name
Native
nameattribute of the inner checkbox.- validate_event
Whether a change triggers form validation. Default
TRUE.- active_action_icon
Component of the icon displayed in action when in
onstate. Element Plus'sactive-action-icon(string / Component). An icon's name, such as"Search".- active_icon
Component of the icon displayed when in
onstate, overridesactive-text. Element Plus'sactive-icon(string / Component). An icon's name, such as"Search".- aria_label
Same as
aria-labelin native input. Element Plus'saria-label(string).- before_change
Before-change hook before the switch state changes. If
falseis returned or aPromiseis returned and then is rejected, will stop switching. Element Plus'sbefore-change(() => Promise<boolean> | boolean).- border_color
Border color of the switch: the CSS variable
--el-switch-border-color, which replaced Element Plus's deprecatedborder-color.- inactive_action_icon
Component of the icon displayed in action when in
offstate. Element Plus'sinactive-action-icon(string / Component). An icon's name, such as"Search".- inactive_icon
Component of the icon displayed when in
offstate, overridesinactive-text. Element Plus'sinactive-icon(string / Component). An icon's name, such as"Search".- inline_prompt
Whether icon or text is displayed inside dot, only the first character will be rendered for text. Element Plus's
inline-prompt(boolean).- loading
Whether Switch is in loading state. Element Plus's
loading(boolean).- size
Size of Switch. Element Plus's
size(” | 'large' | 'default' | 'small').- tabindex
Tabindex for input. Element Plus's
tabindex(string / number).- slots
Named list of Element slot contents, such as
list(title = shiny::tags$b("Bold")). A shiny.element component given here is absorbed rather than nested. For a scoped slot, write the template withtemplate().- on
Handlers of your own, for an event not reported or to send something else: a named list of
JS()functions, one per event – Element's, or a DOM event with Vue's modifiers ("keyup.enter"). Each is called withreportand the event's arguments;report(name, value)setsinput$<id>_<name>. Seeel_widget().- session
In
el_switch(), deprecated: inside a module, wrapidinns(), as for any Shiny input; a session given here namespacesidonce more, with a warning. Inupdate_el_switch(), the Shiny session, the current one by default, as forshiny::updateTextInput().
Shiny inputs
| Input | Reported | Value |
input$<id> | unasked | active_value when on, inactive_value when off |
The same list as el_events("el_switch"), which says how an event's arguments travel.
Binding
Every argument can follow v_bind() or v_model() except label, label_position, label_width, label_suffix, required, error, show_message, inline_message (the form item around the component, drawn by R); width (the mount point's style).
Updating from the server
Server-side update for el_switch(). Pass only the fields to change;
NULL fields are excluded from the update message.
Every other argument of el_switch() that can change once it is
drawn is an argument here too, under the same name. One left NULL
stays as it is; NA returns it to Element's default.
update_el_switch() is called for its side effect and returns NULL invisibly.
Examples
el_switch("sw1", value = TRUE)
#> <div id="sw1" data-shiny-vue style="display: contents">
#> <script type="text/x-template" data-shiny-vue-template><div id="sw1_container" style="display: contents">
#> <el-switch v-bind="$optional('switch')" v-model="value" :disabled="disabled" :active-text="activeText" :inactive-text="inactiveText" :style="{ '--el-switch-on-color': activeColor || undefined, '--el-switch-off-color': inactiveColor || undefined, '--el-switch-border-color': borderColor || undefined }" :active-value="activeValue" :inactive-value="inactiveValue" @change="handleChange"></el-switch>
#> </div></script>
#> <script type="application/json" data-shiny-vue-options>{"options":{"data":{"value":true,"disabled":false,"activeText":"","inactiveText":"","activeColor":"","inactiveColor":"","activeValue":true,"inactiveValue":false,"width":null,"name":null,"validateEvent":null,"activeActionIcon":null,"activeIcon":null,"ariaLabel":null,"beforeChange":null,"borderColor":null,"inactiveActionIcon":null,"inactiveIcon":null,"inlinePrompt":null,"loading":null,"size":null,"tabindex":null},"methods":{"handleChange":"function(value) { }"}},"input":"value","rate":null,"type":null,"use":["shinyElement.plugin"],"optional":{"switch":{"width":"width","name":"name","validate-event":"validateEvent","active-action-icon":"activeActionIcon","active-icon":"activeIcon","aria-label":"ariaLabel","before-change":"beforeChange","border-color":"borderColor","inactive-action-icon":"inactiveActionIcon","inactive-icon":"inactiveIcon","inline-prompt":"inlinePrompt","loading":"loading","size":"size","tabindex":"tabindex"}},"evals":["options.methods.handleChange"]}</script>
#> </div>
if (interactive()) {
library(shiny)
library(shiny.element)
ui <- el_page(
el_switch("sw1", active_text = "On", inactive_text = "Off"),
verbatimTextOutput("state")
)
server <- function(input, output, session) {
output$state <- renderPrint(input$sw1)
}
shinyApp(ui, server)
}
if (interactive()) {
# inside a server function
observeEvent(input$go, {
update_el_switch(session, "live", value = TRUE)
})
}