king
2019-11-13 8b9effb98612ee8a00d76d639a5733d12e9ecce6
src/components/sidemenu/comtableconfig/dragelement/index.jsx
@@ -2,11 +2,13 @@
import { useDrop } from 'react-dnd'
import update from 'immutability-helper'
import { Col } from 'antd'
import Utils from '@/utils/utils.js'
import Card from './card'
import ItemTypes from './itemtypes'
import './index.scss'
const Container = ({list, type, handleList, handleMenu }) => {
  let target = null
  const [cards, setCards] = useState(list)
  const moveCard = (id, atIndex) => {
    const { card, index } = findCard(id)
@@ -27,7 +29,75 @@
    const { card } = findCard(id)
    handleMenu(card)
  }
  const [, drop] = useDrop({ accept: ItemTypes[type] })
  const hasDrop = (item) => {
    target = item
  }
  const [, drop] = useDrop({
    accept: ItemTypes[type],
    drop(item) {
      if (item.hasOwnProperty('originalIndex')) {
        return
      }
      let newcard = {}
      if (item.type === 'search') {
        newcard.uuid = Utils.getuuid()
        newcard.label = 'fieldName'
        newcard.field = 'field'
        newcard.initval = ''
        newcard.type = item.subType
        if (item.subType === 'select') {
          newcard.resourceType = 0
          newcard.options = []
          newcard.dataSource = ''
        }
      } else if (item.type === 'action') {
        newcard.uuid = Utils.getuuid()
        newcard.label = 'button'
        newcard.func = ''
        newcard.Ot = 'notRequired'
        newcard.OpenType = item.subType
        newcard.icon = 'plus'
        newcard.class = 'green'
      } else if (item.type === 'columns') {
        newcard.uuid = Utils.getuuid()
        newcard.Align = 'left'
        newcard.label = 'fieldName'
        newcard.field = 'field'
        newcard.Hide = 'false'
        newcard.IsSort = item.subType
        newcard.Width = 120
      }
      let indexes = cards.map(car => {return car.id})
      let newid = 0
      while (indexes.includes(newid)) {
        newid++
      }
      newcard.id = newid
      let targetId = indexes.length > 0 ? indexes[indexes.length - 1] : 0
      if (target) {
        targetId = target.id
      }
      const { index: overIndex } = findCard(`${targetId}`)
      let targetIndex = overIndex
      if (!target) {
        targetIndex++
      }
      if (targetIndex < 0) {
        targetIndex = 0
      }
      const _cards = update(cards, { $splice: [[targetIndex, 0, newcard]] })
      setCards(_cards)
      handleList({[type]: _cards})
      target = null
    }
  })
  // const [, drop] = useDrop({ accept: ItemTypes[type] })
  return (
    <div ref={drop} className="ant-row" style={type === 'columns' ? {display: 'flex'} : {}}>
      {type === 'action' && cards.map(card => (
@@ -39,6 +109,7 @@
          moveCard={moveCard}
          editCard={editCard}
          findCard={findCard}
          hasDrop={hasDrop}
        />
      ))}
      {type === 'search' && cards.map(card => (
@@ -51,6 +122,7 @@
            moveCard={moveCard}
            editCard={editCard}
            findCard={findCard}
            hasDrop={hasDrop}
          />
        </Col>
      ))}
@@ -67,6 +139,7 @@
          moveCard={moveCard}
          editCard={editCard}
          findCard={findCard}
          hasDrop={hasDrop}
        />
      ))}
    </div>