Skip to content

Instantly share code, notes, and snippets.

@alandipert
Last active February 22, 2019 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alandipert/44e6f9e670f45a77b3b64f21952af176 to your computer and use it in GitHub Desktop.
Save alandipert/44e6f9e670f45a77b3b64f21952af176 to your computer and use it in GitHub Desktop.
Example of reactR input helpers
import * as pickers from 'react-color';
import { reactInput } from 'reactR';
const ColorPicker = ({ configuration, value, setValue }) => {
const picker = pickers[configuration.type];
return React.createElement(picker, {
color: value,
onChangeComplete: color => setValue(color.hex)
});
};
reactInput('.colorpicker', 'reactR.ColorPicker', ColorPicker);
picker_types <- c(
"sketch",
"alpha",
"block",
"chrome",
"circle",
"compact",
"github",
"hue",
"material",
"photoshop",
"slider",
"swatches",
"twitter"
)
capitalize <- function(s) {
gsub("^(.)", perl = TRUE, replacement = '\\U\\1', s)
}
#' Color picker input.
#'
#' @param inputId
#' @param defaultColor
#' @param type
#'
#' @return
#' @export
#'
#' @examples
colorpickerInput <- function(inputId, defaultColor = "#fff", type = picker_types) {
color <- restoreInput(id = inputId, default = defaultColor)
type <- paste0(capitalize(match.arg(type)), "Picker")
reactR::createReactInput(
inputId,
"colorpicker",
htmltools::htmlDependency(
name = "react-color-input",
version = "1.0.0",
src = "www/colorpicker",
package = "colorpicker",
script = "colorpicker.js"
),
color,
list(type = type),
htmltools::tags$div
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment