king
7 天以前 a1e9b18a4dbfd21e1bf4d5cb60974ac2f0115efd
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
import React, { Component } from 'react'
import { is, fromJS } from 'immutable'
import { Input } from 'antd'
import md5 from 'md5'
 
import MKEmitter from '@/utils/events.js'
 
import './index.scss'
 
/**
 * @description 自定义文本输入
 */
class MKInput extends Component {
  constructor(props) {
    super(props)
    
    const config = props.config
    let _value = config.initval
    let encryption = 'false'
 
    if (config.encryption === 'true') {
      encryption = 'true'
      if (_value) {
        try {
          _value = window.decodeURIComponent(window.atob(_value))
        } catch (e) {
          _value = config.initval
        }
      }
    } else if (config.encryption === 'md5') {
      encryption = 'md5'
    }
    
    this.state = {
      value: _value,
      encryption
    }
  }
  
  inputRef = React.createRef()
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  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.select()
    } else if (type === 'input') {
      this.setState({value})
      let _val = value
 
      if (this.state.encryption === 'true') {
        try {
          _val = window.btoa(window.encodeURIComponent(_val))
        } catch (e) {
          _val = value
        }
        this.props.onChange(_val)
      } else if (this.state.encryption === 'md5') {
        _val = _val + ''
        _val = md5(_val.toLowerCase())
        _val = _val.toUpperCase()
        
        this.props.onChange(_val)
      } else {
        this.props.onChange(_val, true)
      }
    }
  }
 
  handleChange = (e) => {
    let val = e.target.value
    let submit = /\n/g.test(val)
 
    if (submit) {
      val = val.replace(/\n|'/g, '')
    }
 
    if (submit && /^\s+$/.test(val)) {
      submit = false
    }
 
    let _val = val
 
    if (this.state.encryption === 'true') {
      try {
        _val = window.btoa(window.encodeURIComponent(_val))
      } catch (e) {
        _val = val
      }
    } else if (this.state.encryption === 'md5') {
      _val = md5(_val.toLowerCase())
      _val = _val.toUpperCase()
    }
 
    this.props.onChange(_val)
 
    this.setState({value: val}, () => {
      if (submit) {
        this.handleInputSubmit()
      } else if (!val) {
        this.inputRef.current.focus()
      }
    })
  }
 
  handleInputSubmit = () => {
    const { config } = this.props
 
    if (config.enter === 'false') return
    if (config.enter === 'tab') {
      MKEmitter.emit('mkFC', 'focus', config.tabUuid)
    } else {
      MKEmitter.emit('mkFC', 'focus', config.tabUuid)
      this.props.onSubmit(config.tabUuid, config.errTabUuid)
    }
  }
 
  render() {
    const { config } = this.props
    const { value } = this.state
 
    if (config.inputType === 'password') {
      return <Input.Password ref={this.inputRef} className="mk-form-input" placeholder={config.placeholder || ''} value={value} autoComplete="off" disabled={config.readonly} onChange={this.handleChange} onPressEnter={this.handleInputSubmit}/>
    }
 
    return <Input ref={this.inputRef} className="mk-form-input" allowClear placeholder={config.placeholder || ''} value={value} autoComplete="off" disabled={config.readonly} onChange={this.handleChange} onPressEnter={this.handleInputSubmit} />
  }
}
 
export default MKInput