king
2022-12-09 cb9ade2afd2a367ad767bc605ab7086c695dd010
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Select, Input } from 'antd'
 
import './index.scss'
 
const { Option } = Select
 
class StyleInput extends Component {
  static propTpyes = {
    defaultValue: PropTypes.any,
    options: PropTypes.any,
    value: PropTypes.any,
    onChange: PropTypes.func,
  }
 
  state = {
    value: '',
    unit: '',
    options: null
  }
 
  UNSAFE_componentWillMount () {
    const { defaultValue, value, options } = this.props
    let val = ''
    let unit = ''
    let _options = ['px']
 
    if (value !== undefined) {
      val = value
    } else if (defaultValue !== undefined) {
      val = defaultValue
    }
 
    if (options && options.length > 0) {
      _options = options
    }
 
    unit = _options[0]
 
    if (val) {
      if (val.indexOf('px') > -1) {
        unit = 'px'
      } else if (val.indexOf('%') > -1) {
        unit = '%'
      } else if (val.indexOf('vw') > -1) {
        unit = 'vw'
      } else if (val.indexOf('vh') > -1) {
        unit = 'vh'
      }
    }
 
    let _val = parseInt(val)
 
    if (isNaN(_val)) {
      _val = ''
    }
 
    this.setState({value: _val, options: _options, unit})
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (nextProps.value !== undefined && nextProps.value !== this.state.value) {
      let val = nextProps.value
      let unit = this.state.unit
 
      if (val) {
        if (val.indexOf('px') > -1) {
          unit = 'px'
        } else if (val.indexOf('%') > -1) {
          unit = '%'
        } else if (val.indexOf('vw') > -1) {
          unit = 'vw'
        } else if (val.indexOf('vh') > -1) {
          unit = 'vh'
        }
      }
 
      let _val = parseInt(val)
 
      if (isNaN(_val)) {
        _val = ''
      }
 
      this.setState({value: _val, unit})
    }
  }
 
  componentDidMount () {}
 
  /**
   * @description 组件销毁,清除state更新,清除快捷键设置
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
 
  changeValue = (e) => {
    const { unit } = this.state
    let val = e.target.value
    let _val = parseInt(val)
    
    if (isNaN(_val)) {
      _val = ''
    }
 
    this.setState({
      value: _val,
    })
 
    if (this.props.onChange) {
      if (!_val) {
        this.props.onChange('0px')
      } else {
        this.props.onChange(`${_val}${unit}`)
      }
    }
  }
 
  changeUnit = (val) => {
    const { value } = this.state
 
    this.setState({unit: val})
    if (value && this.props.onChange) {
      this.props.onChange(`${value}${val}`)
    }
  }
 
  render () {
    const { value, options, unit } = this.state
 
    return (
      <div className="style-input-box">
        <Input value={value} addonAfter={
          options.length > 1 ?
          <Select value={<span>{unit}</span>} onChange={this.changeUnit}>
            {options.map(item => <Option key={item} value={item}>{item}</Option>)}
          </Select> :
          <div className="single-unit">{unit}</div>
        } onChange={this.changeValue}/>
      </div>
    )
  }
}
 
export default StyleInput