-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathinput-button-group.R
169 lines (146 loc) · 3.9 KB
/
input-button-group.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#' Create a button group of radio/check boxes
#'
#' Use `input_check_buttons()` if multiple choices may be selected at once; otherwise, use `input_radio_buttons()`
#'
#' @param theme a theme color.
#' @export
input_check_buttons <- function(
id,
choices,
...,
selected = NULL,
gap = 0,
theme = NULL
) {
input_tags <- toggle_button_tags(
type = "checkbox", id = id, choices = choices,
selected = selected, theme = theme
)
res <- toggle_button_container(
id = id,
input_tags = input_tags,
gap = gap,
...
)
as_fragment(
tag_require(res, version = 5, caller = "input_check_buttons()")
)
}
#' @export
#' @rdname input_check_buttons
input_radio_buttons <- function(
id,
choices,
...,
selected = NULL,
gap = 0,
theme = NULL
) {
input_tags <- toggle_button_tags(
type = "radio", id = id, choices = choices,
selected = selected, theme = theme
)
res <- toggle_button_container(
id = id,
input_tags = input_tags,
gap = gap,
...
)
as_fragment(
tag_require(res, version = 5, caller = "input_radio_buttons()")
)
}
#' @export
#' @rdname input_check_buttons
update_check_buttons <- function(id, choices = NULL, selected = NULL, session = get_current_session()) {
if (!is.null(choices)) {
choices <- processDeps(
toggle_button_tags(type = "checkbox", id, choices, selected),
session
)
}
message <- dropNulls(list(
choices = choices,
selected = as.list(selected)
))
session$sendInputMessage(id, message)
}
#' @export
#' @rdname input_check_buttons
update_radio_buttons <- function(id, choices = NULL, selected = NULL, session = get_current_session()) {
if (!is.null(choices)) {
choices <- processDeps(
toggle_button_tags(type = "radio", id, choices, selected),
session
)
}
message <- dropNulls(list(
choices = choices,
selected = as.list(selected)
))
session$sendInputMessage(id, message)
}
# TODO: container should have an aria-label!
toggle_button_container <- function(id, input_tags, gap = 0, ...) {
has_gap <- !identical(gap, 0)
div(
id = id,
class = "bslib-toggle-buttons bslib-mb-spacing",
class = if (!has_gap) "btn-group",
style = css(
display = "flex",
gap = validateCssUnit(gap),
flexWrap = if (has_gap) "wrap"
),
role = "group",
...,
!!!input_tags,
toggle_dependency()
)
}
toggle_button_tags <- function(type = c("radio", "checkbox"), id, choices, selected, theme = NULL) {
if (is.null(names(choices)) && is.atomic(choices)) {
names(choices) <- choices
}
if (is.null(names(choices))) {
stop("names() must be provided on list() vectors provided to choices")
}
vals <- rlang::names2(choices)
#if (!all(nzchar(vals))) {
# stop("Input values must be non-empty character strings")
#}
is_checked <- vapply(vals, function(x) isTRUE(x %in% selected) || identical(I("all"), selected), logical(1))
if (!any(is_checked) && !identical(selected, I("none"))) {
is_checked[1] <- TRUE
}
type <- match.arg(type)
if (type == "radio" && sum(is_checked) > 1) {
stop("input_radio_buttons() doesn't support more than one selected choice (do you want input_check_buttons() instead?)", call. = FALSE)
}
unname(Map(
vals, choices, is_checked, paste0(id, "-", seq_along(is_checked)),
f = function(val, lbl, checked, this_id) {
list(
tags$input(
type = type, class = "btn-check", name = id,
id = this_id, autocomplete = "off",
`data-value` = val,
checked = if (checked) NA
),
tags$label(
class = paste0("btn btn-outline-", theme %||% "secondary"),
`for` = this_id, lbl
)
)
}
))
}
toggle_dependency <- function() {
htmltools::htmlDependency(
"bslib-toggle-buttons",
version = get_package_version("bslib"),
package = "bslib",
src = "components",
script = "toggle-buttons.js"
)
}