UNPKG

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