diff --git a/R/inputs.R b/R/inputs.R index 9bbbf1c..56d1008 100644 --- a/R/inputs.R +++ b/R/inputs.R @@ -115,6 +115,33 @@ date_extra <- function(id, key = NULL, ...) { ) } +#' Numeric input for reactable column cell +#' +#' @param id id of the input +#' @param key alternative unique id for server side processing +#' @param ... parameters of numeric input, only `class` is supported for now +#' +#' @examples +#' reactable::colDef(cell = number_extra("date", class = "table-number")) +#' +#' @export +number_extra <- function(id, key = NULL, ...) { + key <- define_key(key) + reactable::JS( + htmltools::doRenderTags( + htmltools::htmlTemplate( + text_ = "function(cellInfo) { + return React.createElement(numberExtras, + {id: '{{id}}', value: cellInfo.value, uuid: {{key}}, + column: cellInfo.column.id {{args}}}, cellInfo.id) + }", + id = id, + key = key, + args = args_js(...)) + ) + ) +} + #' Select input for reactable column cell #' #' @param id id of the select input diff --git a/inst/assets/js/reactable-extras.js b/inst/assets/js/reactable-extras.js index 0d51cbc..98404f5 100644 --- a/inst/assets/js/reactable-extras.js +++ b/inst/assets/js/reactable-extras.js @@ -25,6 +25,17 @@ function dateExtras ({ id, value, uuid, column, className, children }) { ) }; +function numberExtras ({ id, value, uuid, column, className, children }) { + const onChange = event => { + Shiny.setInputValue(id, { row: uuid, value: event.target.value, column: column }, { priority: 'event' }) + } + + return React.createElement( + 'input', + { type: 'number', key: uuid, defaultValue: value, onChange, className } + ) +}; + function dropdownExtras ({ id, value, uuid, column, choices, className, children }) { const onChange = event => { Shiny.setInputValue(id, { row: uuid, value: event.target.value, column: column }, { priority: 'event' })