import React, { useState } from 'react'
|
import { useDrop } from 'react-dnd'
|
import update from 'immutability-helper'
|
import { is, fromJS } from 'immutable'
|
import { Tabs, Icon } from 'antd'
|
import Utils from '@/utils/utils.js'
|
import Card from './card'
|
import './index.scss'
|
|
const { TabPane } = Tabs
|
|
const Container = ({list, type, groupId, placeholder, handleList, handleMenu, deleteMenu }) => {
|
let target = null
|
const [cards, setCards] = useState(list)
|
const moveCard = (id, atIndex) => {
|
const { card, index } = findCard(id)
|
if (!card) return
|
|
const _cards = update(cards, { $splice: [[index, 1], [atIndex, 0, card]] })
|
handleList(type, _cards, card)
|
}
|
|
if (!is(fromJS(cards), fromJS(list))) {
|
setCards(list)
|
}
|
|
const findCard = id => {
|
const card = cards.filter(c => `${c.uuid}` === id)[0]
|
return {
|
card,
|
index: cards.indexOf(card),
|
}
|
}
|
|
const hasDrop = (item) => {
|
target = item
|
}
|
|
const [, drop] = useDrop({
|
accept: type,
|
drop(item) {
|
if (item.hasOwnProperty('originalIndex')) {
|
return
|
}
|
|
let newcard = {}
|
|
newcard.uuid = Utils.getuuid()
|
newcard.label = 'tab'
|
newcard.icon = ''
|
newcard.type = item.subType
|
newcard.linkTab = ''
|
newcard.subtabs = []
|
newcard.supMenu = 'mainTable'
|
newcard.groupId = groupId
|
newcard.focus = true
|
|
let targetId = cards.length > 0 ? cards[cards.length - 1].uuid : 0
|
if (target) {
|
targetId = target.uuid
|
}
|
|
const { index: overIndex } = findCard(`${targetId}`)
|
let targetIndex = overIndex
|
|
targetIndex++
|
|
const _cards = update(cards, { $splice: [[targetIndex, 0, newcard]] })
|
handleList(type, _cards, newcard)
|
target = null
|
}
|
})
|
|
const edit = (card) => {
|
handleMenu(card)
|
}
|
|
const del = (card) => {
|
deleteMenu({card: card, type: type})
|
}
|
|
return (
|
<div ref={drop} className="ant-row maintable-tab-list">
|
<Tabs defaultActiveKey="0">
|
{cards.map((card, index) => (
|
<TabPane tab={
|
<div key={card.uuid}>
|
<Card
|
key={card.uuid}
|
id={`${card.uuid}`}
|
type={type}
|
card={card}
|
moveCard={moveCard}
|
findCard={findCard}
|
hasDrop={hasDrop}
|
/>
|
<Icon className="edit" type="edit" onClick={() => edit(card)} />
|
<Icon className="edit close" type="close" onClick={() => del(card)} />
|
</div>
|
} key={`${index}`}>
|
《{card.label}》标签内容
|
</TabPane>
|
))}
|
</Tabs>
|
{cards.length === 0 ?
|
<div className="commontab-drawarea-placeholder">
|
{placeholder}
|
</div> : null
|
}
|
</div>
|
)
|
}
|
export default Container
|