|
| 1 | +#' Create a button group of radio/check boxes |
| 2 | +#' |
| 3 | +#' Use `input_check_buttons()` if multiple choices may be selected at once; otherwise, use `input_radio_buttons()` |
| 4 | +#' |
| 5 | +#' @inheritParams input_check_search |
| 6 | +#' @param size size of the button group |
| 7 | +#' @param bg a theme color to use for the btn modifier class |
| 8 | +#' @export |
| 9 | +input_check_buttons <- function(id, choices, ..., selected = NULL, size = c("md", "sm", "lg"), bg = "primary") { |
| 10 | + size <- match.arg(size) |
| 11 | + tag <- div( |
| 12 | + id = id, |
| 13 | + class = "btn-group bslib-toggle-buttons", |
| 14 | + class = if (size != "md") paste0("btn-group-", size), |
| 15 | + role = "group", |
| 16 | + ..., |
| 17 | + !!!input_buttons_container( |
| 18 | + type = "checkbox", id = id, choices = choices, selected = selected, |
| 19 | + size = size, bg = bg |
| 20 | + ), |
| 21 | + toggle_dependency() |
| 22 | + ) |
| 23 | + tag <- tag_require(tag, version = 5, caller = "input_check_buttons()") |
| 24 | + as_fragment(tag) |
| 25 | +} |
| 26 | + |
| 27 | +#' @export |
| 28 | +#' @rdname input_check_buttons |
| 29 | +update_check_buttons <- function(id, choices = NULL, selected = NULL, session = get_current_session()) { |
| 30 | + if (!is.null(choices)) { |
| 31 | + choices <- processDeps( |
| 32 | + input_buttons_container(type = "checkbox", id, choices, selected), |
| 33 | + session |
| 34 | + ) |
| 35 | + } |
| 36 | + message <- dropNulls(list( |
| 37 | + choices = choices, |
| 38 | + selected = as.list(selected) |
| 39 | + )) |
| 40 | + session$sendInputMessage(id, message) |
| 41 | +} |
| 42 | + |
| 43 | +#' @export |
| 44 | +#' @rdname input_check_buttons |
| 45 | +input_radio_buttons <- function(id, choices, ..., selected = NULL, size = c("md", "sm", "lg"), bg = "primary") { |
| 46 | + size <- match.arg(size) |
| 47 | + tag <- div( |
| 48 | + id = id, |
| 49 | + class = "btn-group bslib-toggle-buttons", |
| 50 | + class = if (size != "md") paste0("btn-group-", size), |
| 51 | + role = "group", |
| 52 | + ..., |
| 53 | + !!!input_buttons_container( |
| 54 | + type = "radio", id = id, choices = choices, selected = selected, |
| 55 | + size = size, bg = bg |
| 56 | + ), |
| 57 | + toggle_dependency() |
| 58 | + ) |
| 59 | + tag <- tag_require(tag, version = 5, caller = "input_radio_buttons()") |
| 60 | + as_fragment(tag) |
| 61 | +} |
| 62 | + |
| 63 | +#' @export |
| 64 | +#' @rdname input_check_buttons |
| 65 | +update_radio_buttons <- function(id, choices = NULL, selected = NULL, session = get_current_session()) { |
| 66 | + if (!is.null(choices)) { |
| 67 | + choices <- processDeps( |
| 68 | + input_buttons_container(type = "radio", id, choices, selected), |
| 69 | + session |
| 70 | + ) |
| 71 | + } |
| 72 | + message <- dropNulls(list( |
| 73 | + choices = choices, |
| 74 | + selected = as.list(selected) |
| 75 | + )) |
| 76 | + session$sendInputMessage(id, message) |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +input_buttons_container <- function(type = c("radio", "checkbox"), id, choices, selected, size = "md", bg = "primary") { |
| 81 | + |
| 82 | + if (is.null(names(choices)) && is.atomic(choices)) { |
| 83 | + names(choices) <- choices |
| 84 | + } |
| 85 | + if (is.null(names(choices))) { |
| 86 | + stop("names() must be provided on list() vectors provided to choices") |
| 87 | + } |
| 88 | + |
| 89 | + vals <- rlang::names2(choices) |
| 90 | + #if (!all(nzchar(vals))) { |
| 91 | + # stop("Input values must be non-empty character strings") |
| 92 | + #} |
| 93 | + |
| 94 | + is_checked <- vapply(vals, function(x) isTRUE(x %in% selected) || identical(I("all"), selected), logical(1)) |
| 95 | + |
| 96 | + if (!any(is_checked) && !identical(selected, I("none"))) { |
| 97 | + is_checked[1] <- TRUE |
| 98 | + } |
| 99 | + |
| 100 | + type <- match.arg(type) |
| 101 | + if (type == "radio" && sum(is_checked) > 1) { |
| 102 | + stop("input_radio_buttons() doesn't support more than one selected choice (do you want input_check_buttons() instead?)", call. = FALSE) |
| 103 | + } |
| 104 | + |
| 105 | + inputs <- Map( |
| 106 | + vals, choices, is_checked, paste0(id, "-", seq_along(is_checked)), |
| 107 | + f = function(val, lbl, checked, this_id) { |
| 108 | + list( |
| 109 | + tags$input( |
| 110 | + type = type, class = "btn-check", name = id, |
| 111 | + id = this_id, autocomplete = "off", |
| 112 | + `data-value` = val, |
| 113 | + checked = if (checked) NA |
| 114 | + ), |
| 115 | + tags$label( |
| 116 | + class = paste0("btn btn-outline-", bg), |
| 117 | + `for` = this_id, lbl |
| 118 | + ) |
| 119 | + ) |
| 120 | + } |
| 121 | + ) |
| 122 | + |
| 123 | + inputs <- unlist(inputs, recursive = FALSE, use.names = FALSE) |
| 124 | +} |
| 125 | + |
| 126 | +toggle_dependency <- function() { |
| 127 | + htmltools::htmlDependency( |
| 128 | + "bslib-toggle-buttons", |
| 129 | + version = get_package_version("bslib"), |
| 130 | + package = "bslib", |
| 131 | + src = "components", |
| 132 | + script = "toggle-buttons.js" |
| 133 | + ) |
| 134 | +} |
0 commit comments