Skip to content

Commit

Permalink
null value in autonumericInput()
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed May 4, 2021
1 parent 874a04c commit 66a507b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
5 changes: 4 additions & 1 deletion R/input-autonumeric.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ updateFormatNumericInput <- function(session, inputId,
#' @param modifyValueOnWheel Allows the user to increment or decrement the
#' element value with the mouse wheel. The wheel behavior can be modified
#' by the \code{wheelStep} option. Defaults to TRUE.
#' @param emptyInputBehavior Defines what should be displayed in the element if the raw value is an empty string ''.
#' @param ... Additional parameters that can be passed to AutoNumeric. See
#' details for more information.
#'
Expand Down Expand Up @@ -462,6 +463,7 @@ autonumericInput <- function(inputId, label, value,
maximumValue = NULL,
minimumValue = NULL,
modifyValueOnWheel = NULL,
emptyInputBehavior = "null",
...) {
value <- shiny::restoreInput(inputId, value)

Expand All @@ -485,7 +487,8 @@ autonumericInput <- function(inputId, label, value,
formatOnPageLoad = formatOnPageLoad,
maximumValue = maximumValue,
minimumValue = minimumValue,
modifyValueOnWheel = modifyValueOnWheel
modifyValueOnWheel = modifyValueOnWheel,
emptyInputBehavior = emptyInputBehavior
),
list(...)
)
Expand Down
33 changes: 25 additions & 8 deletions inst/assets/autonumeric/autonumeric-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
/*global AutoNumeric, Shiny */

// Autonumeric Input Binding
Shiny.InputBinding.prototype.store = [];
Shiny.InputBinding.prototype.updateStore = function(el, instance) {
this.store[el.id] = instance;
};
var autonumericInputBinding = new Shiny.InputBinding();
$.extend(autonumericInputBinding, {

Expand All @@ -12,19 +16,27 @@ $.extend(autonumericInputBinding, {
},

initialize: function(el) {
var autonum, emptyInputBehavior;
var config = $(el)
.parent()
.find('script[data-for="' + el.id + '"]');
config = JSON.parse(config.html());
if (config.hasOwnProperty("format")) {
this[el.id] = new AutoNumeric(el, config.format);
autonum = new AutoNumeric(el, config.format);
emptyInputBehavior = config.format.emptyInputBehavior;
} else {
this[el.id] = new AutoNumeric(el, config.options);
autonum = new AutoNumeric(el, config.options);
emptyInputBehavior = config.options.emptyInputBehavior;
}
if ($(el).val() === "" & emptyInputBehavior === "null") {
autonum.set(null);
}
this.updateStore(el, autonum);
},

getValue: function(el) {
return this[el.id].getNumber();
var autonum = this.getAutonumeric(el);
return autonum.getNumber();
},

subscribe: function(el, callback) {
Expand All @@ -38,16 +50,17 @@ $.extend(autonumericInputBinding, {
},

receiveMessage: function(el, data) {
var autonum = this.getAutonumeric(el);
if (data.hasOwnProperty("value")) {
this[el.id].set(data.value);
autonum.set(data.value);
}

if (data.hasOwnProperty("format")) {
this[el.id].update(data.format);
autonum.update(data.format);
}

if (data.hasOwnProperty("options")) {
this[el.id].update(data.options);
autonum.update(data.options);
}

if (data.hasOwnProperty("label"))
Expand All @@ -61,14 +74,15 @@ $.extend(autonumericInputBinding, {
},

getState: function(el) {
var autonum = this.getAutonumeric(el);
return{
label: $(el)
.parent()
.parent()
.find('label[for="' + Shiny.$escape(el.id) + '"]')
.text(),
value: this[el.id].getNumber(),
options: this[el.id].getSettings()
value: autonum.getNumber(),
options: autonum.getSettings()
};
},

Expand All @@ -77,6 +91,9 @@ $.extend(autonumericInputBinding, {
policy: "debounce",
delay: 500
};
},
getAutonumeric: function(el) {
return this.store[el.id];
}
});

Expand Down
2 changes: 1 addition & 1 deletion inst/assets/shinyWidgets-bindings.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions man/autonumericInput.Rd

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

0 comments on commit 66a507b

Please sign in to comment.