Skip to content

Commit

Permalink
updateNumericInputIcon: update icons
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Jun 26, 2020
1 parent 4c163dc commit 05a1272
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ importFrom(grDevices,col2rgb)
importFrom(grDevices,rgb2hsv)
importFrom(htmltools,HTML)
importFrom(htmltools,attachDependencies)
importFrom(htmltools,doRenderTags)
importFrom(htmltools,findDependencies)
importFrom(htmltools,htmlDependency)
importFrom(htmltools,htmlEscape)
Expand Down Expand Up @@ -151,7 +152,6 @@ importFrom(shiny,shinyAppDir)
importFrom(shiny,shinyAppFile)
importFrom(shiny,singleton)
importFrom(shiny,sliderInput)
importFrom(shiny,updateNumericInput)
importFrom(shiny,updateSelectizeInput)
importFrom(shiny,updateTextInput)
importFrom(stats,aggregate)
Expand Down
29 changes: 27 additions & 2 deletions R/input-icon.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ numericInputIcon <- function(inputId,
#' @title Change the value of a numeric input icon on the client
#'
#' @inheritParams shiny::updateNumericInput
#' @param icon Icon to update, note that you can update icon only
#' if initialized in \code{\link{numericInputIcon}}.
#'
#' @return No value.
#' @export
#'
#' @importFrom shiny updateNumericInput
#' @importFrom htmltools doRenderTags
#'
#' @examples
#' library(shiny)
Expand Down Expand Up @@ -177,7 +179,30 @@ numericInputIcon <- function(inputId,
#'
#' if (interactive())
#' shinyApp(ui, server)
updateNumericInputIcon <- shiny::updateNumericInput
updateNumericInputIcon <- function(session,
inputId,
label = NULL,
value = NULL,
min = NULL,
max = NULL,
step = NULL,
icon = NULL) {
addons <- validate_addon(icon)
if (!is.null(addons$right))
addons$right <- htmltools::doRenderTags(addons$right)
if (!is.null(addons$left))
addons$left <- htmltools::doRenderTags(addons$left)
message <- dropNulls(list(
label = label,
value = formatNoSci(value),
min = formatNoSci(min),
max = formatNoSci(max),
step = formatNoSci(step),
right = addons$right,
left = addons$left
))
session$sendInputMessage(inputId, message)
}



Expand Down
22 changes: 14 additions & 8 deletions inst/assets/numeric/numeric-icon-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function hideHelp(element) {
}
var numericInputIconBinding = new Shiny.InputBinding();
$.extend(numericInputIconBinding, {
find: function find(scope) {
find: function(scope) {
return $(scope).find(".shinywidgets-numeric");
},
getValue: function getValue(el) {
Expand Down Expand Up @@ -85,10 +85,10 @@ $.extend(numericInputIconBinding, {
}
return numberVal;
},
setValue: function setValue(el, value) {
setValue: function(el, value) {
el.value = value;
},
subscribe: function subscribe(el, callback) {
subscribe: function(el, callback) {
$(el).on(
"keyup.numericInputIconBinding input.numericInputIconBinding",
function(event) {
Expand All @@ -99,21 +99,27 @@ $.extend(numericInputIconBinding, {
callback(false);
});
},
unsubscribe: function unsubscribe(el) {
unsubscribe: function(el) {
$(el).off(".textInputBinding");
},
getType: function getType(el) {
getType: function(el) {
return "shiny.number";
},
receiveMessage: function receiveMessage(el, data) {
receiveMessage: function(el, data) {
if (data.hasOwnProperty("value")) el.value = data.value;
if (data.hasOwnProperty("min")) el.min = data.min;
if (data.hasOwnProperty("max")) el.max = data.max;
if (data.hasOwnProperty("step")) el.step = data.step;
updateLabel(data.label, this._getLabelNode(el));
if (data.hasOwnProperty("left")) {
$(el).prev(".input-group-addon").replaceWith(data.left);
}
if (data.hasOwnProperty("right")) {
$(el).next(".input-group-addon").replaceWith(data.right);
}
$(el).trigger("change");
},
getState: function getState(el) {
getState: function(el) {
return {
label: this._getLabelNode(el).text(),
value: this.getValue(el),
Expand All @@ -122,7 +128,7 @@ $.extend(numericInputIconBinding, {
step: Number(el.step)
};
},
_getLabelNode: function _getLabelNode(el) {
_getLabelNode: function(el) {
return $(el)
.parent()
.find('label[for="' + Shiny.$escape(el.id) + '"]');
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/updateNumericInputIcon.Rd

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

0 comments on commit 05a1272

Please sign in to comment.