Files
AVSDevR.HTMLUtils/R/htmlMarkOptional.R

28 lines
984 B
R

#' Explicitly mark a Shiny input as optional
#'
#' @param input The Shiny input to mark as optional
#' @param optClass The classes to add to the indicator text
#'
#' @return The modified input
#' @export
#'
#' @examples
#' x <- shiny::textInput("input_id", "Some text input")
#' htmlMarkOptional(x)
htmlMarkOptional <- function(input, optClass = "text-muted font-italic") {
opt_span <- htmltools::tags$span(class = optClass, " (optional)")
tq <- htmltools::tagQuery(input)
if (tq$hasClass("shiny-input-checkboxgroup")) {
tq <- tq$children("label")$append(opt_span)$reset()
} else if (tq$find(".shiny-options-group")$length() > 0) {
tq <- tq$children("label")$append(opt_span)$reset()
} else if (tq$find(".btn-file")$length() > 0) {
tq <- tq$children("label")$append(opt_span)$reset()
} else {
tq <- tq$
children(".checkbox")$find("label span")$append(opt_span)$reset()$
find("label.control-label")$append(opt_span)$reset()
}
tq$allTags()
}