king
2022-10-22 03a22ec6f9ad7303d10b4c65bb5bc6fa5cbd448a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { SketchPicker } from 'react-color'
import { Popover } from 'antd'
import { CloseCircleFilled } from '@ant-design/icons'
 
import './index.scss'
 
const presetColors = [
  '#1890ff', '#f5222d', '#fa541c', '#fa8c16', '#faad14', '#fadb14', '#a0d911', '#52c41a', '#13c2c2', '#2f54eb', '#722ed1',
  '#eb2f96', '#aeb303', '#c32539', '#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 })`
    let _hex = color.hex === 'transparent' ? '#ffffff' : color.hex
 
    this.setState({ color: _color }, () => {
      this.props.onChange && this.props.onChange(_color, _hex)
    })
  }
 
  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 ? <CloseCircleFilled onClick={this.clear}/> : null}</div>
      </div>
    )
  }
}
 
export default ColorSketch