import React, {Component} from 'react'
|
import { is, fromJS } from 'immutable'
|
import { Select } from 'antd'
|
|
import MKEmitter from '@/utils/events.js'
|
|
class MKSearchSelect extends Component {
|
constructor(props) {
|
super(props)
|
|
const config = props.config
|
let value = config.initval
|
|
if (config.type === 'multiselect') {
|
if (value) {
|
value = value.split(',')
|
} else {
|
value = []
|
}
|
}
|
|
this.state = {
|
config: fromJS(config).toJS(),
|
options: fromJS(config.options).toJS(),
|
value,
|
}
|
}
|
|
componentDidMount () {
|
const { config } = this.state
|
|
if (config.type !== 'multiselect') {
|
MKEmitter.addListener('mkSP', this.mkFormHandle)
|
}
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
UNSAFE_componentWillReceiveProps (nextProps) {
|
const { config } = this.state
|
|
if (!is(fromJS(config.oriOptions), fromJS(nextProps.config.oriOptions))) {
|
this.setState({
|
config: fromJS(nextProps.config).toJS(),
|
options: fromJS(nextProps.config.options).toJS()
|
})
|
}
|
}
|
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('mkSP', this.mkFormHandle)
|
}
|
|
mkFormHandle = (uuid, parentId, level) => {
|
const { config } = this.state
|
|
if (uuid !== config.uuid) return
|
|
let options = config.oriOptions.filter(option => option.ParentID === parentId || option.Value === '')
|
let val = options[0] ? options[0].Value : ''
|
|
this.setState({
|
options,
|
value: val
|
})
|
|
this.props.onChange(val, true)
|
|
if (level < 7 && config.linkFields) {
|
config.linkFields.forEach((m, i) => {
|
setTimeout(() => {
|
MKEmitter.emit('mkSP', m.uuid, val, level + 1)
|
}, (i + 1) * 10)
|
})
|
}
|
}
|
|
selectChange = (val) => {
|
const { config } = this.state
|
|
if (config.type === 'multiselect') {
|
this.props.onChange(val.join(','))
|
} else {
|
config.linkFields && config.linkFields.forEach((m, i) => {
|
setTimeout(() => {
|
MKEmitter.emit('mkSP', m.uuid, val, 0)
|
}, (i + 1) * 10)
|
})
|
|
this.props.onChange(val)
|
}
|
|
this.setState({value: val})
|
}
|
|
render() {
|
const { value, config, options } = this.state
|
|
return (
|
<Select
|
showSearch
|
mode={config.type === 'multiselect' ? 'multiple' : ''}
|
dropdownMatchSelectWidth={config.dropdown !== 'false'}
|
value={value}
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
onChange={this.selectChange}
|
>
|
{options.map(option =>
|
<Select.Option id={option.key} key={option.key} title={option.Text} value={option.Value}>{option.Text}</Select.Option>
|
)}
|
</Select>
|
)
|
}
|
}
|
|
export default MKSearchSelect
|