Last active
July 4, 2016 02:18
-
-
Save arackaf/20dbeffd0463ad8f2d0f2605a1d84f9a to your computer and use it in GitHub Desktop.
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 React from 'react'; | |
import 'jscolor'; | |
let uniqueIdCounter = 0; | |
class CustomColorPicker extends React.Component { | |
constructor(){ | |
super(); | |
this.uniqueId = `customColorPickerId${++uniqueIdCounter}` | |
} | |
get valueElementId(){ return `${this.uniqueId}_value` } | |
get styleElementId(){ return `${this.uniqueId}_style` } | |
componentDidMount(){ | |
let onColorChosen = this.props.onColorChosen; | |
this._colorChosen = function(){ | |
let hexColor = this.rgb.map(n => (~~n).toString(16)).map(n => n.length == 1 ? `0${n}` : n).join(''); | |
onColorChosen('#' + hexColor) | |
} | |
this.jscolorInstance = new jscolor(this.rootElement, { valueElement: this.valueElementId, styleElement: this.styleElementId, onFineChange: this._colorChosen }); | |
} | |
shouldComponentUpdate(props){ | |
this.jscolorInstance.fromString(props.currentColor); | |
return false; | |
} | |
render(){ | |
return ( | |
<div> | |
<a id={`${this.uniqueId}`} ref={ el => this.rootElement = el } style={{ width: '80px', height: '20px' }}>Custom</a> | |
<input style={{ display: 'none' }} ref={el => this.valueElement = el} id={this.valueElementId} defaultValue={this.props.currentColor} /> | |
<input style={{ display: 'none' }} id={this.styleElementId} /> | |
</div> | |
); | |
} | |
} | |
export default CustomColorPicker; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yep - it's an old-school JS library that exposes a global variable. If you really hate it, I imagine there'd be nothing stopping you from taking the library and piping it through some sort of UMD generator. Or, now that I think about it, I imagine adding a single line to the end of the file of
module.exports = jscolor
would be enough.