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
|
|
this.state = {
|
value: config.initval,
|
precision: config.precision
|
}
|
}
|
|
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, field, value) => {
|
if (field !== this.props.config.field) return
|
|
if (type === 'focus') {
|
this.inputNumberRef.current.focus()
|
} else if (type === 'input') {
|
this.setState({value})
|
this.props.onChange(value, true)
|
}
|
}
|
|
handleChange = (val) => {
|
this.setState({value: val})
|
this.props.onChange(val)
|
}
|
|
render() {
|
const { onSubmit } = this.props
|
const { value, precision } = this.state
|
|
return (<InputNumber ref={this.inputNumberRef} value={value} precision={precision} onChange={this.handleChange} onPressEnter={onSubmit} />)
|
}
|
}
|
|
export default MKNumberInput
|