Skip to content

Commit

Permalink
added currencyInput() (minimal working version)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Aug 3, 2020
1 parent f93bfdf commit 66c9a9c
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
10 changes: 9 additions & 1 deletion R/dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,12 @@ html_dependency_dollarinput <- function() {
)
}


html_dependency_autonumeric <- function() {
htmlDependency(
name = "autonumeric",
version = "4.6.0",
package = "shinyWidgets",
src = c(href = "shinyWidgets/autonumeric", file = "assets/autonumeric"),
script = c("autoNumeric.min.js", "autonumeric-bindings.js")
)
}
29 changes: 29 additions & 0 deletions R/input-autonumeric.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

currencyInput <- function(inputId, label, value, format = "euro", width = NULL) {
value <- shiny::restoreInput(inputId, value)
tags$div(
class = "form-group shiny-input-container",
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
if (!is.null(label)) tags$label(`for` = inputId, label),
tags$input(
type = "text",
style = "text-align: center; font-size: 1.5rem;",
value = value,
id = inputId,
class = "form-control currency-input"
),
tags$script(
type = "application/json",
`data-for` = inputId,
jsonlite::toJSON(
x = list(
format = format
),
auto_unbox = TRUE,
json_verbatim = TRUE
)
),
html_dependency_autonumeric()
)
}

7 changes: 7 additions & 0 deletions inst/assets/autonumeric/autoNumeric.min.js

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions inst/assets/autonumeric/autonumeric-bindings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

var currencyInputBinding = new Shiny.InputBinding();
$.extend(currencyInputBinding, {

find: function(scope) {
return $(scope).find(".currency-input");
},

initialize: function(el) {
var config = $(el)
.parent()
.find('script[data-for="' + el.id + '"]');
config = JSON.parse(config.html());
this[el.id] = new AutoNumeric(el, config.format);
},

getValue: function(el) {
console.log(this[el.id].getNumber());
return this[el.id].getNumber();
},

subscribe: function(el, callback) {
$(el).on("change", function(event) {
callback();
});
},

unsubscribe: function(el) {
$(el).off(".currencyInputBinding");
},

receiveMessage: function(el, data) {

},

getState: function(el) {

},

getRatePolicy: function() {
return {
policy: "debounce",
delay: 500
};
}
});

Shiny.inputBindings.register(currencyInputBinding, "shinyWidgets.currencyInput");

0 comments on commit 66c9a9c

Please sign in to comment.