UNPKG

2.83 kBJavaScriptView Raw
1import React from 'react';
2import PropTypes from 'prop-types';
3import {
4 compose,
5 defaultProps,
6 withHandlers,
7 withState,
8 withContext,
9 withProps,
10 withPropsOnChange,
11} from 'recompose';
12import { createSelector } from 'reselect';
13
14import { londonCoords, generateMarkers } from './data/fakeData';
15
16import GoogleMapReact from '../src';
17import SimpleMarker from './markers/SimpleMarker';
18
19import ptInBounds from './utils/ptInBounds';
20import withStateSelector from './utils/withStateSelector';
21
22export const gMap = (
23 {
24 style,
25 hoverDistance,
26 options,
27 mapParams: { center, zoom },
28 onChange,
29 onChildMouseEnter,
30 onChildMouseLeave,
31 markers,
32 draggable, // hoveredMarkerId,
33 }
34) => (
35 <GoogleMapReact
36 bootstrapURLKeys={{
37 key: 'AIzaSyBMqz4ueWMfGGqdXlvwE_cIVfar60GROi8',
38 }}
39 style={style}
40 options={options}
41 draggable={draggable}
42 hoverDistance={hoverDistance}
43 zoom={zoom}
44 center={center}
45 onChange={onChange}
46 onChildMouseEnter={onChildMouseEnter}
47 onChildMouseLeave={onChildMouseLeave}
48 layerTypes={
49 zoom > 12
50 ? []
51 : zoom > 10 ? ['TrafficLayer'] : ['TrafficLayer', 'TransitLayer']
52 }
53 >
54 {markers}
55 </GoogleMapReact>
56);
57
58export const gMapHOC = compose(
59 defaultProps({
60 clusterRadius: 60,
61 hoverDistance: 30,
62 options: {
63 minZoom: 3,
64 maxZoom: 15,
65 },
66 style: {
67 position: 'relative',
68 margin: 0,
69 padding: 0,
70 flex: 1,
71 },
72 }),
73 withContext({ hello: PropTypes.string }, () => ({ hello: 'world' })),
74 // withState so you could change markers if you want
75 withStateSelector('markers', 'setMarkers', () =>
76 createSelector(
77 ({ route: { markersCount = 20 } }) => markersCount,
78 markersCount => generateMarkers(markersCount)
79 )),
80 withState('hoveredMarkerId', 'setHoveredMarkerId', -1),
81 withState('mapParams', 'setMapParams', { center: londonCoords, zoom: 9 }),
82 // describe events
83 withHandlers({
84 onChange: ({ setMapParams }) =>
85 ({ center, zoom, bounds }) => {
86 setMapParams({ center, zoom, bounds });
87 },
88 onChildMouseEnter: ({ setHoveredMarkerId }) =>
89 (hoverKey, { id }) => {
90 setHoveredMarkerId(id);
91 },
92 onChildMouseLeave: ({ setHoveredMarkerId }) =>
93 () => {
94 setHoveredMarkerId(-1);
95 },
96 }),
97 withPropsOnChange(['markers', 'mapParams'], ({
98 markers,
99 mapParams: { bounds },
100 }) => ({
101 markers: bounds ? markers.filter(m => ptInBounds(bounds, m)) : [],
102 })),
103 withProps(({ hoveredMarkerId }) => ({
104 draggable: hoveredMarkerId === -1,
105 })),
106 withPropsOnChange(['markers'], ({ markers }) => ({
107 markers: markers.map(({ ...markerProps, id }) => (
108 <SimpleMarker key={id} id={id} {...markerProps} />
109 )),
110 }))
111);
112
113export default gMapHOC(gMap);