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 MKEInput 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 () {
|
const { config } = this.props
|
MKEmitter.addListener('mkFC', this.mkFormHandle)
|
|
if (config.focus) {
|
this.inputRef.current.select()
|
}
|
}
|
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('mkFC', this.mkFormHandle)
|
}
|
|
mkFormHandle = (type, field, value) => {
|
if (field !== this.props.config.field) return
|
|
if (type === 'focus') {
|
this.inputRef.current.select()
|
} else if (type === 'input') {
|
this.setState({value})
|
this.props.onChange(value, true)
|
}
|
}
|
|
handleChange = (e) => {
|
let val = e.target.value
|
|
this.setState({value: val})
|
this.props.onChange(val)
|
}
|
|
render() {
|
const { config } = this.props
|
const { value } = this.state
|
|
return <Input ref={this.inputRef} readOnly={config.readOnly} placeholder={config.placeholder || ''} value={value} autoComplete="off" onChange={this.handleChange} onPressEnter={this.props.onSubmit} />
|
}
|
}
|
|
export default MKEInput
|