|
| 1 | +# A robust name string is a valid |
| 2 | +# - CSS class |
| 3 | +# - JavaScript variable name |
| 4 | +# - R variable name |
| 5 | +robustName <- "^[[:alpha:]_][[:alnum:]_]*$" |
| 6 | + |
1 | 7 | isUpper <- function(s) { |
2 | 8 | grepl("^[[:upper:]]+$", s) |
3 | 9 | } |
@@ -92,3 +98,57 @@ reactMarkup <- function(tag) { |
92 | 98 | list(tag = tag, class = "reactR_markup") |
93 | 99 | } |
94 | 100 |
|
| 101 | +#' Create a React-based input |
| 102 | +#' |
| 103 | +#' @param inputId The \code{input} slot that will be used to access the value. |
| 104 | +#' @param class Space-delimited list of CSS class names that should identify |
| 105 | +#' this input type in the browser. |
| 106 | +#' @param dependencies HTML dependencies to include in addition to those |
| 107 | +#' supporting React. Must contain at least one dependency, that of the input's |
| 108 | +#' implementation. |
| 109 | +#' @param default Initial value. |
| 110 | +#' @param configuration Static configuration data. |
| 111 | +#' @param container Function to generate an HTML element to contain the input. |
| 112 | +#' |
| 113 | +#' @return Shiny input suitable for inclusion in a UI. |
| 114 | +#' @export |
| 115 | +#' |
| 116 | +#' @examples |
| 117 | +#' myInput <- function(inputId, default = "") { |
| 118 | +#' # The value of createReactShinyInput should be returned from input constructor functions. |
| 119 | +#' createReactShinyInput( |
| 120 | +#' inputId, |
| 121 | +#' "myinput", |
| 122 | +#' # At least one htmlDependency must be provided -- the JavaScript implementation of the input. |
| 123 | +#' htmlDependency( |
| 124 | +#' name = "my-input", |
| 125 | +#' version = "1.0.0", |
| 126 | +#' src = "www/mypackage/myinput", |
| 127 | +#' package = "mypackage", |
| 128 | +#' script = "myinput.js" |
| 129 | +#' ), |
| 130 | +#' default |
| 131 | +#' ) |
| 132 | +#' } |
| 133 | +createReactShinyInput <- function(inputId, |
| 134 | + class, |
| 135 | + dependencies, |
| 136 | + default = NULL, |
| 137 | + configuration = list(), |
| 138 | + container = htmltools::tags$div) { |
| 139 | + if(length(dependencies) < 1) stop("Must include at least one HTML dependency.") |
| 140 | + value <- shiny::restoreInput(id = inputId, default = default) |
| 141 | + htmltools::tagList( |
| 142 | + html_dependency_corejs(), |
| 143 | + html_dependency_react(), |
| 144 | + html_dependency_reacttools(), |
| 145 | + container(id = inputId, class = class), |
| 146 | + htmltools::tags$script(id = sprintf("%s_value", inputId), |
| 147 | + type = "application/json", |
| 148 | + jsonlite::toJSON(value, auto_unbox = TRUE)), |
| 149 | + htmltools::tags$script(id = sprintf("%s_configuration", inputId), |
| 150 | + type = "application/json", |
| 151 | + jsonlite::toJSON(configuration, auto_unbox = TRUE)), |
| 152 | + dependencies |
| 153 | + ) |
| 154 | +} |
0 commit comments