-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[google-maps-input] Componentize the different aspects of the map
- Loading branch information
Showing
7 changed files
with
299 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"rules": { | ||
"react/prop-types": "off" | ||
"react/prop-types": "off", | ||
"complexity": ["warn", 15] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@import 'part:@sanity/base/theme/variables-style'; | ||
|
||
.map { | ||
height: 100%; | ||
width: 100%; | ||
box-sizing: border-box; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import React from 'react' | ||
import {LatLng} from '../types' | ||
import styles from './Map.css' | ||
|
||
interface MapProps { | ||
api: typeof window.google.maps | ||
location: LatLng | ||
defaultZoom?: number | ||
onClick?: (event: google.maps.MouseEvent) => void | ||
children?: (map: google.maps.Map) => React.ReactElement | ||
} | ||
|
||
interface MapState { | ||
map: google.maps.Map | undefined | ||
} | ||
|
||
export class GoogleMap extends React.Component<MapProps, MapState> { | ||
static defaultProps = { | ||
defaultZoom: 8 | ||
} | ||
|
||
state: MapState = {map: undefined} | ||
clickHandler: google.maps.MapsEventListener | undefined | ||
mapRef = React.createRef<HTMLDivElement>() | ||
mapEl: HTMLDivElement | null = null | ||
|
||
componentDidMount() { | ||
this.attachClickHandler() | ||
} | ||
|
||
attachClickHandler() { | ||
const map = this.state.map | ||
if (!map) { | ||
return | ||
} | ||
|
||
const {api, onClick} = this.props | ||
const {event} = api | ||
|
||
if (this.clickHandler) { | ||
this.clickHandler.remove() | ||
} | ||
|
||
if (onClick) { | ||
this.clickHandler = event.addListener(map, 'click', onClick) | ||
} | ||
} | ||
|
||
componentDidUpdate(prevProps: MapProps) { | ||
const map = this.state.map | ||
if (!map) { | ||
return | ||
} | ||
|
||
if (prevProps.onClick !== this.props.onClick) { | ||
this.attachClickHandler() | ||
} | ||
|
||
if (prevProps.location !== this.props.location) { | ||
map.panTo(this.getCenter()) | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
if (this.clickHandler) { | ||
this.clickHandler.remove() | ||
} | ||
} | ||
|
||
getCenter(): google.maps.LatLng { | ||
const {location, api} = this.props | ||
return new api.LatLng(location.lat, location.lng) | ||
} | ||
|
||
constructMap(el: HTMLDivElement) { | ||
const {defaultZoom, api} = this.props | ||
const GMap = api.Map | ||
const geoPoint = this.getCenter() | ||
const options = { | ||
zoom: defaultZoom, | ||
center: geoPoint | ||
} | ||
|
||
return new GMap(el, options) | ||
} | ||
|
||
setMapElement = (element: HTMLDivElement | null) => { | ||
if (element && element !== this.mapEl) { | ||
const map = this.constructMap(element) | ||
this.setState({map}) | ||
} | ||
|
||
this.mapEl = element | ||
} | ||
|
||
render() { | ||
const {children} = this.props | ||
const {map} = this.state | ||
return ( | ||
<> | ||
<div ref={this.setMapElement} className={styles.map} /> | ||
{children && map && children(map)} | ||
</> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import * as React from 'react' | ||
import {LatLng} from '../types' | ||
|
||
interface Props { | ||
api: typeof window.google.maps | ||
map: google.maps.Map | ||
onMove?: (event: google.maps.MouseEvent) => void | ||
position: LatLng | google.maps.LatLng | ||
zIndex?: number | ||
opacity?: number | ||
label?: string | ||
} | ||
|
||
export class Marker extends React.Component<Props> { | ||
marker: google.maps.Marker | undefined | ||
|
||
moveHandler: google.maps.MapsEventListener | undefined | ||
|
||
componentDidMount() { | ||
const {position, api, map, onMove, zIndex, opacity, label} = this.props | ||
const {Marker: GMarker} = api | ||
this.marker = new GMarker({ | ||
position, | ||
map, | ||
draggable: Boolean(onMove), | ||
zIndex, | ||
opacity, | ||
label | ||
}) | ||
|
||
this.attachMoveHandler() | ||
} | ||
|
||
componentDidUpdate(prevProps: Props) { | ||
if (!this.marker) { | ||
return | ||
} | ||
|
||
const {position, onMove, label, zIndex, opacity} = this.props | ||
|
||
if (prevProps.onMove !== onMove) { | ||
this.attachMoveHandler() | ||
} | ||
|
||
if (prevProps.position !== position) { | ||
this.marker.setPosition(position) | ||
} | ||
|
||
if (prevProps.label !== label) { | ||
this.marker.setLabel(label || null) | ||
} | ||
|
||
if (prevProps.zIndex !== zIndex) { | ||
this.marker.setZIndex(zIndex || null) | ||
} | ||
|
||
if (prevProps.opacity !== opacity) { | ||
this.marker.setOpacity(opacity || null) | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
if (this.moveHandler) { | ||
this.moveHandler.remove() | ||
} | ||
} | ||
|
||
attachMoveHandler() { | ||
const {api, onMove} = this.props | ||
|
||
if (this.moveHandler) { | ||
this.moveHandler.remove() | ||
} | ||
|
||
if (this.marker && onMove) { | ||
this.moveHandler = api.event.addListener(this.marker, 'dragend', onMove) | ||
} | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
render() { | ||
return null | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/@sanity/google-maps-input/src/map/SearchInput.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@import 'part:@sanity/base/theme/variables-style'; | ||
|
||
.wrapper { | ||
position: absolute; | ||
right: 10px; | ||
top: 10px; | ||
width: 220px; | ||
} | ||
|
||
.input { | ||
composes: textInput from 'part:@sanity/base/theme/forms/text-input-style'; | ||
} | ||
|
||
:global(.pac-container) { | ||
z-index: calc(var(--zindex-modal) + 1); | ||
} |
Oops, something went wrong.