Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a solution to prevent single click while double clicking #683

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/libs/components/src/map/BoundsDriftMarker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import ReactLeafletDriftMarker from 'react-leaflet-drift-marker';
import { MarkerProps, Bounds } from './Types';
import L, { LeafletMouseEvent, LatLngBounds } from 'leaflet';

import { debounce } from 'lodash';

export interface BoundsDriftMarkerProps extends MarkerProps {
bounds: Bounds;
duration: number;
Expand Down Expand Up @@ -280,6 +282,8 @@ export default function BoundsDriftMarker({

// add click events for highlighting markers
const handleClick = (e: LeafletMouseEvent) => {
//DKDK left this for checking purpose: will remove later
console.log('e.originalEvent.detail =', e.originalEvent.detail);
// check the number of mouse click and enable function for single click only
if (e.originalEvent.detail === 1) {
if (setSelectedMarkers) {
Expand Down Expand Up @@ -310,6 +314,9 @@ export default function BoundsDriftMarker({
}
};

// debounce single click to be prevented when double clicking marker
const debounceSingleClick = debounce(handleClick, 300);

// set this marker as highlighted
if (icon && selectedMarkers?.find((id) => id === props.id))
icon.options.className += ' highlight-marker';
Expand All @@ -335,7 +342,8 @@ export default function BoundsDriftMarker({
position={position}
// new way to handle mouse events
eventHandlers={{
click: (e: LeafletMouseEvent) => handleClick(e),
// debounce single click to be prevented when double clicking marker
click: (e: LeafletMouseEvent) => debounceSingleClick(e),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would save a few bytes! (same for the other handlers)

Suggested change
click: (e: LeafletMouseEvent) => debounceSingleClick(e),
click: debounceSingleClick,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bobular Thank you for your review and tests 👍 Yes they can be simplified as you noticed: thanks! I addressed your feedbacks and removed the console.log I made for test purpose.

mouseover: (e: LeafletMouseEvent) => handleMouseOver(e),
mouseout: (e: LeafletMouseEvent) => handleMouseOut(e),
dblclick: handleDoubleClick,
Expand Down
Loading