king
2022-09-08 0a72ad847aad17cf7dbffc239dec0d62c34a10d1
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
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
    }
  }
 
  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') {
      let node = document.getElementById(uuid)
      node.select()
    } else if (type === 'input') {
      this.setState({value})
      this.props.onChange(value, true)
    }
  }
 
  handleChange = (val) => {
    this.setState({value: val})
 
    let _val = val
 
    if (typeof(_val) !== 'number') {
      _val = parseFloat(_val)
 
      if (isNaN(_val)) {
        _val = ''
      }
    }
    
    this.props.onChange(_val)
  }
 
  handleSubmit = () => {
    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()
    }
  }
 
  render() {
    const { config } = this.props
    const { value, precision } = this.state
 
    if (precision === null) {
      return (<InputNumber id={config.uuid} value={value} disabled={config.readonly} onChange={this.handleChange} onPressEnter={this.handleSubmit}/>)
    } else {
      return (<InputNumber id={config.uuid} value={value} precision={precision} disabled={config.readonly} onChange={this.handleChange} onPressEnter={this.handleSubmit} />)
    }
  }
}
 
export default MKNumberInput