import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Col, Row } from 'antd'
|
|
import MKEmitter from '@/utils/events.js'
|
import './index.scss'
|
|
class MKCheckCard extends Component {
|
static propTpyes = {
|
config: PropTypes.object,
|
onChange: PropTypes.func
|
}
|
|
state = {
|
selectKeys: null
|
}
|
|
UNSAFE_componentWillMount() {
|
const { config } = this.props
|
|
if (config.multiple === 'true') {
|
this.setState({
|
selectKeys: config.initval ? config.initval.split(',') : []
|
})
|
} else {
|
this.setState({
|
selectKeys: config.initval
|
})
|
}
|
}
|
|
changeCard = (item) => {
|
const { config } = this.props
|
const { selectKeys } = this.state
|
|
if (config.readonly) return
|
|
if (config.multiple === 'true') {
|
let keys = []
|
if (selectKeys.includes(item.$value)) {
|
keys = selectKeys.filter(key => key !== item.$value)
|
} else {
|
keys = [...selectKeys, item.$value]
|
}
|
|
this.setState({
|
selectKeys: keys
|
}, () => {
|
this.props.onChange(keys.join(','))
|
})
|
} else if (selectKeys !== item.$value) {
|
let other = {}
|
config.subFields && config.subFields.forEach((n, i) => {
|
other[n.field] = item[n.field]
|
setTimeout(() => {
|
MKEmitter.emit('mkFC', 'input', n.uuid, item[n.field])
|
}, i * 5)
|
})
|
config.linkFields && config.linkFields.forEach((m, i) => {
|
setTimeout(() => {
|
MKEmitter.emit('mkFP', m.uuid, item.$value, 0)
|
}, (i + 1) * 100)
|
})
|
|
this.setState({
|
selectKeys: item.$value
|
}, () => {
|
this.props.onChange(item.$value, other)
|
})
|
}
|
}
|
|
getCards = () => {
|
const { display, width, options, fields, ratio, multiple, backgroundColor, borderColor } = this.props.config
|
const { selectKeys } = this.state
|
|
let paddingTop = '100%'
|
if (ratio === '4:3') {
|
paddingTop = '75%'
|
} else if (ratio === '3:2') {
|
paddingTop = '66.7%'
|
} else if (ratio === '16:9') {
|
paddingTop = '56.25%'
|
}
|
|
let style = borderColor ? {borderColor} : {}
|
let _style = backgroundColor ? {backgroundColor} : null
|
|
if (!options || options.length === 0) {
|
return null
|
} else if (display !== 'picture') {
|
if (!fields || fields.length === 0) {
|
return null
|
}
|
return options.map(item => {
|
let _active = false
|
if (multiple === 'true' && selectKeys.includes(item.$value)) {
|
_active = true
|
} else if (multiple !== 'true' && selectKeys === item.$value) {
|
_active = true
|
}
|
|
return <Col span={width} key={item.key}>
|
<div className={'card-cell' + (_active ? ' active' : '') + (_style ? ' bg-control' : '')} style={style} onClick={() => this.changeCard(item)}>
|
<div className="bg-mask" style={_style}></div>
|
{fields.map(col => {
|
return <span key={col.key} style={{color: col.color, fontSize: col.fontSize + 'px', height: col.fontSize * 1.5 + 'px', textAlign: col.align}}>{item[col.field]}</span>
|
})}
|
</div>
|
</Col>
|
})
|
} else {
|
return options.map(item => {
|
let _active = false
|
if (multiple === 'true' && selectKeys.includes(item.$value)) {
|
_active = true
|
} else if (multiple !== 'true' && selectKeys === item.$value) {
|
_active = true
|
}
|
|
return <Col span={width} key={item.key}>
|
<div className={'card-pic-cell ' + (_active ? 'active' : '')} onClick={() => this.changeCard(item)} style={{...style, paddingTop, backgroundImage: `url(${item.$url})`}}>
|
</div>
|
</Col>
|
})
|
}
|
}
|
|
render() {
|
const { config } = this.props
|
|
return (
|
<div className={'check-card-form-box' + (config.readonly ? ' readonly' : '')}>
|
<Row gutter={12}>{this.getCards()}</Row>
|
</div>
|
)
|
}
|
}
|
|
export default MKCheckCard
|