Skip to content

Commit

Permalink
dnd: fix drop on time-gutter
Browse files Browse the repository at this point in the history
previously dropping on the time-gutter leaves calendar in drag state.

fix is to add the ability of specific drop targets and cancel/reset drag-
and-drop if the drop was not on an allowed drop target.

streamlined with [1].

[1]: jquense/react-big-calendar#1901
  • Loading branch information
ktomk committed May 19, 2021
1 parent 497267a commit 193db92
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/addons/dragAndDrop/WeekWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,13 @@ class WeekWrapper extends React.Component {
_selectable = () => {
let node = findDOMNode(this).closest('.rbc-month-row, .rbc-allday-cell');
let container = node.closest('.rbc-month-view, .rbc-time-view');
let allowedTargets = container.querySelectorAll(
'.rbc-day-slot, .rbc-allday-cell'
);

let selector = (this._selector = new Selection(() => container));
let selector = (this._selector = new Selection(() => container, {
allowedTargets: () => allowedTargets,
}));

selector.on('beforeSelect', point => {
const { isAllDay } = this.props;
Expand Down
19 changes: 17 additions & 2 deletions src/components/selection/Selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ const clickInterval = 250;

class Selection {

constructor(node, { global = false, longPressThreshold = 250 } = {}) {
constructor(node, { global = false, longPressThreshold = 250, allowedTargets = () => [] } = {}) {
this.isDetached = false;
this.container = node;
this.globalMouse = !node || global;
this.longPressThreshold = longPressThreshold;
this.getAllowedTargets = allowedTargets;
this._listeners = Object.create(null);
this._handleInitialEvent = this._handleInitialEvent.bind(this);
this._handleMoveEvent = this._handleMoveEvent.bind(this);
Expand Down Expand Up @@ -311,6 +312,7 @@ class Selection {
if (!this._initialEventData) return;

let inRoot = !this.container || contains(this.container(), e.target);
let inAllowedTarget = this._checkIfInAllowedTarget(e);
let bounds = this._selectRect;
let click = this.isClick(pageX, pageY);

Expand All @@ -320,7 +322,7 @@ class Selection {
return this.emit('reset');
}

if (!inRoot) {
if (!inRoot || !inAllowedTarget) {
return this.emit('reset');
}

Expand All @@ -332,6 +334,19 @@ class Selection {
if (!click) return this.emit('select', bounds);
}

_checkIfInAllowedTarget(e) {
let allowedTargets = this.getAllowedTargets();
if (!allowedTargets || !allowedTargets.length) return true;
let inAllowedTarget = false;
allowedTargets.forEach(n => {
if (contains(n, e.target)) {
inAllowedTarget = true;
}
});

return inAllowedTarget;
}

_handleClickEvent(e) {
const { pageX, pageY, clientX, clientY } = getEventCoordinates(e);
const now = new Date().getTime();
Expand Down

0 comments on commit 193db92

Please sign in to comment.