UNPKG

3.16 kBJavaScriptView Raw
1// Example to test the React Reconciler. This example
2// is 100x faster in development mode,
3// and 1.5-2x faster with NODE_ENV==='production'
4
5// The idea was to not draw map children on hovers, but subscribe inside children on hover change
6// see ./markers/ReactiveMarker source
7import React from 'react';
8import {
9 compose,
10 defaultProps,
11 withHandlers,
12 withState,
13 withProps,
14 withPropsOnChange,
15} from 'recompose';
16import { createSelector } from 'reselect';
17
18import { susolvkaCoords, generateMarkers } from './data/fakeData';
19
20import GoogleMapReact from '../src';
21// import SimpleMarker from './markers/SimpleMarker';
22import ReactiveMarker from './markers/ReactiveMarker';
23
24import ptInBounds from './utils/ptInBounds';
25import props2Stream from './utils/props2Stream';
26import withStateSelector from './utils/withStateSelector';
27import { GOOGLE_API_KEY } from './config/Google_API_key';
28
29export const gMap = (
30 {
31 style,
32 hoverDistance,
33 options,
34 mapParams: { center, zoom },
35 onChange,
36 onChildMouseEnter,
37 onChildMouseLeave,
38 markers,
39 draggable,
40 }
41) => (
42 <GoogleMapReact
43 bootstrapURLKeys={{
44 key: GOOGLE_API_KEY,
45 }}
46 style={style}
47 options={options}
48 draggable={draggable}
49 hoverDistance={hoverDistance}
50 zoom={zoom}
51 center={center}
52 onChange={onChange}
53 onChildMouseEnter={onChildMouseEnter}
54 onChildMouseLeave={onChildMouseLeave}
55 experimental
56 >
57 {markers}
58 </GoogleMapReact>
59);
60
61export const gMapHOC = compose(
62 defaultProps({
63 clusterRadius: 60,
64 hoverDistance: 30,
65 options: {
66 minZoom: 3,
67 maxZoom: 15,
68 },
69 style: {
70 position: 'relative',
71 margin: 0,
72 padding: 0,
73 flex: 1,
74 },
75 }),
76 // withState so you could change markers if you want
77 withStateSelector('markers', 'setMarkers', () =>
78 createSelector(
79 ({ route: { markersCount = 20 } }) => markersCount,
80 markersCount => generateMarkers(markersCount)
81 )),
82 withState('hoveredMarkerId', 'setHoveredMarkerId', -1),
83 withState('mapParams', 'setMapParams', { center: susolvkaCoords, zoom: 6 }),
84 // describe events
85 withHandlers({
86 onChange: ({ setMapParams }) =>
87 ({ center, zoom, bounds }) => {
88 setMapParams({ center, zoom, bounds });
89 },
90 onChildMouseEnter: ({ setHoveredMarkerId }) =>
91 (hoverKey, { id }) => {
92 setHoveredMarkerId(id);
93 },
94 onChildMouseLeave: ({ setHoveredMarkerId }) =>
95 () => {
96 setHoveredMarkerId(-1);
97 },
98 }),
99 withPropsOnChange(['markers', 'mapParams'], ({
100 markers,
101 mapParams: { bounds },
102 }) => ({
103 markers: bounds ? markers.filter(m => ptInBounds(bounds, m)) : [],
104 })),
105 withProps(({ hoveredMarkerId }) => ({
106 draggable: hoveredMarkerId === -1,
107 })),
108 props2Stream('hoveredMarkerId'),
109 withPropsOnChange(['markers', 'hoveredMarkerId$'], ({
110 markers,
111 hoveredMarkerId$,
112 }) => ({
113 markers: markers.map(({ ...markerProps, id }) => (
114 <ReactiveMarker
115 key={id}
116 id={id}
117 hoveredMarkerId$={hoveredMarkerId$}
118 {...markerProps}
119 />
120 )),
121 }))
122);
123
124export default gMapHOC(gMap);