chowa
Version:
UI component library based on React
71 lines (70 loc) • 2.44 kB
JavaScript
/**
* @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.
*/
;
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-row');
const sourceSpec = {
beginDrag(props) {
return {
renderDataIndex: props.renderDataIndex
};
}
};
const sourceCollect = (connect, monitor) => {
return {
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging()
};
};
const targetSpec = {
drop(props, monitor) {
const dragIndex = monitor.getItem().renderDataIndex;
const hoverIndex = props.renderDataIndex;
if (dragIndex === hoverIndex) {
return;
}
props.rowDragSorter(dragIndex, hoverIndex);
}
};
const targetCollect = (connect, monitor) => {
const dragIndex = monitor.getItem()
? monitor.getItem().renderDataIndex
: undefined;
return {
connectDropTarget: connect.dropTarget(),
isOver: monitor.isOver(),
dragIndex
};
};
const TableRowDragWrapper = (props) => {
const { children, connectDragSource, connectDropTarget, className, dragIndex, renderDataIndex, isDragging, isOver } = props;
const attr = utils_1.omitProps(props, [
'children',
'connectDragSource',
'connectDropTarget',
'className',
'dragIndex',
'renderDataIndex',
'isDragging',
'isOver',
'rowDragSorter'
]);
const rowClass = classnames_1.default({
[utils_1.preClass('table-dragging')]: isDragging,
[utils_1.preClass('table-drop-over-up')]: isOver && dragIndex > renderDataIndex,
[utils_1.preClass('table-drop-over-down')]: isOver && dragIndex < renderDataIndex,
[className]: utils_1.isExist(className)
});
return connectDropTarget(connectDragSource(React.createElement("tr", Object.assign({}, attr, { className: rowClass }), children)));
};
exports.default = react_dnd_1.DropTarget(DragType, targetSpec, targetCollect)(react_dnd_1.DragSource(DragType, sourceSpec, sourceCollect)(TableRowDragWrapper));