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="" value={value} autoComplete="off" disabled={config.readonly} onChange={this.handleChange} onPressEnter={this.handleInputSubmit} />
|
}
|
}
|
|
export default MKInput
|