king
2021-03-31 4f5f8002496e8b32c1ba83e9d3a0674e61c41c03
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { Input } from 'antd'
 
import './index.scss'
 
const { TextArea } = Input
 
class CustomTextArea extends Component {
  static propTpyes = {
    Item: PropTypes.bool,      // 表单
    onChange: PropTypes.func   // 数据切换
  }
 
  state = {
    value: '',
    encryption: false
  }
 
  UNSAFE_componentWillMount () {
    let value = ''
    let encryption = false
 
    if (this.props['data-__meta']) {
      value = this.props['data-__meta'].initialValue || ''
    }
 
    if (this.props.Item && this.props.Item.encryption === 'true') {
      encryption = true
      if (value) {
        try {
          value = window.decodeURIComponent(window.atob(value))
        } catch {
          value = this.props['data-__meta'].initialValue || ''
        }
      }
    }
    
    this.setState({
      value,
      encryption
    })
  }
 
  UNSAFE_componentWillReceiveProps(nextProps) {
    const { value, encryption } = this.state
 
    if (!encryption && value !== nextProps.value) {
      this.setState({ value: nextProps.value || '' })
    } else if (encryption && window.btoa(window.encodeURIComponent(value)) !== nextProps.value) {
      let _value = nextProps.value || ''
      try {
        _value = window.decodeURIComponent(window.atob(_value))
      } catch {
        _value = nextProps.value || ''
      }
      this.setState({ value: _value })
    }
  }
 
  onChange = (e) => {
    const { encryption } = this.state
    let val = e.target.value
 
    this.setState({ value: val })
 
    let _val = val
    if (encryption) {
      try {
        _val = window.btoa(window.encodeURIComponent(_val))
      } catch {
        _val = val
      }
    }
    this.props.onChange(_val)
  }
 
  render() {
    const { Item } = this.props
    const { value } = this.state
 
    return (
      <TextArea value={value} autoSize={{ minRows: 2, maxRows: Item.maxRows || 6 }} onChange={this.onChange} disabled={Item.readonly === 'true'} />
    )
  }
}
 
export default CustomTextArea