@entando/ddtable
Version:
react components used to render drag and drop tables in Entando projects
145 lines (119 loc) • 5.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.collect = exports.dragSource = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _reactDndHtml5Backend = require('react-dnd-html5-backend');
var _reactDnd = require('react-dnd');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
// Specifies the drag source contract.
var dragSource = exports.dragSource = {
canDrag: function canDrag() {
return true;
},
isDragging: function isDragging() {
return true;
},
beginDrag: function beginDrag(props, monitor, component) {
component.onDragBegin(props);
// Return the data describing the dragged item
return component.getRowData();
},
endDrag: function endDrag(props, monitor, component) {
component.onDragEnd(props);
}
};
// Specifies which props to inject into the component
var collect = exports.collect = function collect(connect, monitor) {
return {
connectDragSource: connect.dragSource(),
connectDragPreview: connect.dragPreview(),
isDragging: monitor.isDragging()
};
};
var DragHandle = function (_Component) {
_inherits(DragHandle, _Component);
function DragHandle(props, context) {
_classCallCheck(this, DragHandle);
var _this = _possibleConstructorReturn(this, (DragHandle.__proto__ || Object.getPrototypeOf(DragHandle)).call(this, props));
if (!context || !context.onDragBegin || !context.onDragEnd || !context.rowData) {
// eslint-disable-next-line no-console
console.error('Warning: ' + DragHandle.displayName + ' should only be used inside DDTable.Tr!');
}
return _this;
}
_createClass(DragHandle, [{
key: 'componentDidMount',
value: function componentDidMount() {
// Use empty image as a drag preview so browsers don't draw it
this.props.connectDragPreview((0, _reactDndHtml5Backend.getEmptyImage)(), {
// IE fallback: specify that we'd rather screenshot the node
// when it already knows it's being dragged so we can hide it with CSS.
captureDraggingState: true
});
}
}, {
key: 'onDragBegin',
value: function onDragBegin() {
this.context.onDragBegin(this.context.rowData);
}
}, {
key: 'onDragEnd',
value: function onDragEnd() {
this.context.onDragEnd(this.context.rowData);
}
}, {
key: 'getRowData',
value: function getRowData() {
return this.context.rowData;
}
}, {
key: 'render',
value: function render() {
var _props = this.props,
connectDragSource = _props.connectDragSource,
children = _props.children;
try {
return connectDragSource(_react2.default.Children.only(children));
} catch (e) {
return connectDragSource(_react2.default.createElement(
'button',
null,
_react2.default.createElement('i', { className: 'fa fa-ellipsis-v' }),
'\xA0',
_react2.default.createElement('i', { className: 'fa fa-ellipsis-v' })
));
}
}
}]);
return DragHandle;
}(_react.Component);
DragHandle.displayName = 'DDTable.Handle';
DragHandle.propTypes = {
connectDragSource: _propTypes2.default.func.isRequired,
connectDragPreview: _propTypes2.default.func.isRequired,
children: _propTypes2.default.node
};
DragHandle.defaultProps = {
children: _react2.default.createElement(
'button',
null,
_react2.default.createElement('i', { className: 'fa fa-ellipsis-v' }),
'\xA0',
_react2.default.createElement('i', { className: 'fa fa-ellipsis-v' })
)
};
DragHandle.contextTypes = {
onDragBegin: _propTypes2.default.func,
onDragEnd: _propTypes2.default.func,
rowData: _propTypes2.default.shape({})
};
exports.default = (0, _reactDnd.DragSource)('DDTable', dragSource, collect)(DragHandle);