import React, {Component} from 'react'
|
import { is, fromJS } from 'immutable'
|
import { Cascader } from 'antd'
|
|
import MKEmitter from '@/utils/events.js'
|
|
const provinces = require('./data.json')
|
|
class MKSelect extends Component {
|
constructor(props) {
|
super(props)
|
|
const config = props.config
|
let value = config.initval
|
let options = fromJS(config.options).toJS()
|
|
if (value && config.separator) {
|
value = value.split(config.separator)
|
} else {
|
value = value ? [value] : []
|
}
|
|
if (config.resourceType === '2') {
|
options = provinces.map(pro => {
|
let _pro = {
|
label: pro[0],
|
value: pro[0]
|
}
|
if (pro[1]) {
|
_pro.children = pro[1].map(city => {
|
let _city = {
|
label: city[0],
|
value: city[0]
|
}
|
if (city[1]) {
|
_city.children = city[1].map(county => {
|
return {
|
label: county,
|
value: county
|
}
|
})
|
}
|
return _city
|
})
|
}
|
return _pro
|
})
|
} else if (options.length > 0) {
|
options = this.getOptionTree(options)
|
}
|
|
this.state = {
|
config: fromJS(config).toJS(),
|
options: options,
|
value,
|
}
|
}
|
|
componentDidMount () {
|
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
UNSAFE_componentWillReceiveProps (nextProps) {
|
const { config } = this.state
|
|
if (config.resourceType === '1' && !is(fromJS(config.oriOptions), fromJS(nextProps.config.oriOptions))) {
|
let options = fromJS(nextProps.config.options).toJS()
|
|
this.setState({
|
options: this.getOptionTree(options)
|
})
|
}
|
}
|
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
}
|
|
getOptionTree = (options) => {
|
const { config } = this.props
|
|
let _options = []
|
|
options = options.filter(option => {
|
if (option.ParentID === config.topmark) {
|
_options.push(option)
|
return false
|
}
|
return true
|
})
|
|
return this.getTree(_options, options)
|
}
|
|
getTree = (parents, options) => {
|
parents.forEach(parent => {
|
parent.children = []
|
|
options = options.filter(option => {
|
if (option.ParentID === parent.value) {
|
parent.children.push(option)
|
return false
|
}
|
return true
|
})
|
|
if (parent.children.length === 0) {
|
parent.children = null
|
} else {
|
parent.children = this.getTree(parent.children, options)
|
}
|
})
|
|
return parents
|
}
|
|
selectChange = (val, option) => {
|
const { config } = this.state
|
let other = {}
|
|
let _value = val.join(config.separator)
|
let _option = option[option.length - 1]
|
|
if (config.subFields && _option) {
|
config.subFields.forEach((n, i) => {
|
other[n.field] = _option[n.field]
|
setTimeout(() => {
|
MKEmitter.emit('mkFC', 'input', n.uuid, _option[n.field])
|
}, i * 5)
|
})
|
}
|
|
this.props.onChange(_value, other)
|
this.setState({value: val})
|
}
|
|
render() {
|
const { value, options } = this.state
|
|
return (<Cascader defaultValue={value} options={options} onChange={this.selectChange} placeholder="请选择" />)
|
}
|
}
|
|
export default MKSelect
|