king
2021-07-23 c7414c3cc93649479119d51b230c4b8e36884ca7
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { Input } from 'antd'
 
import MKEmitter from '@/utils/events.js'
import './index.scss'
 
const { TextArea } = Input
 
class MkTextArea extends Component {
  static propTpyes = {
    config: PropTypes.bool,
    onChange: PropTypes.func
  }
 
  inputRef = React.createRef()
 
  state = {
    value: '',
    encryption: 'false'
  }
 
  UNSAFE_componentWillMount () {
    const { config } = this.props
    let _value = config.initval
    let encryption = 'false'
 
    if (config.encryption === 'true') {
      encryption = 'true'
      if (_value) {
        try {
          _value = window.decodeURIComponent(window.atob(_value))
        } catch {
          _value = config.initval
        }
      }
    }
    
    this.setState({
      value: _value,
      encryption
    })
  }
 
  componentDidMount () {
    MKEmitter.addListener('mkFC', this.mkFormHandle)
  }
 
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('mkFC', this.mkFormHandle)
  }
 
  mkFormHandle = (type, uuid, value) => {
    if (uuid !== this.props.config.uuid) return
    if (type === 'focus') {
      this.inputRef.current.focus()
    } else if (type === 'input') {
      this.setState({value})
    }
  }
 
  onChange = (e) => {
    const { encryption } = this.state
    let val = e.target.value
 
    this.setState({ value: val })
 
    let _val = val
    if (encryption === 'true') {
      try {
        _val = window.btoa(window.encodeURIComponent(_val))
      } catch {
        _val = val
      }
    }
    this.props.onChange(_val)
  }
 
  render() {
    const { config } = this.props
    const { value } = this.state
 
    return (
      <TextArea ref={this.inputRef} value={value} autoSize={{ minRows: 2, maxRows: config.maxRows || 6 }} onChange={this.onChange} disabled={config.readonly} />
    )
  }
}
 
export default MkTextArea