maputnik/src/fields/color.jsx

48 lines
1.3 KiB
React
Raw Normal View History

2016-09-12 19:47:28 +02:00
import React from 'react'
import inputStyle from './input.js'
2016-12-17 21:25:00 +01:00
import ColorPicker from 'react-colorpickr'
import 'react-colorpickr/dist/colorpickr.css'
function formatColor(color) {
if(color.a !== 1) {
return `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a})`
}
return `rgb(${color.r}, ${color.g}, ${color.b})`
}
2016-09-12 19:47:28 +02:00
/*** Number fields with support for min, max and units and documentation*/
class ColorField extends React.Component {
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
default: React.PropTypes.number,
doc: React.PropTypes.string,
}
2016-12-17 21:25:00 +01:00
render() {
return <div style={{...inputStyle.property, position: 'relative'}}>
<div style={{
position: 'absolute',
left: 200
}}>
<ColorPicker
value={this.props.value}
onChange={c => this.props.onChange(formatColor(c))}
/>
</div>
2016-09-12 20:29:53 +02:00
2016-12-17 21:25:00 +01:00
<label style={inputStyle.label}>{this.props.name}</label>
<input
style={inputStyle.input}
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