Skip to content

Commit

Permalink
disable radio and checkbox buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed May 22, 2020
1 parent f33ec7e commit db10dd7
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 21 deletions.
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
shinyWidgets 0.5.3
======================

* Added the ability to disable `radioGroupButtons()` and `checkboxGroupButtons()` via respective update methods.



shinyWidgets 0.5.2
======================

Expand Down
32 changes: 26 additions & 6 deletions R/input-checkboxgroupbuttons.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ generateCBGB <- function(inputId, choices, selected, status, size, checkIcon) {
#' @param size Size, only used if choices is not NULL.
#' @param checkIcon Icon, only used if choices is not NULL.
#' @param choiceNames,choiceValues List of names and values, an alternative to choices.
#' @param disabled Logical, disable or enable buttons,
#' if \code{TRUE} users won't be able to select a value.
#'
#' @export
#'
Expand Down Expand Up @@ -278,15 +280,33 @@ generateCBGB <- function(inputId, choices, selected, status, size, checkIcon) {
#' shinyApp(ui = ui, server = server)
#'
#' }
updateCheckboxGroupButtons <- function(session, inputId, label = NULL, choices = NULL, selected = NULL,
status = "default", size = "normal",
checkIcon = list(), choiceNames = NULL, choiceValues = NULL) {
updateCheckboxGroupButtons <- function(session,
inputId,
label = NULL,
choices = NULL,
selected = NULL,
status = "default",
size = "normal",
checkIcon = list(),
choiceNames = NULL,
choiceValues = NULL,
disabled = FALSE) {
args <- normalizeChoicesArgs(choices, choiceNames, choiceValues, mustExist = FALSE)
options <- if (!is.null(args$choiceNames)) {
format(htmltools::tagList(generateCBGB(session$ns(inputId), args, selected, status = status, size = size,
checkIcon = checkIcon)))
as.character(tagList(generateCBGB(
session$ns(inputId),
args, selected,
status = status,
size = size,
checkIcon = checkIcon
)))
}
message <- dropNulls(list(selected = selected, options = options, label = label))
message <- dropNulls(list(
selected = selected,
options = options,
label = label,
disabled = isTRUE(disabled)
))
session$sendInputMessage(inputId, message)
}

36 changes: 28 additions & 8 deletions R/input-radiogroupbuttons.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ generateRGB <- function(inputId, choices, selected, status, size, checkIcon) {
#' @param size Size, only used if choices is not NULL.
#' @param checkIcon Icon, only used if choices is not NULL.
#' @param choiceNames,choiceValues List of names and values, an alternative to choices.
#' @param disabled Logical, disable or enable buttons,
#' if \code{TRUE} users won't be able to select a value.
#'
#' @export
#'
Expand Down Expand Up @@ -239,20 +241,38 @@ generateRGB <- function(inputId, choices, selected, status, size, checkIcon) {
#' shinyApp(ui = ui, server = server)
#'
#' }
updateRadioGroupButtons <- function(session, inputId, label = NULL, choices = NULL, selected = NULL,
status = "default", size = "normal",
checkIcon = list(), choiceNames = NULL, choiceValues = NULL) {
updateRadioGroupButtons <- function(session,
inputId,
label = NULL,
choices = NULL,
selected = NULL,
status = "default",
size = "normal",
checkIcon = list(),
choiceNames = NULL,
choiceValues = NULL,
disabled = FALSE) {
args <- normalizeChoicesArgs(choices, choiceNames, choiceValues, mustExist = FALSE)
if (is.null(selected) && !is.null(args$choiceValues)) {
selected <- args$choiceValues[[1]]
} else {
selected <- as.character(selected)
}
options <- if (!is.null(args$choiceValues)) {
format(htmltools::tagList(generateRGB(session$ns(inputId), args, selected, status = status, size = size,
checkIcon = checkIcon)))
as.character(tagList(
generateRGB(
session$ns(inputId),
args, as.character(selected),
status = status,
size = size,
checkIcon = checkIcon
)
))
}
message <- dropNulls(list(selected = selected, options = options, label = label))
message <- dropNulls(list(
selected = selected,
options = options,
label = label,
disabled = isTRUE(disabled)
))
session$sendInputMessage(inputId, message)
}

Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ $.extend(checkboxGroupButtonsBinding, {
if (data.hasOwnProperty("label"))
$el.find('label[for="' + Shiny.$escape(el.id) + '"]').text(data.label);

if (data.disabled) {
$el.find("button").attr("disabled", "disabled");
} else {
$el.find("button").removeAttr("disabled");
}

$(el).trigger("change");
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ $.extend(radioGroupButtonsBinding, {
return el.id;
},
getValue: function(el) {
var value = $('input:radio[name="' + Shiny.$escape(el.id) + '"]:checked').val();
var value = $(
'input:radio[name="' + Shiny.$escape(el.id) + '"]:checked'
).val();
return typeof value == "undefined" ? null : value;
},
setValue: function(el, value) {
$('input:radio[name="' + Shiny.$escape(el.id) + '"]')
.parent()
.removeClass("active");
$('input:radio[name="' + Shiny.$escape(el.id) + '"]')
.prop("checked", false);
$('input:radio[name="' + Shiny.$escape(el.id) + '"]').prop(
"checked",
false
);
if (value.length > 0) {
$(
'input:radio[name="' +
Expand Down Expand Up @@ -86,14 +90,22 @@ $.extend(radioGroupButtonsBinding, {
$el.find("div.btn-group-container-sw").append(data.options);
}

if (data.hasOwnProperty("selected")) this.setValue(el, data.selected);
if (data.hasOwnProperty("selected")) {
this.setValue(el, data.selected);
}

if (data.hasOwnProperty("label"))
$(el)
.parent()
.find('label[for="' + Shiny.$escape(el.id) + '"]')
.text(data.label);

if (data.disabled) {
$el.find("button").attr("disabled", "disabled");
} else {
$el.find("button").removeAttr("disabled");
}

$(el).trigger("change");
}
});
Expand Down
2 changes: 1 addition & 1 deletion inst/assets/shinyWidgets-bindings.min.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion man/updateCheckboxGroupButtons.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/updateRadioGroupButtons.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit db10dd7

Please sign in to comment.