king
2021-07-30 f6a1ab6a58215cf7546976a86eb6face1a7be32f
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
import React, { Component } from 'react'
import { is, fromJS } from 'immutable'
import { Input } from 'antd'
 
import MKEmitter from '@/utils/events.js'
 
import './index.scss'
 
/**
 * @description 自定义文本输入
 */
class MKInput extends Component {
  constructor(props) {
    super(props)
    
    const config = props.config
    
    this.state = {
      value: config.initval
    }
  }
  
  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})
    }
  }
 
  handleChange = (e) => {
    let val = e.target.value
 
    if (!/\n/ig.test(val)) {
      this.props.onChange(val)
      this.setState({value: val})
    } else {
      val = val.replace(/\n/ig, '')
 
      this.props.onChange(val)
      this.setState({value: val}, () => {
        this.handleInputSubmit()
      })
    }
  }
 
  handleInputSubmit = () => {
    const { config } = this.props
 
    if (config.enter === 'false') return
    if (config.enter === 'tab') {
      MKEmitter.emit('mkFC', 'focus', config.tabUuid)
    } else {
      this.props.onSubmit()
    }
  }
 
  render() {
    const { config } = this.props
    const { value } = this.state
 
    return <Input ref={this.inputRef} placeholder={config.placeholder || ''} value={value} autoComplete="off" disabled={config.readonly} onChange={this.handleChange} onPressEnter={this.handleInputSubmit} />
  }
}
 
export default MKInput