From 24ed83749f683508d94d9dfa3c80a93f06ed5697 Mon Sep 17 00:00:00 2001 From: Tymoteusz Makowski Date: Tue, 19 Mar 2024 23:35:42 +0100 Subject: [PATCH] fix!: clear single-selection dropdown on `character(0)` and `""` Prior to this change only multi-selection dropdown could be cleared. This change makes `update_dropdown_input` behavior analogous to `shiny::updateSelectInput`. Note: this is a breaking change. Prior to this commit updating with `character(0)` or `""` did not have any effect. --- R/dropdown.R | 3 ++- inst/www/shiny-semantic-dropdown.js | 4 ++++ man/update_dropdown_input.Rd | 3 ++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/R/dropdown.R b/R/dropdown.R index d04e23ea..4bb88156 100644 --- a/R/dropdown.R +++ b/R/dropdown.R @@ -149,7 +149,8 @@ selectInput <- function(inputId, label, choices, selected = NULL, multiple = FAL #' @param input_id The id of the input object #' @param choices All available options one can select from. If no need to update then leave as \code{NULL} #' @param choices_value What reactive value should be used for corresponding choice. -#' @param value The initially selected value. +#' @param value A value to update dropdown to. \code{character(0)} and \code{""} clear the dropdown. +#' \code{NULL} (the default) does not change selection. #' #' @examples #' if (interactive()) { diff --git a/inst/www/shiny-semantic-dropdown.js b/inst/www/shiny-semantic-dropdown.js index b32988aa..6ea29780 100644 --- a/inst/www/shiny-semantic-dropdown.js +++ b/inst/www/shiny-semantic-dropdown.js @@ -32,6 +32,10 @@ $.extend(semanticDropdownBinding, { // Given the DOM element for the input, set the value. setValue: function(el, value) { + if (value === '') { + $(el).dropdown('clear'); + return; + } if ($(el).hasClass('multiple')) { $(el).dropdown('clear', true); value.split(",").map(v => $(el).dropdown('set selected', v)); diff --git a/man/update_dropdown_input.Rd b/man/update_dropdown_input.Rd index 17ed4c83..4bf5fee4 100644 --- a/man/update_dropdown_input.Rd +++ b/man/update_dropdown_input.Rd @@ -21,7 +21,8 @@ update_dropdown_input( \item{choices_value}{What reactive value should be used for corresponding choice.} -\item{value}{The initially selected value.} +\item{value}{A value to update dropdown to. \code{character(0)} and \code{""} clear the dropdown. +\code{NULL} (the default) does not change selection.} } \description{ Change the value of a \code{\link{dropdown_input}} input on the client.