Skip to content

Commit

Permalink
feat(Marker): add drag and dragstart events (#173)
Browse files Browse the repository at this point in the history
* feat(Marker): add drag and dragstart events (#173)
  • Loading branch information
boyur authored and stepankuzmin committed Mar 31, 2019
1 parent e77ba81 commit 87313ed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/components/Marker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ type Props = {
draggable?: boolean,

/** Fired when the marker is finished being dragged */
onDragEnd?: (lngLat: LngLat) => any
onDragEnd?: (lngLat: LngLat) => any,

/** Fired when the marker is finished being dragged */
onDragStart?: (lngLat: LngLat) => any,

/** Fired when the marker is dragged */
onDrag?: (lngLat: LngLat) => any
};

class Marker extends PureComponent<Props> {
Expand All @@ -56,7 +62,15 @@ class Marker extends PureComponent<Props> {
}

componentDidMount() {
const { longitude, latitude, offset, draggable, onDragEnd } = this.props;
const {
longitude,
latitude,
offset,
draggable,
onDragEnd,
onDragStart,
onDrag
} = this.props;

this._marker = new mapboxgl.Marker(this._el, {
draggable,
Expand All @@ -68,6 +82,14 @@ class Marker extends PureComponent<Props> {
if (onDragEnd) {
this._marker.on('dragend', this._onDragEnd);
}

if (onDragStart) {
this._marker.on('dragstart', this._onDragStart);
}

if (onDrag) {
this._marker.on('drag', this._onDrag);
}
}

componentDidUpdate(prevProps: Props) {
Expand Down Expand Up @@ -97,6 +119,16 @@ class Marker extends PureComponent<Props> {
this.props.onDragEnd(this._marker.getLngLat());
};

_onDragStart = (): void => {
// $FlowFixMe
this.props.onDragStart(this._marker.getLngLat());
};

_onDrag = (): void => {
// $FlowFixMe
this.props.onDrag(this._marker.getLngLat());
};

render() {
return createElement(MapContext.Consumer, {}, (map) => {
if (map) {
Expand Down
4 changes: 4 additions & 0 deletions src/components/Marker/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import MapGL, { Marker } from '../..';
const Element = <div>ok</div>;
test('render', () => {
const onDragEnd = jest.fn();
const onDragStart = jest.fn();
const onDrag = jest.fn();

const wrapper = mount(
<MapGL latitude={0} longitude={0} zoom={0}>
Expand All @@ -15,6 +17,8 @@ test('render', () => {
latitude={0}
element={Element}
onDragEnd={onDragEnd}
onDragStart={onDragStart}
onDrag={onDrag}
/>
</MapGL>
);
Expand Down

0 comments on commit 87313ed

Please sign in to comment.