Last active
February 22, 2019 15:20
-
-
Save alandipert/44e6f9e670f45a77b3b64f21952af176 to your computer and use it in GitHub Desktop.
Example of reactR input helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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