import React from 'react'
|
import { useDrag, useDrop } from 'react-dnd'
|
import { Button, Select, DatePicker, Input, InputNumber, Popover } from 'antd'
|
import { EditOutlined, CopyOutlined, ProfileOutlined, CloseOutlined, UploadOutlined, TableOutlined } from '@ant-design/icons'
|
import moment from 'moment'
|
import ItemTypes from './itemtypes'
|
import './index.scss'
|
|
const { MonthPicker } = DatePicker
|
const { TextArea } = Input
|
|
const Card = ({ id, type, cols, card, moveCard, findCard, editCard, delCard, profileCard, copyCard }) => {
|
const originalIndex = findCard(id).index
|
const [{ isDragging }, drag] = useDrag({
|
item: { type: ItemTypes[type], id, originalIndex },
|
collect: monitor => ({
|
isDragging: monitor.isDragging(),
|
}),
|
})
|
const [, drop] = useDrop({
|
accept: ItemTypes[type],
|
canDrop: () => true,
|
drop: (item) => {
|
const { id: draggedId, originalIndex } = item
|
|
if (originalIndex === undefined) {
|
item.dropTargetId = id
|
} else if (draggedId && draggedId !== id) {
|
const { index: overIndex } = findCard(id)
|
moveCard(draggedId, overIndex)
|
}
|
}
|
})
|
const opacity = isDragging ? 0 : 1
|
|
const edit = () => {
|
editCard(id)
|
}
|
|
const del = () => {
|
delCard(id)
|
}
|
|
const copy = () => {
|
copyCard(id)
|
}
|
|
const profile = () => {
|
profileCard(id)
|
}
|
|
let _defaultValue = '' // 下拉搜索、时间范围类型,初始值需要预处理
|
|
if (type === 'search' && (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) {
|
_defaultValue = _option.Text || ''
|
} else {
|
_defaultValue = ''
|
}
|
} else if (card.setAll === 'true') {
|
_defaultValue = card.emptyText || '空'
|
}
|
} else if (type === 'search' && card.type === 'daterange') {
|
_defaultValue = [null, null]
|
if (card.initval === 'week') {
|
_defaultValue = [moment().startOf('week'), moment().endOf('week')]
|
} else if (card.initval === 'month') {
|
_defaultValue = [moment().startOf('month'), moment().endOf('month')]
|
} else if (card.initval) {
|
try {
|
let _initval = JSON.parse(card.initval)
|
_defaultValue = [moment().subtract(_initval[0], 'days'), moment().subtract(_initval[1], 'days')]
|
} catch (e) {
|
_defaultValue = [null, null]
|
}
|
}
|
}
|
|
let labelCol = 'ant-col-sm-8'
|
let wrapCol = 'ant-col-sm-16'
|
if (card.type === 'textarea') {
|
if (cols === '2') {
|
labelCol = 'textarea-line ant-col-sm-4'
|
wrapCol = 'textarea-line ant-col-sm-20'
|
} else if (cols === '3') {
|
labelCol = 'textarea-line ant-col-cuslabel'
|
wrapCol = 'textarea-line ant-col-cuswrap'
|
} else if (cols === '4') {
|
labelCol = 'textarea-line ant-col-sm-2'
|
wrapCol = 'textarea-line ant-col-sm-22'
|
}
|
}
|
|
return (
|
<Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
|
<div className="mk-popover-control">
|
<EditOutlined className="edit" title="编辑" onClick={edit} />
|
{type === 'search' ? <CopyOutlined className="edit copy" title="复制" onClick={copy} /> : null}
|
{type === 'action' && card.btnType !== 'cancel' ?
|
<ProfileOutlined className="edit profile" title="校验规则" onClick={profile} /> : null
|
}
|
{type === 'search' || (type === 'action' && card.btnType !== 'confirm' && card.btnType !== 'cancel') ? <CloseOutlined className="edit close" title="删除" onClick={del} /> : null}
|
</div>
|
} trigger="hover">
|
<div className="page-card" style={{ opacity: opacity}}>
|
<div ref={node => drag(drop(node))}>
|
{type === 'search' ?
|
<div className="ant-row ant-form-item">
|
<div className={'ant-col ant-form-item-label ant-col-xs-24 ' + labelCol}>
|
<label title={card.label}>{card.label}</label>
|
</div>
|
<div className={'ant-col ant-form-item-control-wrapper ant-col-xs-24 ' + wrapCol}>
|
{card.type === 'text' ?
|
<Input style={{marginTop: '4px'}} value={card.initval} /> : null
|
}
|
{card.type === 'number' ?
|
<InputNumber value={card.initval} precision={card.decimal} /> : null
|
}
|
{(card.type === 'multiselect' || card.type === 'select' || card.type === 'link') ?
|
<Select value={_defaultValue}></Select> : null
|
}
|
{card.type === 'date' ?
|
<DatePicker value={card.initval ? moment().subtract(card.initval, 'days') : null} /> : null
|
}
|
{card.type === 'datemonth' ?
|
<MonthPicker value={card.initval ? moment().subtract(card.initval, 'month') : null} /> : null
|
}
|
{card.type === 'datetime' ?
|
<DatePicker showTime value={card.initval ? moment().subtract(card.initval, 'days') : null} /> : null
|
}
|
{card.type === 'textarea' ?
|
<TextArea value={card.initval} autoSize={{ minRows: 2, maxRows: 6 }} /> : null
|
}
|
{card.type === 'funcvar' &&
|
<Input style={{marginTop: '4px'}} value={card.linkfield} />
|
}
|
{card.type === 'fileupload' ?
|
<Button>
|
<UploadOutlined /> 点击上传
|
</Button> : null
|
}
|
</div>
|
</div> : null
|
}
|
{type === 'action' ?
|
<Button
|
className={'mk-btn mk-' + card.class}
|
icon={card.icon}
|
key={card.uuid}
|
>
|
{card.label}{card.position === 'grid' && <TableOutlined />}
|
</Button> : null
|
}
|
</div>
|
</div>
|
</Popover>
|
)
|
}
|
export default Card
|