maputnik/src/components/fields/ColorField.jsx

101 lines
2.5 KiB
React
Raw Normal View History

2016-09-12 19:47:28 +02:00
import React from 'react'
2016-12-19 11:46:48 +01:00
import Color from 'color'
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 = {
onChange: React.PropTypes.func.isRequired,
2016-12-17 21:25:00 +01:00
name: React.PropTypes.string.isRequired,
value: React.PropTypes.string,
2016-09-12 19:47:28 +02:00
doc: React.PropTypes.string,
2016-12-19 16:21:22 +01:00
style: React.PropTypes.object,
2016-12-30 17:16:58 +01:00
default: React.PropTypes.string,
2016-09-12 19:47:28 +02:00
}
2016-12-17 22:28:24 +01:00
constructor(props) {
super(props)
this.state = {
2016-12-21 12:06:33 +01:00
pickerOpened: false,
}
}
//TODO: I much rather would do this with absolute positioning
//but I am too stupid to get it to work together with fixed position
//and scrollbars so I have to fallback to JavaScript
calcPickerOffset() {
const elem = this.refs.colorInput
if(elem) {
const pos = elem.getBoundingClientRect()
return {
top: pos.top,
2016-12-26 11:22:41 +01:00
left: pos.left + 196,
2016-12-21 12:06:33 +01:00
}
} else {
2016-12-22 21:06:32 +01:00
console.warn('Color field has no element to adjust position')
2016-12-21 12:06:33 +01:00
return {
top: 160,
2016-12-26 11:22:41 +01:00
left: 555,
2016-12-21 12:06:33 +01:00
}
2016-12-17 22:28:24 +01:00
}
}
togglePicker() {
this.setState({ pickerOpened: !this.state.pickerOpened })
}
get color() {
return Color(this.props.value || '#fff')
}
2016-12-17 21:25:00 +01:00
render() {
2016-12-21 12:06:33 +01:00
const offset = this.calcPickerOffset()
const picker = <div
2017-01-10 21:28:30 +01:00
className="maputnik-color-picker-offset"
2016-12-21 12:06:33 +01:00
style={{
2017-01-10 21:28:30 +01:00
position: 'fixed',
zIndex: 1,
2016-12-21 12:06:33 +01:00
left: offset.left,
top: offset.top,
2016-12-17 21:25:00 +01:00
}}>
2016-12-17 22:28:24 +01:00
<ChromePicker
color={this.color.object()}
2016-12-17 22:28:24 +01:00
onChange={c => this.props.onChange(formatColor(c))}
/>
<div
2017-01-10 21:28:30 +01:00
className="maputnik-color-picker-offset"
2016-12-17 22:28:24 +01:00
onClick={this.togglePicker.bind(this)}
style={{
zIndex: -1,
position: 'fixed',
top: '0px',
right: '0px',
bottom: '0px',
left: '0px',
2016-12-21 12:06:33 +01:00
}}
/>
2016-12-17 22:28:24 +01:00
</div>
2016-09-12 20:29:53 +02:00
2017-01-10 21:28:30 +01:00
return <div className="maputnik-color-wrapper">
2016-12-17 22:28:24 +01:00
{this.state.pickerOpened && picker}
2016-12-17 21:25:00 +01:00
<input
2017-01-10 21:28:30 +01:00
className="maputnik-color"
2016-12-21 12:06:33 +01:00
ref="colorInput"
2016-12-17 22:28:24 +01:00
onClick={this.togglePicker.bind(this)}
2017-01-10 21:28:30 +01:00
style={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