Skip to content

Commit

Permalink
Fix: drag box showing on single click
Browse files Browse the repository at this point in the history
  • Loading branch information
forabi committed Mar 22, 2019
1 parent 354606c commit 63bcb49
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/components/TimeGridScheduler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export const TimeGridScheduler = React.memo(function TimeGridScheduler({
const dateRanges = cellInfoToDateRanges(cell);
const event = dateRanges;
setPendingCreation(event);
}, [box, grid, cellInfoToDateRanges, toY, setPendingCreation]);
}, [box, grid, cellInfoToDateRanges, toY]);

useEffect(() => {
if (disabled) {
Expand Down
32 changes: 19 additions & 13 deletions src/hooks/useClickAndDrag.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import isEqual from 'lodash/isEqual';
import React, { useCallback, useEffect, useState } from 'react';
import { fromEvent, merge, of } from 'rxjs';
import { fromEvent, merge, Observable, of } from 'rxjs';
import {
delay,
distinctUntilChanged,
filter,
map,
mergeMap,
Expand All @@ -23,7 +21,7 @@ export function useClickAndDrag(
ref: React.RefObject<HTMLElement>,
isDisabled?: boolean,
) {
const [style, setStyle] = useState({
const [style, setStyle] = useState<React.CSSProperties>({
transform: 'translate(0, 0)',
width: 0,
height: 0,
Expand Down Expand Up @@ -88,7 +86,7 @@ export function useClickAndDrag(

const move$ = merge(mouseMove$, touchMove$).pipe(map(mapCoordsToContainer));

const box$ = dragStart$.pipe(
const box$: Observable<Rect | null> = dragStart$.pipe(
tap(() => {
setIsDragging(true);
setHasFinishedDragging(false);
Expand Down Expand Up @@ -121,19 +119,27 @@ export function useClickAndDrag(
};
},
),
filter(({ width }) => width >= 5),
takeUntil(dragEnd$),
);
}),
distinctUntilChanged(isEqual),
map(rect => {
return rect.width < 5 ? null : rect;
}),
);

const style$ = box$.pipe(
map(({ top, left, width, height }) => ({
transform: `translate(${left}px, ${top}px)`,
width,
height,
})),
map(rect => {
if (rect !== null) {
const { width, height, left, top } = rect;
return {
transform: `translate(${left}px, ${top}px)`,
width,
height,
};
}

return { display: 'none' };
}),
);

const boxSubscriber = box$.subscribe(setBox);
Expand All @@ -149,7 +155,7 @@ export function useClickAndDrag(
setIsDragging(false);
setHasFinishedDragging(false);
setBox(null);
}, [setBox]);
}, []);

return { style, box, isDragging, cancel, hasFinishedDragging };
}

0 comments on commit 63bcb49

Please sign in to comment.