From 097d901191cb4f928d972b162bea3f4eebfbd48c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Tue, 23 Aug 2016 20:42:33 +0100 Subject: [PATCH] Updates can clear date and date range inputs --- NEWS | 3 +++ R/update-input.R | 6 +++--- man/updateDateInput.Rd | 2 +- man/updateDateRangeInput.Rd | 4 ++-- srcjs/input_binding_date.js | 6 ++++++ srcjs/input_binding_daterange.js | 17 +++++++++++++---- 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/NEWS b/NEWS index a269892bc..863358d32 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,9 @@ shiny 0.13.2.9005 -------------------------------------------------------------------------------- * Added support for bookmarkable state. +* `updateDateInput()` and `updateDateRangeInput()` can now clear the date + input fields. (#1299, #896) + * Added support for the `pool` package (use Shiny's timer/scheduler) * `Display: Showcase` now displays the .js, .html and .css files in the `www` diff --git a/R/update-input.R b/R/update-input.R index 9b153d069..ea42f3f21 100644 --- a/R/update-input.R +++ b/R/update-input.R @@ -166,7 +166,7 @@ updateActionButton <- function(session, inputId, label = NULL, icon = NULL) { #' #' @template update-input #' @param value The desired date value. Either a Date object, or a string in -#' \code{yyyy-mm-dd} format. +#' \code{yyyy-mm-dd} format. Supply \code{NA} to clear the date. #' @param min The minimum allowed date. Either a Date object, or a string in #' \code{yyyy-mm-dd} format. #' @param max The maximum allowed date. Either a Date object, or a string in @@ -226,9 +226,9 @@ updateDateInput <- function(session, inputId, label = NULL, value = NULL, #' #' @template update-input #' @param start The start date. Either a Date object, or a string in -#' \code{yyyy-mm-dd} format. +#' \code{yyyy-mm-dd} format. Supplying \code{NA} clears the start date. #' @param end The end date. Either a Date object, or a string in -#' \code{yyyy-mm-dd} format. +#' \code{yyyy-mm-dd} format. Supplying \code{NA} clears the end date. #' @param min The minimum allowed date. Either a Date object, or a string in #' \code{yyyy-mm-dd} format. #' @param max The maximum allowed date. Either a Date object, or a string in diff --git a/man/updateDateInput.Rd b/man/updateDateInput.Rd index 436c9bd23..b8717f4a0 100644 --- a/man/updateDateInput.Rd +++ b/man/updateDateInput.Rd @@ -16,7 +16,7 @@ updateDateInput(session, inputId, label = NULL, value = NULL, min = NULL, \item{label}{The label to set for the input object.} \item{value}{The desired date value. Either a Date object, or a string in -\code{yyyy-mm-dd} format.} +\code{yyyy-mm-dd} format. Supply \code{NA} to clear the date.} \item{min}{The minimum allowed date. Either a Date object, or a string in \code{yyyy-mm-dd} format.} diff --git a/man/updateDateRangeInput.Rd b/man/updateDateRangeInput.Rd index 0560d51de..d39723ddd 100644 --- a/man/updateDateRangeInput.Rd +++ b/man/updateDateRangeInput.Rd @@ -16,10 +16,10 @@ updateDateRangeInput(session, inputId, label = NULL, start = NULL, \item{label}{The label to set for the input object.} \item{start}{The start date. Either a Date object, or a string in -\code{yyyy-mm-dd} format.} +\code{yyyy-mm-dd} format. Supplying \code{NA} clears the start date.} \item{end}{The end date. Either a Date object, or a string in -\code{yyyy-mm-dd} format.} +\code{yyyy-mm-dd} format. Supplying \code{NA} clears the end date.} \item{min}{The minimum allowed date. Either a Date object, or a string in \code{yyyy-mm-dd} format.} diff --git a/srcjs/input_binding_date.js b/srcjs/input_binding_date.js index d731d6510..edb1228b4 100644 --- a/srcjs/input_binding_date.js +++ b/srcjs/input_binding_date.js @@ -14,6 +14,12 @@ $.extend(dateInputBinding, { }, // value must be an unambiguous string like '2001-01-01', or a Date object. setValue: function(el, value) { + // R's NA, which is null here will remove current value + if (value === null) { + $(el).find('input').val('').datepicker('update'); + return; + } + var date = this._newDate(value); // If date is invalid, do nothing if (isNaN(date)) diff --git a/srcjs/input_binding_daterange.js b/srcjs/input_binding_daterange.js index 8d8e41fe9..317247cd3 100644 --- a/srcjs/input_binding_daterange.js +++ b/srcjs/input_binding_daterange.js @@ -23,13 +23,22 @@ $.extend(dateRangeInputBinding, dateInputBinding, { var $inputs = $(el).find('input'); // If value is undefined, don't try to set + // null will remove the current value if (value[0] !== undefined) { - var start = this._newDate(value[0]); - $inputs.eq(0).datepicker('update', start); + if (value[0] === null) { + $inputs.eq(0).val('').datepicker('update'); + } else { + var start = this._newDate(value[0]); + $inputs.eq(0).datepicker('update', start); + } } if (value[1] !== undefined) { - var end = this._newDate(value[1]); - $inputs.eq(1).datepicker('update', end); + if (value[1] === null) { + $inputs.eq(1).val('').datepicker('update'); + } else { + var end = this._newDate(value[1]); + $inputs.eq(1).datepicker('update', end); + } } }, getState: function(el) {