Skip to content

Commit 2c09f4b

Browse files
committed
Add input_check_buttons() and input_radio_buttons()
1 parent e847d5a commit 2c09f4b

File tree

8 files changed

+242
-243
lines changed

8 files changed

+242
-243
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Collate:
6161
'deprecated.R'
6262
'files.R'
6363
'imports.R'
64-
'input-check-search.R'
64+
'input-button-group.R'
6565
'layout.R'
6666
'nav-items.R'
6767
'nav-update.R'

NAMESPACE

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export(font_collection)
6161
export(font_face)
6262
export(font_google)
6363
export(font_link)
64-
export(input_check_search)
64+
export(input_check_buttons)
65+
export(input_radio_buttons)
6566
export(is.card_item)
6667
export(is_bs_theme)
6768
export(layout_column_wrap)
@@ -95,7 +96,8 @@ export(showcase_left_center)
9596
export(showcase_top_right)
9697
export(theme_bootswatch)
9798
export(theme_version)
98-
export(update_check_search)
99+
export(update_check_buttons)
100+
export(update_radio_buttons)
99101
export(value_box)
100102
export(version_default)
101103
export(versions)

R/input-button-group.R

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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 = "checkbox", 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+
67+
if (!is.null(choices)) {
68+
choices <- processDeps(
69+
input_buttons_container(type = "radio", id, choices, selected),
70+
session
71+
)
72+
}
73+
message <- dropNulls(list(
74+
choices = choices,
75+
selected = as.list(selected)
76+
))
77+
session$sendInputMessage(id, message)
78+
}
79+
80+
81+
input_buttons_container <- function(type = c("radio", "checkbox"), id, choices, selected, size = "md", bg = "primary") {
82+
83+
if (is.null(names(choices)) && is.atomic(choices)) {
84+
names(choices) <- choices
85+
}
86+
if (is.null(names(choices))) {
87+
stop("names() must be provided on list() vectors provided to choices")
88+
}
89+
90+
vals <- rlang::names2(choices)
91+
#if (!all(nzchar(vals))) {
92+
# stop("Input values must be non-empty character strings")
93+
#}
94+
95+
is_checked <- vapply(vals, function(x) isTRUE(x %in% selected) || identical(I("all"), selected), logical(1))
96+
97+
if (!any(is_checked) && !identical(selected, I("none"))) {
98+
is_checked[1] <- TRUE
99+
}
100+
101+
type <- match.arg(type)
102+
if (type == "radio" && sum(is_checked) > 1) {
103+
stop("input_radio_buttons() doesn't support more than one selected choice (do you want input_check_buttons() instead?)", call. = FALSE)
104+
}
105+
106+
inputs <- Map(
107+
vals, choices, is_checked, paste0(id, "-", seq_along(is_checked)),
108+
f = function(val, lbl, checked, this_id) {
109+
list(
110+
tags$input(
111+
type = type, class = "btn-check", name = id,
112+
id = this_id, autocomplete = "off",
113+
`data-value` = val,
114+
checked = if (checked) NA
115+
),
116+
tags$label(
117+
class = paste0("btn btn-outline-", bg),
118+
`for` = this_id, lbl
119+
)
120+
)
121+
}
122+
)
123+
124+
inputs <- unlist(inputs, recursive = FALSE, use.names = FALSE)
125+
}
126+
127+
toggle_dependency <- function() {
128+
htmltools::htmlDependency(
129+
"bslib-toggle-buttons",
130+
version = get_package_version("bslib"),
131+
package = "bslib",
132+
src = "components",
133+
script = "toggle-buttons.js"
134+
)
135+
}

R/input-check-search.R

Lines changed: 0 additions & 109 deletions
This file was deleted.

inst/components/input_check_search.js

Lines changed: 0 additions & 106 deletions
This file was deleted.

0 commit comments

Comments
 (0)