king
2021-09-01 31ec63f0419895876cbaba99637a884a32d33d0d
src/mob/colorsketch/index.jsx
@@ -2,14 +2,14 @@
import PropTypes from 'prop-types'
// import { is, fromJS } from 'immutable'
import { SketchPicker } from 'react-color'
import { Popover } from 'antd'
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', '#ffadd2', '#d9d9d9', '#434343', '#000000', '#ffffff', 'transparent'
  '#adc6ff', '#d3adf7', '#EBE9E9', '#d9d9d9', '#434343', '#000000', '#ffffff', 'transparent'
]
class ColorSketch extends Component {
@@ -20,10 +20,12 @@
  }
  state = {
    color: '',
    colors: [],
    allowClear: false
  }
  UNSAFE_componentWillMount () {
    const { defaultValue, value } = this.props
    const { defaultValue, value, allowClear } = this.props
    let initVal = ''
    if (this.props['data-__meta']) {
@@ -33,15 +35,41 @@
    } 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})
    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('')
    })
  }
@@ -52,17 +80,17 @@
  }
  render() {
    const { color } = this.state
    const { color, allowClear, colors } = this.state
    return (
      <div className="color-sketch-block">
        <Popover content={
          <SketchPicker color={ color } presetColors={presetColors} onChange={ this.handleChange } />
          <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}</div>
        <div className="color-sketch-value">{color}{allowClear && color ? <Icon onClick={this.clear} type="close"/> : null}</div>
      </div>
    )
  }