import React from 'react'
|
import { useDrag, useDrop } from 'react-dnd'
|
import { Icon, Select, DatePicker, Input, InputNumber } from 'antd'
|
import moment from 'moment'
|
import ItemTypes from './itemtypes'
|
import './index.scss'
|
|
const { MonthPicker } = DatePicker
|
|
const Card = ({ id, card, moveCard, findCard, editCard, closeCard, hasDrop }) => {
|
const originalIndex = findCard(id).index
|
const [{ isDragging }, drag] = useDrag({
|
item: { type: ItemTypes.form, id, originalIndex },
|
collect: monitor => ({
|
isDragging: monitor.isDragging(),
|
}),
|
})
|
const [, drop] = useDrop({
|
accept: ItemTypes.form,
|
canDrop: () => true,
|
drop: (item) => {
|
if (!item.hasOwnProperty('originalIndex')) {
|
hasDrop(card)
|
}
|
},
|
hover({ id: draggedId }) {
|
if (!draggedId) return
|
if (draggedId !== id) {
|
const { index: overIndex } = findCard(id)
|
moveCard(draggedId, overIndex)
|
}
|
},
|
})
|
const opacity = isDragging ? 0 : 1
|
|
const edit = () => {
|
editCard(id)
|
}
|
|
const close = () => {
|
closeCard(id)
|
}
|
|
let selectval = ''
|
if (card.type === 'multiselect' || card.type === 'select' || card.type === 'link') {
|
if (card.initval) {
|
let _option = card.options.filter(option => option.Value === card.initval)[0]
|
if (_option) {
|
selectval = _option.Text || ''
|
} else {
|
selectval = ''
|
}
|
} else if (card.setAll === 'true') {
|
selectval = '全部'
|
}
|
}
|
|
return (
|
<div className="page-card" style={{ opacity: opacity}}>
|
<div ref={node => drag(drop(node))}>
|
{<div className="ant-row ant-form-item">
|
<div className="ant-col ant-form-item-label ant-col-xs-24 ant-col-sm-8">
|
<label title={card.label}>{card.label}</label>
|
</div>
|
<div className="ant-col ant-form-item-control-wrapper ant-col-xs-24 ant-col-sm-16">
|
{card.type === 'text' &&
|
<Input style={{marginTop: '4px'}} defaultValue={card.initval} />
|
}
|
{card.type === 'number' &&
|
<InputNumber defaultValue={card.initval} precision={card.decimal} />
|
}
|
{(card.type === 'multiselect' || card.type === 'select' || card.type === 'link') &&
|
<Select defaultValue={selectval}></Select>
|
}
|
{card.type === 'date' &&
|
<DatePicker defaultValue={card.initval ? moment().subtract(card.initval, 'days') : null} />
|
}
|
{card.type === 'datemonth' ?
|
<MonthPicker defaultValue={card.initval ? moment().subtract(card.initval, 'month') : null} /> : null
|
}
|
{card.type === 'datetime' &&
|
<DatePicker showTime defaultValue={card.initval ? moment().subtract(card.initval, 'days') : null} />
|
// <DatePicker showTime defaultValue={card.initval ? moment(card.initval, 'YYYY-MM-DD HH:mm:ss') : null} />
|
}
|
<div className="input-mask"></div>
|
</div>
|
</div>}
|
</div>
|
<Icon className="edit" type="edit" onClick={edit} />
|
<Icon className="edit close" type="close" onClick={close} />
|
</div>
|
)
|
}
|
export default Card
|