import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Col, Row } from 'antd'
|
|
import './index.scss'
|
|
class CheckCard extends Component {
|
static propTpyes = {
|
card: PropTypes.bool, // 卡片信息
|
onChange: PropTypes.func // 数据切换
|
}
|
|
state = {
|
selectKeys: null
|
}
|
|
UNSAFE_componentWillMount() {
|
const { card } = this.props
|
|
if (card.multiple === 'true') {
|
this.setState({
|
selectKeys: card.initval ? card.initval.split(',') : []
|
})
|
} else {
|
this.setState({
|
selectKeys: card.initval
|
})
|
}
|
}
|
|
changeCard = (item) => {
|
const { multiple, readonly } = this.props.card
|
const { selectKeys } = this.state
|
|
if (readonly) return
|
|
if (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 && this.props.onChange(keys.join(','))
|
})
|
} else if (multiple !== 'true' && selectKeys !== item.$value) {
|
this.setState({
|
selectKeys: item.$value
|
}, () => {
|
this.props.onChange && this.props.onChange(item.$value)
|
})
|
}
|
}
|
|
getCards = () => {
|
const { display, width, options, fields, ratio, multiple, backgroundColor, borderColor } = this.props.card
|
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 = {}
|
|
if (borderColor) {
|
style.borderColor = borderColor
|
}
|
|
if (!options || options.length === 0) {
|
return null
|
} else if (display !== 'picture') {
|
if (!fields || fields.length === 0) {
|
return null
|
}
|
if (backgroundColor) {
|
style.backgroundColor = backgroundColor
|
}
|
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={style} onClick={() => this.changeCard(item)}>
|
{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 { card } = this.props
|
return (
|
<div className={'check-card-form-box' + (card.readonly ? ' readonly' : '')}>
|
<Row gutter={12}>{this.getCards()}</Row>
|
</div>
|
)
|
}
|
}
|
|
export default CheckCard
|