import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Input } from 'antd'
|
|
import MKEmitter from '@/utils/events.js'
|
import './index.scss'
|
|
const { TextArea } = Input
|
|
class MkTextArea extends Component {
|
static propTpyes = {
|
config: PropTypes.bool,
|
onChange: PropTypes.func
|
}
|
|
inputRef = React.createRef()
|
|
state = {
|
value: '',
|
encryption: 'false'
|
}
|
|
UNSAFE_componentWillMount () {
|
const { config } = this.props
|
let _value = config.initval
|
let encryption = 'false'
|
|
if (config.encryption === 'true') {
|
encryption = 'true'
|
if (_value) {
|
try {
|
_value = window.decodeURIComponent(window.atob(_value))
|
} catch (e) {
|
_value = config.initval
|
}
|
}
|
}
|
|
this.setState({
|
value: _value,
|
encryption
|
})
|
}
|
|
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.focus()
|
} else if (type === 'input') {
|
this.setState({value})
|
this.props.onChange(value, true)
|
}
|
}
|
|
onChange = (e) => {
|
const { encryption } = this.state
|
let val = e.target.value
|
|
this.setState({ value: val })
|
|
let _val = val
|
if (encryption === 'true') {
|
try {
|
_val = window.btoa(window.encodeURIComponent(_val))
|
} catch (e) {
|
_val = val
|
}
|
}
|
this.props.onChange(_val)
|
}
|
|
render() {
|
const { config } = this.props
|
const { value } = this.state
|
|
return (
|
<TextArea ref={this.inputRef} placeholder={config.placeholder || ''} value={value} autoSize={{ minRows: 2, maxRows: config.maxRows || 6 }} onChange={this.onChange} disabled={config.readonly} />
|
)
|
}
|
}
|
|
export default MkTextArea
|