import React, { Component } from 'react'
|
import { is, fromJS } from 'immutable'
|
import { Select } from 'antd'
|
|
import MKEmitter from '@/utils/events.js'
|
|
class MKSelect extends Component {
|
constructor(props) {
|
super(props)
|
|
const config = props.config
|
let value = config.initval
|
|
if (config.type === 'multiselect') {
|
value = value || []
|
}
|
|
this.state = {
|
config: fromJS(config).toJS(),
|
options: fromJS(config.options).toJS(),
|
value,
|
}
|
}
|
|
componentDidMount () {
|
const { config } = this.state
|
|
if (config.type !== 'multiselect') {
|
MKEmitter.addListener('mkFP', this.mkFormHandle)
|
}
|
MKEmitter.addListener('mkFC', this.mkFormControl)
|
}
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
if (nextProps.config.timestamp && nextProps.config.timestamp !== this.state.config.timestamp) {
|
this.setState({
|
config: fromJS(nextProps.config).toJS(),
|
options: fromJS(nextProps.config.options).toJS(),
|
})
|
}
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('mkFP', this.mkFormHandle)
|
MKEmitter.removeListener('mkFC', this.mkFormControl)
|
}
|
|
mkFormControl = (type, field, value) => {
|
if (field !== this.props.config.field) return
|
|
if (type === 'input') {
|
this.setState({value})
|
this.props.onChange(value, {})
|
}
|
}
|
|
mkFormHandle = (field, parentId) => {
|
if (field !== this.state.config.field) return
|
|
const { config } = this.state
|
|
let options = config.oriOptions ? config.oriOptions.filter(option => option.ParentID === parentId || option.ParentID === '') : []
|
let val = options[0] ? options[0].value : ''
|
|
this.setState({
|
options,
|
value: val
|
})
|
|
this.props.onChange(val)
|
|
config.linkFields && config.linkFields.forEach((m, i) => {
|
setTimeout(() => {
|
MKEmitter.emit('mkFP', m, val)
|
}, (i + 1) * 70)
|
})
|
}
|
|
selectChange = (val) => {
|
const { config } = this.state
|
let other = {}
|
|
if (config.subFields) {
|
let option = this.state.options.filter(m => m.value === val)[0]
|
option && config.subFields.forEach((n, i) => {
|
other[n] = option[n] || ''
|
setTimeout(() => {
|
MKEmitter.emit('mkFC', 'input', n, option[n] || '')
|
}, i * 5)
|
})
|
}
|
|
config.linkFields && config.linkFields.forEach((m, i) => {
|
setTimeout(() => {
|
MKEmitter.emit('mkFP', m, val)
|
}, (i + 1) * 100)
|
})
|
|
this.setState({value: val})
|
this.props.onChange(val, other)
|
}
|
|
mutilselectChange = (val) => {
|
this.props.onChange(val)
|
}
|
|
render() {
|
const { value, config, options } = this.state
|
|
if (config.type !== 'multiselect') {
|
if (config.joint) {
|
return (
|
<Select
|
showSearch
|
allowClear
|
value={value}
|
dropdownMatchSelectWidth={config.dropdown !== 'false'}
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
onSelect={this.selectChange}
|
onChange={(val) => val === undefined && this.selectChange('')}
|
getPopupContainer={() => document.getElementById('normal-form-field')}
|
>
|
{options.map((option, i) =>
|
<Select.Option key={i} disabled={option.disabled} value={option.field || ''}>{`${option.label}(${option.field})`}</Select.Option>
|
)}
|
</Select>
|
)
|
}
|
return (
|
<Select
|
showSearch
|
allowClear
|
value={value}
|
dropdownMatchSelectWidth={config.dropdown !== 'false'}
|
filterOption={(input, option) => (option.props.children + option.props.extend).toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
onSelect={this.selectChange}
|
onChange={(val) => val === undefined && this.selectChange('')}
|
getPopupContainer={() => document.getElementById('normal-form-field')}
|
>
|
{options.map((option, i) =>
|
<Select.Option key={i} disabled={option.disabled} title={option.label || option.text} extend={config.extendName ? (option[config.extendName] || '') : ''} value={option.value || option.field || ''}>{option.label || option.text}</Select.Option>
|
)}
|
</Select>
|
)
|
} else {
|
return (<Select
|
showSearch
|
mode="multiple"
|
defaultValue={value}
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
onChange={this.mutilselectChange}
|
getPopupContainer={() => document.getElementById('normal-form-field')}
|
>
|
{options.map((option, i) =>
|
<Select.Option key={i} disabled={option.disabled} value={option.value || option.field}>{option.label || option.text}</Select.Option>
|
)}
|
</Select>)
|
}
|
}
|
}
|
|
export default MKSelect
|