2016-09-12 19:47:28 +02:00
|
|
|
import React from 'react'
|
2016-12-19 11:46:48 +01:00
|
|
|
import Color from 'color'
|
2016-09-13 20:30:03 +02:00
|
|
|
import inputStyle from './input.js'
|
2016-12-17 22:19:03 +01:00
|
|
|
import ChromePicker from 'react-color/lib/components/chrome/Chrome'
|
2016-12-17 21:25:00 +01:00
|
|
|
|
|
|
|
function formatColor(color) {
|
2016-12-17 21:52:27 +01:00
|
|
|
const rgb = color.rgb
|
|
|
|
return `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${rgb.a})`
|
2016-12-17 21:25:00 +01:00
|
|
|
}
|
2016-09-12 19:47:28 +02:00
|
|
|
|
|
|
|
/*** Number fields with support for min, max and units and documentation*/
|
|
|
|
class ColorField extends React.Component {
|
2016-12-17 22:28:24 +01:00
|
|
|
static propTypes = {
|
2016-09-12 20:10:49 +02:00
|
|
|
onChange: React.PropTypes.func.isRequired,
|
2016-12-17 21:25:00 +01:00
|
|
|
name: React.PropTypes.string.isRequired,
|
2016-09-13 20:30:03 +02:00
|
|
|
value: React.PropTypes.string,
|
2016-09-12 19:47:28 +02:00
|
|
|
default: React.PropTypes.number,
|
|
|
|
doc: React.PropTypes.string,
|
2016-12-19 16:21:22 +01:00
|
|
|
style: React.PropTypes.object,
|
2016-09-12 19:47:28 +02:00
|
|
|
}
|
|
|
|
|
2016-12-17 22:28:24 +01:00
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = {
|
|
|
|
pickerOpened: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
togglePicker() {
|
|
|
|
this.setState({ pickerOpened: !this.state.pickerOpened })
|
|
|
|
}
|
|
|
|
|
2016-12-17 21:25:00 +01:00
|
|
|
render() {
|
2016-12-17 22:28:24 +01:00
|
|
|
const picker = <div style={{
|
2016-12-17 21:25:00 +01:00
|
|
|
position: 'absolute',
|
2016-12-19 16:30:48 +01:00
|
|
|
left: 163,
|
|
|
|
top: 0,
|
2016-12-17 21:25:00 +01:00
|
|
|
}}>
|
2016-12-17 22:28:24 +01:00
|
|
|
<ChromePicker
|
2016-12-19 11:46:48 +01:00
|
|
|
color={this.props.value ? Color(this.props.value).object() : null}
|
2016-12-17 22:28:24 +01:00
|
|
|
onChange={c => this.props.onChange(formatColor(c))}
|
|
|
|
/>
|
|
|
|
<div
|
|
|
|
onClick={this.togglePicker.bind(this)}
|
|
|
|
style={{
|
|
|
|
zIndex: -1,
|
|
|
|
position: 'fixed',
|
|
|
|
top: '0px',
|
|
|
|
right: '0px',
|
|
|
|
bottom: '0px',
|
|
|
|
left: '0px',
|
|
|
|
}} />
|
|
|
|
</div>
|
2016-09-12 20:29:53 +02:00
|
|
|
|
2016-12-19 16:30:48 +01:00
|
|
|
return <div style={{
|
|
|
|
...inputStyle.property,
|
|
|
|
position: 'relative',
|
|
|
|
display: 'inline',
|
|
|
|
}}>
|
2016-12-17 22:28:24 +01:00
|
|
|
{this.state.pickerOpened && picker}
|
2016-12-17 21:25:00 +01:00
|
|
|
<input
|
2016-12-17 22:28:24 +01:00
|
|
|
onClick={this.togglePicker.bind(this)}
|
2016-12-19 16:21:22 +01:00
|
|
|
style={{
|
|
|
|
...inputStyle.select,
|
|
|
|
...this.props.style
|
|
|
|
}}
|
2016-12-17 21:25:00 +01:00
|
|
|
name={this.props.name}
|
|
|
|
placeholder={this.props.default}
|
|
|
|
value={this.props.value ? this.props.value : ""}
|
|
|
|
onChange={(e) => this.props.onChange(e.target.value)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
}
|
2016-09-12 19:47:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ColorField
|