king
2021-03-30 ecbe0dc46ce2b8f607b9afd063104adeb7f10fe8
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
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
    })
  }
 
  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 { 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