-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathlocation-control.js
More file actions
96 lines (88 loc) · 2.55 KB
/
location-control.js
File metadata and controls
96 lines (88 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* External dependencies
*/
import { i18n, isPro } from 'stackable'
import { DynamicContentControl, useDynamicContentControlProps } from '~stackable/components'
/**
* WordPress dependencies
*/
import { TextControl, BaseControl as GutBaseControl } from '@wordpress/components'
import { __ } from '@wordpress/i18n'
import {
useState, useEffect, useRef,
} from '@wordpress/element'
/**
* Location Control
*
* @param {Object} props Component props
*/
const LocationControl = props => {
const [ waitForGoogle, setWaitForGoogle ] = useState( 0 )
const dynamicContentProps = useDynamicContentControlProps( {
value: props.value,
onChange: value => {
props.onTextChange( value )
},
isFormatType: false,
} )
useEffect( () => {
if ( ! window?.google?.maps ) {
const interval = setInterval( () => {
if ( window.google && window.google.maps ) {
clearInterval( interval )
setWaitForGoogle( waitForGoogle + 1 )
}
}, 250 )
return () => clearInterval( interval )
}
}, [] )
const ref = useRef()
const autocompleteRef = useRef()
useEffect( () => {
if ( ref.current && window.google && window.google.maps ) {
// eslint-disable-next-line no-undef
const autocomplete = autocompleteRef.current = new google.maps.places.Autocomplete( ref.current )
autocomplete.setFields( [
'place_id',
'geometry',
'formatted_address',
] )
autocomplete.addListener( 'place_changed', () => {
const place = autocomplete.getPlace()
if ( place.geometry ) {
props.onPlaceChange( place )
}
} )
window.autocomplete = autocomplete
}
}, [ ref.current, waitForGoogle ] )
return (
<GutBaseControl
className="stk-control stk-control__location-control"
label={ __( 'Location', i18n ) }
help={ ! isPro
? __( 'Type in a pair of latitude longitude coordinates. You can also type in the name of the location if your API Key has Geocoding API and Places API enabled.', i18n )
: __( 'Type in a pair of latitude longitude coordinates. You can also type in the name of the location if your API Key has Geocoding API and Places API enabled. Dynamic Content only allows latitude longtitude coordinates', i18n )
}
>
<DynamicContentControl
enable={ true }
hasPanelModifiedIndicator={ true }
{ ...dynamicContentProps }
>
<TextControl
ref={ ref }
value={ props.value }
onChange={ value => {
props.onTextChange( value )
} }
/>
</DynamicContentControl>
</GutBaseControl>
)
}
LocationControl.defaultProps = {
onChange: null,
value: '',
}
export default LocationControl