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
import React, { Component } from 'react'
import { is, fromJS } from 'immutable'
import { InputNumber } from 'antd'
 
import MKEmitter from '@/utils/events.js'
 
import './index.scss'
 
class MKNumberInput extends Component {
  constructor(props) {
    super(props)
 
    const config = props.config
 
    let precision = (config.decimal || config.decimal === 0) ? config.decimal : null
    
    this.state = {
      precision,
      value: config.initval
    }
  }
 
  inputNumberRef = 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.inputNumberRef.current.focus()
    } else if (type === 'input') {
      this.setState({value})
    }
  }
 
  handleChange = (val) => {
 
    this.props.onChange(val)
    this.setState({value: val})
  }
 
  render() {
    const { config, onSubmit } = this.props
    const { value, precision } = this.state
 
    if (precision === null) {
      return (<InputNumber ref={this.inputNumberRef} value={value} disabled={config.readonly} onChange={this.handleChange} onPressEnter={onSubmit}/>)
    } else {
      return (<InputNumber ref={this.inputNumberRef} value={value} precision={precision} disabled={config.readonly} onChange={this.handleChange} onPressEnter={onSubmit} />)
    }
  }
}
 
export default MKNumberInput