-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add input_check_buttons() and input_radio_buttons() #485
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#' 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()` | ||
#' | ||
#' @inheritParams input_check_search | ||
#' @param size size of the button group | ||
#' @param bg a theme color to use for the btn modifier class | ||
#' @export | ||
input_check_buttons <- function(id, choices, ..., selected = NULL, size = c("md", "sm", "lg"), bg = "primary") { | ||
size <- match.arg(size) | ||
tag <- div( | ||
id = id, | ||
class = "btn-group bslib-toggle-buttons", | ||
class = if (size != "md") paste0("btn-group-", size), | ||
role = "group", | ||
..., | ||
!!!input_buttons_container( | ||
type = "checkbox", id = id, choices = choices, selected = selected, | ||
size = size, bg = bg | ||
), | ||
toggle_dependency() | ||
) | ||
tag <- tag_require(tag, version = 5, caller = "input_check_buttons()") | ||
as_fragment(tag) | ||
} | ||
|
||
#' @export | ||
#' @rdname input_check_buttons | ||
update_check_buttons <- function(id, choices = NULL, selected = NULL, session = get_current_session()) { | ||
if (!is.null(choices)) { | ||
choices <- processDeps( | ||
input_buttons_container(type = "checkbox", id, choices, selected), | ||
session | ||
) | ||
} | ||
message <- dropNulls(list( | ||
choices = choices, | ||
selected = as.list(selected) | ||
)) | ||
session$sendInputMessage(id, message) | ||
} | ||
|
||
#' @export | ||
#' @rdname input_check_buttons | ||
input_radio_buttons <- function(id, choices, ..., selected = NULL, size = c("md", "sm", "lg"), bg = "primary") { | ||
size <- match.arg(size) | ||
tag <- div( | ||
id = id, | ||
class = "btn-group bslib-toggle-buttons", | ||
class = if (size != "md") paste0("btn-group-", size), | ||
role = "group", | ||
..., | ||
!!!input_buttons_container( | ||
type = "checkbox", id = id, choices = choices, selected = selected, | ||
size = size, bg = bg | ||
), | ||
toggle_dependency() | ||
) | ||
tag <- tag_require(tag, version = 5, caller = "input_radio_buttons()") | ||
as_fragment(tag) | ||
} | ||
|
||
#' @export | ||
#' @rdname input_check_buttons | ||
update_radio_buttons <- function(id, choices = NULL, selected = NULL, session = get_current_session()) { | ||
|
||
if (!is.null(choices)) { | ||
choices <- processDeps( | ||
input_buttons_container(type = "radio", id, choices, selected), | ||
session | ||
) | ||
} | ||
message <- dropNulls(list( | ||
choices = choices, | ||
selected = as.list(selected) | ||
)) | ||
session$sendInputMessage(id, message) | ||
} | ||
|
||
|
||
input_buttons_container <- function(type = c("radio", "checkbox"), id, choices, selected, size = "md", bg = "primary") { | ||
|
||
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) | ||
} | ||
|
||
inputs <- 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-", bg), | ||
`for` = this_id, lbl | ||
) | ||
) | ||
} | ||
) | ||
|
||
inputs <- unlist(inputs, recursive = FALSE, use.names = FALSE) | ||
} | ||
|
||
toggle_dependency <- function() { | ||
htmltools::htmlDependency( | ||
"bslib-toggle-buttons", | ||
version = get_package_version("bslib"), | ||
package = "bslib", | ||
src = "components", | ||
script = "toggle-buttons.js" | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
var toggleButtonsInputBinding = new Shiny.InputBinding(); | ||
$.extend(toggleButtonsInputBinding, { | ||
|
||
find: function(scope) { | ||
return $(scope).find(".btn-group.bslib-toggle-buttons"); | ||
}, | ||
|
||
getValue: function(el) { | ||
var inputs = $(el).find("input.btn-check"); | ||
var vals = []; | ||
inputs.each(function(i) { | ||
if (this.checked) { | ||
vals.push($(this).attr("data-value")); | ||
} | ||
}); | ||
return vals.length > 0 ? vals : null; | ||
}, | ||
|
||
subscribe: function(el, callback) { | ||
$(el).on( | ||
'change.toggleButtonsInputBinding', | ||
function(event) { callback(true); } | ||
); | ||
}, | ||
|
||
unsubscribe: function(el) { | ||
$(el).off(".toggleButtonsInputBinding"); | ||
}, | ||
|
||
receiveMessage: function(el, data) { | ||
if (data.hasOwnProperty("choices")) { | ||
Shiny.renderContent(el, data.choices); | ||
} else if (data.hasOwnProperty("selected")) { | ||
const inputs = $(el).find("input"); | ||
inputs.each(function(i) { | ||
const val = $(this).attr("data-value"); | ||
const checked = data.selected.indexOf(val) > -1; | ||
this.checked = checked; | ||
}); | ||
} | ||
|
||
$(el).trigger("change.toggleButtonsInputBinding"); | ||
} | ||
|
||
}); | ||
|
||
Shiny.inputBindings.register(toggleButtonsInputBinding); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy over the relevant params from #484