Compare commits

...

2 Commits

Author SHA1 Message Date
Carson
0be84710fd Add BS5 support as well 2021-03-23 17:54:14 -05:00
Carson
967078ebbb Quick implementation of switchInput() 2021-03-23 17:29:32 -05:00
26 changed files with 170 additions and 9 deletions

View File

@@ -156,6 +156,7 @@ Collate:
'input-select.R'
'input-slider.R'
'input-submit.R'
'input-switch.R'
'input-text.R'
'input-textarea.R'
'input-utils.R'

View File

@@ -280,6 +280,7 @@ export(stopApp)
export(strong)
export(submitButton)
export(suppressDependencies)
export(switchInput)
export(tabPanel)
export(tabPanelBody)
export(tableOutput)
@@ -314,6 +315,7 @@ export(updateRadioButtons)
export(updateSelectInput)
export(updateSelectizeInput)
export(updateSliderInput)
export(updateSwitchInput)
export(updateTabsetPanel)
export(updateTextAreaInput)
export(updateTextInput)

65
R/input-switch.R Normal file
View File

@@ -0,0 +1,65 @@
#' Switch Input Control
#'
#' Create a switch for toggling a logical value.
#'
#' @inheritParams checkboxInput
#' @return A switch control that can be added to a UI definition.
#'
#' @family input elements
#' @seealso [checkboxInput()], [updateSwitchInput()]
#'
#' @examples
#' ## Only run examples in interactive R sessions
#' if (interactive()) {
#' ui <- fluidPage(
#' switchInput("somevalue", "Some value", FALSE),
#' verbatimTextOutput("value")
#' )
#' server <- function(input, output) {
#' output$value <- renderText({ input$somevalue })
#' }
#' shinyApp(ui, server)
#' }
#'
#' @section Server value:
#' `TRUE` if checked, `FALSE` otherwise.
#'
#' @export
switchInput <- function(inputId, label, value = FALSE, width = NULL) {
value <- restoreInput(id = inputId, default = value)
inputTag <- tags$input(
id = inputId, type = "checkbox",
checked = if (isTRUE(value)) "checked"
)
# TODO: checkboxInput() should do this too (for accessibility)?
labelTag <- shinyInputLabel(inputId, label)
tagFunction(function() {
if (getCurrentVersion() < 4) {
stop(
"switchInput() requires Bootstrap 4 or higher. ",
"Please supply `bslib::bs_theme()` to the UI's page layout function ",
"(e.g., `fluidPage(theme = bslib::bs_theme())`).",
call. = FALSE
)
}
isBS4 <- getCurrentVersion() == 4
div(
class = "shiny-input-container",
style = css(width = validateCssUnit(width)),
div(
class = if (isBS4) "custom-control custom-switch" else "form-check form-switch",
tagAppendAttributes(
inputTag, class = if (isBS4) "custom-control-input" else "form-check-input"
),
tagAppendAttributes(
labelTag, class = if (isBS4) "custom-control-label" else "form-check-label"
)
)
)
})
}

View File

@@ -115,6 +115,12 @@ updateCheckboxInput <- function(session = getDefaultReactiveDomain(), inputId, l
session$sendInputMessage(inputId, message)
}
#' @rdname updateCheckboxInput
#' @export
updateSwitchInput <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, value = NULL) {
updateCheckboxInput(session = session, inputId = inputId, label = label, value = value)
}
#' Change the label or icon of an action button on the client
#'

View File

@@ -5163,7 +5163,7 @@
},
getState: function getState(el) {
return {
label: import_jquery6.default(el).parent().find("span").text(),
label: import_jquery6.default(el).parent().find("span, label").text(),
value: el.checked
};
},
@@ -5171,7 +5171,7 @@
if (data.hasOwnProperty("value"))
el.checked = data.value;
if (data.hasOwnProperty("label"))
import_jquery6.default(el).parent().find("span").text(data.label);
import_jquery6.default(el).parent().find("span, label").text(data.label);
import_jquery6.default(el).trigger("change");
}
});

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -83,6 +83,7 @@ Other input elements:
\code{\link{selectInput}()},
\code{\link{sliderInput}()},
\code{\link{submitButton}()},
\code{\link{switchInput}()},
\code{\link{textAreaInput}()},
\code{\link{textInput}()},
\code{\link{varSelectInput}()}

View File

@@ -112,6 +112,7 @@ Other input elements:
\code{\link{selectInput}()},
\code{\link{sliderInput}()},
\code{\link{submitButton}()},
\code{\link{switchInput}()},
\code{\link{textAreaInput}()},
\code{\link{textInput}()},
\code{\link{varSelectInput}()}

View File

@@ -57,6 +57,7 @@ Other input elements:
\code{\link{selectInput}()},
\code{\link{sliderInput}()},
\code{\link{submitButton}()},
\code{\link{switchInput}()},
\code{\link{textAreaInput}()},
\code{\link{textInput}()},
\code{\link{varSelectInput}()}

View File

@@ -143,6 +143,7 @@ Other input elements:
\code{\link{selectInput}()},
\code{\link{sliderInput}()},
\code{\link{submitButton}()},
\code{\link{switchInput}()},
\code{\link{textAreaInput}()},
\code{\link{textInput}()},
\code{\link{varSelectInput}()}

View File

@@ -147,6 +147,7 @@ Other input elements:
\code{\link{selectInput}()},
\code{\link{sliderInput}()},
\code{\link{submitButton}()},
\code{\link{switchInput}()},
\code{\link{textAreaInput}()},
\code{\link{textInput}()},
\code{\link{varSelectInput}()}

View File

@@ -113,6 +113,7 @@ Other input elements:
\code{\link{selectInput}()},
\code{\link{sliderInput}()},
\code{\link{submitButton}()},
\code{\link{switchInput}()},
\code{\link{textAreaInput}()},
\code{\link{textInput}()},
\code{\link{varSelectInput}()}

View File

@@ -71,6 +71,7 @@ Other input elements:
\code{\link{selectInput}()},
\code{\link{sliderInput}()},
\code{\link{submitButton}()},
\code{\link{switchInput}()},
\code{\link{textAreaInput}()},
\code{\link{textInput}()},
\code{\link{varSelectInput}()}

View File

@@ -65,6 +65,7 @@ Other input elements:
\code{\link{selectInput}()},
\code{\link{sliderInput}()},
\code{\link{submitButton}()},
\code{\link{switchInput}()},
\code{\link{textAreaInput}()},
\code{\link{textInput}()},
\code{\link{varSelectInput}()}

View File

@@ -128,6 +128,7 @@ Other input elements:
\code{\link{selectInput}()},
\code{\link{sliderInput}()},
\code{\link{submitButton}()},
\code{\link{switchInput}()},
\code{\link{textAreaInput}()},
\code{\link{textInput}()},
\code{\link{varSelectInput}()}

View File

@@ -148,6 +148,7 @@ Other input elements:
\code{\link{radioButtons}()},
\code{\link{sliderInput}()},
\code{\link{submitButton}()},
\code{\link{switchInput}()},
\code{\link{textAreaInput}()},
\code{\link{textInput}()},
\code{\link{varSelectInput}()}

View File

@@ -151,6 +151,7 @@ Other input elements:
\code{\link{radioButtons}()},
\code{\link{selectInput}()},
\code{\link{submitButton}()},
\code{\link{switchInput}()},
\code{\link{textAreaInput}()},
\code{\link{textInput}()},
\code{\link{varSelectInput}()}

View File

@@ -76,6 +76,7 @@ Other input elements:
\code{\link{radioButtons}()},
\code{\link{selectInput}()},
\code{\link{sliderInput}()},
\code{\link{switchInput}()},
\code{\link{textAreaInput}()},
\code{\link{textInput}()},
\code{\link{varSelectInput}()}

64
man/switchInput.Rd Normal file
View File

@@ -0,0 +1,64 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/input-switch.R
\name{switchInput}
\alias{switchInput}
\title{Switch Input Control}
\usage{
switchInput(inputId, label, value = FALSE, width = NULL)
}
\arguments{
\item{inputId}{The \code{input} slot that will be used to access the value.}
\item{label}{Display label for the control, or \code{NULL} for no label.}
\item{value}{Initial value (\code{TRUE} or \code{FALSE}).}
\item{width}{The width of the input, e.g. \code{'400px'}, or \code{'100\%'};
see \code{\link[=validateCssUnit]{validateCssUnit()}}.}
}
\value{
A switch control that can be added to a UI definition.
}
\description{
Create a switch for toggling a logical value.
}
\section{Server value}{
\code{TRUE} if checked, \code{FALSE} otherwise.
}
\examples{
## Only run examples in interactive R sessions
if (interactive()) {
ui <- fluidPage(
switchInput("somevalue", "Some value", FALSE),
verbatimTextOutput("value")
)
server <- function(input, output) {
output$value <- renderText({ input$somevalue })
}
shinyApp(ui, server)
}
}
\seealso{
\code{\link[=checkboxInput]{checkboxInput()}}, \code{\link[=updateSwitchInput]{updateSwitchInput()}}
Other input elements:
\code{\link{actionButton}()},
\code{\link{checkboxGroupInput}()},
\code{\link{checkboxInput}()},
\code{\link{dateInput}()},
\code{\link{dateRangeInput}()},
\code{\link{fileInput}()},
\code{\link{numericInput}()},
\code{\link{passwordInput}()},
\code{\link{radioButtons}()},
\code{\link{selectInput}()},
\code{\link{sliderInput}()},
\code{\link{submitButton}()},
\code{\link{textAreaInput}()},
\code{\link{textInput}()},
\code{\link{varSelectInput}()}
}
\concept{input elements}

View File

@@ -91,6 +91,7 @@ Other input elements:
\code{\link{selectInput}()},
\code{\link{sliderInput}()},
\code{\link{submitButton}()},
\code{\link{switchInput}()},
\code{\link{textInput}()},
\code{\link{varSelectInput}()}
}

View File

@@ -63,6 +63,7 @@ Other input elements:
\code{\link{selectInput}()},
\code{\link{sliderInput}()},
\code{\link{submitButton}()},
\code{\link{switchInput}()},
\code{\link{textAreaInput}()},
\code{\link{varSelectInput}()}
}

View File

@@ -2,6 +2,7 @@
% Please edit documentation in R/update-input.R
\name{updateCheckboxInput}
\alias{updateCheckboxInput}
\alias{updateSwitchInput}
\title{Change the value of a checkbox input on the client}
\usage{
updateCheckboxInput(
@@ -10,6 +11,13 @@ updateCheckboxInput(
label = NULL,
value = NULL
)
updateSwitchInput(
session = getDefaultReactiveDomain(),
inputId,
label = NULL,
value = NULL
)
}
\arguments{
\item{session}{The \code{session} object passed to function given to

View File

@@ -140,6 +140,7 @@ Other input elements:
\code{\link{selectInput}()},
\code{\link{sliderInput}()},
\code{\link{submitButton}()},
\code{\link{switchInput}()},
\code{\link{textAreaInput}()},
\code{\link{textInput}()}
}

View File

@@ -4627,7 +4627,7 @@ function main(): void {
},
getState: function (el) {
return {
label: $(el).parent().find("span").text(),
label: $(el).parent().find("span, label").text(),
value: el.checked,
};
},
@@ -4637,7 +4637,7 @@ function main(): void {
// checkboxInput()'s label works different from other
// input labels...the label container should always exist
if (data.hasOwnProperty("label"))
$(el).parent().find("span").text(data.label);
$(el).parent().find("span, label").text(data.label);
$(el).trigger("change");
},