UNPKG

chowa

Version:

UI component library based on React

83 lines (82 loc) 2.91 kB
/** * @license chowa v1.1.3 * * Copyright (c) Chowa Techonlogies Co.,Ltd.(http://www.chowa.cn). * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const React = require("react"); const classnames_1 = require("classnames"); const react_dnd_1 = require("react-dnd"); const utils_1 = require("../utils"); const DragType = utils_1.preClass('table-column'); const sourceSpec = { beginDrag(props) { return { index: props.index, parentIndexs: props.parentIndexs }; } }; const sourceCollect = (connect, monitor) => { return { connectDragSource: connect.dragSource(), isDragging: monitor.isDragging() }; }; const targetSpec = { canDrop(props, monitor) { const item = monitor.getItem(); if (!utils_1.isEqual(props.parentIndexs, item.parentIndexs)) { return false; } return true; }, drop(props, monitor) { const dragIndex = monitor.getItem().index; const hoverIndex = props.index; if (dragIndex === hoverIndex) { return; } props.columnDragSorter(dragIndex, hoverIndex, props.parentIndexs); } }; const targetCollect = (connect, monitor) => { const item = monitor.getItem(); const dragIndex = item ? item.index : undefined; const dragParentIndexs = item ? item.parentIndexs : undefined; return { connectDropTarget: connect.dropTarget(), isOver: monitor.isOver(), dragIndex, dragParentIndexs }; }; const TableColumnDragWrapper = (props) => { const { children, connectDragSource, connectDropTarget, className, dragIndex, dragParentIndexs, parentIndexs, index, isDragging, isOver } = props; const attr = utils_1.omitProps(props, [ 'children', 'connectDragSource', 'connectDropTarget', 'className', 'dragIndex', 'dragParentIndexs', 'parentIndexs', 'index', 'isDragging', 'isOver', 'columnDragSorter' ]); const isSameNode = utils_1.isEqual(dragParentIndexs, parentIndexs); const rowClass = classnames_1.default({ [utils_1.preClass('table-dragging')]: isDragging, [utils_1.preClass('table-drop-over-left')]: isOver && isSameNode && dragIndex > index, [utils_1.preClass('table-drop-over-right')]: isOver && isSameNode && dragIndex < index, [className]: utils_1.isExist(className) }); return connectDropTarget(connectDragSource(React.createElement("th", Object.assign({}, attr, { className: rowClass }), children))); }; exports.default = react_dnd_1.DropTarget(DragType, targetSpec, targetCollect)(react_dnd_1.DragSource(DragType, sourceSpec, sourceCollect)(TableColumnDragWrapper));