Added methods for manipulating element classes and improved the optional/required when fieldset is used

This commit is contained in:
2026-02-05 10:55:30 +00:00
parent 066455efa0
commit 0d4b0e362b
7 changed files with 131 additions and 3 deletions

View File

@@ -3,12 +3,15 @@
export(htmlAddBoxAttributes)
export(htmlAddBoxHelpLink)
export(htmlAddBoxRegionFromTitle)
export(htmlAddClasses)
export(htmlButtonStyle)
export(htmlDisableAutocomplete)
export(htmlFixBoxCollapseButtonAria)
export(htmlMarkOptional)
export(htmlMarkRequired)
export(htmlRemoveAttributes)
export(htmlRemoveBoxTitle)
export(htmlRemoveClasses)
export(htmlReplaceBoxTitleLevel)
export(htmlSetMaxLength)
export(htmlSetMinLength)

49
R/htmlClassManipulation.R Normal file
View File

@@ -0,0 +1,49 @@
#' Add classes to an HTML element
#'
#' @param el The element to add the class(es) to
#' @param classes The class(es) to be added
#'
#' @return The modified element
#' @export
#'
#' @examples
#' x <- shiny::tags$div()
#' htmlAddClasses(x, "btn-warning")
htmlAddClasses <- function(el, classes) {
htmltools::tagQuery(el)$addClass(classes)$allTags()
}
#' Remove classes from an HTML element
#'
#' @param el The element to remove the class(es) from
#' @param classes The class(es) to be added
#'
#' @return The modified element
#' @export
#'
#' @examples
#' x <- shiny::tags$div(class = "btn btn-warning")
#' htmlRemoveClasses(x, "btn-warning")
htmlRemoveClasses <- function(el, classes) {
htmltools::tagQuery(el)$removeClass(classes)$allTags()
}
#' Changes the style of a button from btn-default to another style
#'
#' @param el The button
#' @param style The style to change the button on
#'
#' @return The modified element
#' @export
#'
#' @examples
#' x <- shiny::tags$div(class = "btn btn-warning")
#' htmlButtonStyle(x, "warning")
htmlButtonStyle <- function(el, style) {
stopifnot(style %in% c(
"default", "primary", "info", "warning", "success", "danger", "link"
))
el <- htmlRemoveClasses(el, "btn-default")
el <- htmlAddClasses(el, paste0("btn-", style))
el
}

View File

@@ -12,7 +12,9 @@
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")) {
if (tq$find("legend")$length() > 0) {
tq <- tq$children("legend")$append(opt_span)$reset()
} else 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()

View File

@@ -12,12 +12,17 @@
htmlMarkRequired <- function(input, reqClass = "text-danger required") {
req_span <- htmltools::tags$span(class = reqClass, "*")
tq <- htmltools::tagQuery(input)
if (tq$hasClass("shiny-input-checkboxgroup")) {
if (tq$find("legend")$length() > 0) {
tq <- tq$children("legend")$append(req_span)$reset()
tq <- tq$find("input")$addAttr(
required = "true", `aria-required` = "true"
)$reset()
} else if (tq$hasClass("shiny-input-checkboxgroup")) {
tq <- tq$addAttr(`aria-required` = "true")$reset()
tq <- tq$children("label")$append(req_span)$reset()
} else if (tq$find(".shiny-options-group")$length() > 0) {
tq <- tq$children("label")$append(req_span)$reset()
tq <- tq$addAttr(`aria-required` = "true")$reset()
tq <- tq$children("label")$append(req_span)$reset()
tq <- tq$find("input")$addAttr(
required = "true", `aria-required` = "true"
)$reset()

23
man/htmlAddClasses.Rd Normal file
View File

@@ -0,0 +1,23 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/htmlClassManipulation.R
\name{htmlAddClasses}
\alias{htmlAddClasses}
\title{Add classes to an HTML element}
\usage{
htmlAddClasses(el, classes)
}
\arguments{
\item{el}{The element to add the class(es) to}
\item{classes}{The class(es) to be added}
}
\value{
The modified element
}
\description{
Add classes to an HTML element
}
\examples{
x <- shiny::tags$div()
htmlAddClasses(x, "btn-warning")
}

23
man/htmlButtonStyle.Rd Normal file
View File

@@ -0,0 +1,23 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/htmlClassManipulation.R
\name{htmlButtonStyle}
\alias{htmlButtonStyle}
\title{Changes the style of a button from btn-default to another style}
\usage{
htmlButtonStyle(el, style)
}
\arguments{
\item{el}{The button}
\item{style}{The style to change the button on}
}
\value{
The modified element
}
\description{
Changes the style of a button from btn-default to another style
}
\examples{
x <- shiny::tags$div(class = "btn btn-warning")
htmlButtonStyle(x, "warning")
}

23
man/htmlRemoveClasses.Rd Normal file
View File

@@ -0,0 +1,23 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/htmlClassManipulation.R
\name{htmlRemoveClasses}
\alias{htmlRemoveClasses}
\title{Remove classes from an HTML element}
\usage{
htmlRemoveClasses(el, classes)
}
\arguments{
\item{el}{The element to remove the class(es) from}
\item{classes}{The class(es) to be added}
}
\value{
The modified element
}
\description{
Remove classes from an HTML element
}
\examples{
x <- shiny::tags$div(class = "btn btn-warning")
htmlRemoveClasses(x, "btn-warning")
}