import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
// import { is, fromJS } from 'immutable'
|
import { SketchPicker } from 'react-color'
|
import { Popover, Icon } from 'antd'
|
|
import './index.scss'
|
|
const presetColors = [
|
'#f5222d', '#fa541c', '#fa8c16', '#faad14', '#fadb14', '#a0d911', '#52c41a', '#13c2c2', '#1890ff', '#2f54eb', '#722ed1',
|
'#eb2f96', '#595959', '#ffa39e', '#ffbb96', '#ffd591', '#ffe58f', '#fffb8f', '#eaff8f', '#b7eb8f', '#87e8de', '#91d5ff',
|
'#adc6ff', '#d3adf7', '#EBE9E9', '#d9d9d9', '#434343', '#000000', '#ffffff', 'transparent'
|
]
|
|
class ColorSketch extends Component {
|
static propTpyes = {
|
defaultValue: PropTypes.any,
|
value: PropTypes.any,
|
onChange: PropTypes.func
|
}
|
state = {
|
color: '',
|
colors: [],
|
allowClear: false
|
}
|
|
UNSAFE_componentWillMount () {
|
const { defaultValue, value, allowClear } = this.props
|
let initVal = ''
|
|
if (this.props['data-__meta']) {
|
initVal = this.props['data-__meta'].initialValue
|
} else if (defaultValue) {
|
initVal = defaultValue
|
} else if (value) {
|
initVal = value
|
}
|
|
let _colors = sessionStorage.getItem('app_colors')
|
let colors = presetColors
|
|
if (_colors && _colors !== '[]') {
|
try {
|
_colors = JSON.parse(_colors)
|
} catch (e) {
|
_colors = null
|
}
|
|
if (_colors) {
|
colors = presetColors.map((item, i) => {
|
if (_colors[i] && _colors[i].linkurl && !presetColors.includes(_colors[i].linkurl)) {
|
return _colors[i].linkurl
|
}
|
return item
|
})
|
}
|
}
|
|
this.setState({color: initVal, allowClear: allowClear === true, colors})
|
}
|
|
handleChange = (color) => {
|
let _color = `rgba(${ color.rgb.r }, ${ color.rgb.g }, ${ color.rgb.b }, ${ color.rgb.a })`
|
|
this.setState({ color: _color }, () => {
|
this.props.onChange && this.props.onChange(_color)
|
})
|
}
|
|
clear = () => {
|
this.setState({ color: '' }, () => {
|
this.props.onChange && this.props.onChange('')
|
})
|
}
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
if (nextProps.value !== undefined && nextProps.value !== this.state.color) {
|
this.setState({ color: nextProps.value })
|
}
|
}
|
|
render() {
|
const { color, allowClear, colors } = this.state
|
return (
|
<div className="color-sketch-block">
|
<Popover content={
|
<SketchPicker color={ color } presetColors={colors} onChange={ this.handleChange } />
|
} overlayClassName="color-sketch-popover" placement="bottomRight" title="" trigger="click">
|
<div className="color-sketch-block-box">
|
<div className="color-sketch-block-inner" style={ {background: color} }></div>
|
</div>
|
</Popover>
|
<div className="color-sketch-value">{color}{allowClear && color ? <Icon onClick={this.clear} type="close"/> : null}</div>
|
</div>
|
)
|
}
|
}
|
|
export default ColorSketch
|