import React, { useState } from 'react'
|
import { useDrop } from 'react-dnd'
|
import update from 'immutability-helper'
|
import { Modal } from 'antd'
|
|
import Utils from '@/utils/utils.js'
|
import Card from './card'
|
import './index.scss'
|
|
const { confirm } = Modal
|
|
const Container = ({menu, handleList }) => {
|
const [cards, setCards] = useState(menu.components)
|
|
if (menu.components.length > cards.length) {
|
setCards(menu.components)
|
}
|
|
const findCard = id => {
|
const card = cards.filter(c => `${c.uuid}` === id)[0]
|
return {
|
card,
|
index: cards.indexOf(card),
|
}
|
}
|
|
const updateConfig = (element) => {
|
const _cards = cards.map(item => item.uuid === element.uuid ? element : item)
|
handleList({...menu, components: _cards})
|
setCards(_cards)
|
}
|
|
const deleteCard = (id) => {
|
const { card } = findCard(id)
|
|
let hasComponent = false
|
if (card.type === 'tabs') {
|
card.subtabs.forEach(tab => {
|
if (tab.components.length > 0) {
|
hasComponent = true
|
}
|
})
|
}
|
|
confirm({
|
title: `确定删除《${card.name}》吗?`,
|
content: hasComponent ? '当前组件中含有子组件!' : '',
|
onOk() {
|
const _cards = cards.filter(item => item.uuid !== card.uuid)
|
handleList({...menu, components: _cards})
|
setCards(_cards)
|
},
|
onCancel() {}
|
})
|
}
|
|
const [, drop] = useDrop({
|
accept: 'menu',
|
drop(item) {
|
if (item.hasOwnProperty('originalIndex') || item.added) {
|
delete item.added // 删除组件添加标记
|
return
|
}
|
|
let newcard = {
|
uuid: Utils.getuuid(),
|
type: item.component,
|
subtype: item.subtype,
|
config: item.config,
|
width: item.width || 24,
|
isNew: true // 新添加标志,用于初始化
|
}
|
|
let targetId = ''
|
|
if (item.dropTargetId) {
|
targetId = item.dropTargetId
|
delete item.dropTargetId
|
} else if (cards.length > 0) {
|
targetId = cards.slice(-1)[0].uuid
|
}
|
|
const { index: overIndex } = findCard(`${targetId}`)
|
const _cards = update(cards, { $splice: [[overIndex + 1, 0, newcard]] })
|
|
handleList({...menu, components: _cards})
|
setCards(_cards)
|
}
|
})
|
|
return (
|
<div ref={drop} className="table-shell-inner" style={menu.style}>
|
<div className="ant-row">
|
{cards.map(card => (
|
<Card
|
id={card.uuid}
|
key={card.uuid}
|
card={card}
|
delCard={deleteCard}
|
findCard={findCard}
|
updateConfig={updateConfig}
|
/>
|
))}
|
</div>
|
</div>
|
)
|
}
|
export default Container
|