@contentful/field-editor-location 2.2.2-canary.29 → 2.2.2-canary.sha-cf70e54f.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -68,11 +68,17 @@ const BerlinLocation = {
68
68
  lng: 13.41115439
69
69
  };
70
70
  function GoogleMapView(props) {
71
- const [marker, setMarker] = _react.useState();
72
- const [maps, setMaps] = _react.useState();
71
+ const [googleMapsState, setGoogleMapsState] = _react.useState({
72
+ marker: undefined,
73
+ maps: undefined
74
+ });
73
75
  const propsRef = _react.useRef(props);
74
76
  propsRef.current = props;
75
- _react.useEffect(()=>{
77
+ const googleMapsStateRef = _react.useRef(googleMapsState);
78
+ googleMapsStateRef.current = googleMapsState;
79
+ const pendingLoadedMapsRef = _react.useRef(null);
80
+ _react.useLayoutEffect(()=>{
81
+ const { marker, maps } = googleMapsState;
76
82
  if (marker && maps) {
77
83
  if (props.location) {
78
84
  const latLng = new maps.LatLng(props.location.lat, props.location.lng);
@@ -84,13 +90,14 @@ function GoogleMapView(props) {
84
90
  marker.setDraggable(!props.disabled);
85
91
  marker.setCursor(props.disabled ? 'not-allowed' : 'auto');
86
92
  }
87
- }, [
88
- marker,
89
- maps,
90
- props.disabled,
91
- props.location
92
- ]);
93
- const handleGoogleApiLoaded = (event)=>{
93
+ if (pendingLoadedMapsRef.current !== null && pendingLoadedMapsRef.current === maps) {
94
+ pendingLoadedMapsRef.current = null;
95
+ propsRef.current.onGoogleApiLoaded({
96
+ maps
97
+ });
98
+ }
99
+ });
100
+ const handleGoogleApiLoaded = _react.useCallback((event)=>{
94
101
  const { maps: loadedMaps, map } = event;
95
102
  const loadedMarker = new loadedMaps.Marker({
96
103
  map,
@@ -100,11 +107,12 @@ function GoogleMapView(props) {
100
107
  visible: Boolean(propsRef.current.location)
101
108
  });
102
109
  loadedMaps.event.addListener(map, 'click', (event)=>{
103
- if (propsRef.current.disabled) {
110
+ const { marker, maps } = googleMapsStateRef.current;
111
+ if (propsRef.current.disabled || !marker || !maps) {
104
112
  return;
105
113
  }
106
- loadedMarker.setPosition(event.latLng);
107
- loadedMarker.setVisible(true);
114
+ marker.setPosition(event.latLng);
115
+ marker.setVisible(true);
108
116
  propsRef.current.onChangeLocation({
109
117
  lat: event.latLng.lat(),
110
118
  lng: event.latLng.lng()
@@ -116,12 +124,12 @@ function GoogleMapView(props) {
116
124
  lng: event.latLng.lng()
117
125
  });
118
126
  });
119
- setMarker(loadedMarker);
120
- setMaps(loadedMaps);
121
- propsRef.current.onGoogleApiLoaded({
127
+ pendingLoadedMapsRef.current = loadedMaps;
128
+ setGoogleMapsState({
129
+ marker: loadedMarker,
122
130
  maps: loadedMaps
123
131
  });
124
- };
132
+ }, []);
125
133
  return /*#__PURE__*/ _react.createElement("div", {
126
134
  className: styles.root
127
135
  }, /*#__PURE__*/ _react.createElement(_googlemapreact.default, {
@@ -101,16 +101,14 @@ function LocationEditor({ disabled, value, setValue, googleMapsKey, selectedView
101
101
  });
102
102
  });
103
103
  }, 300), []);
104
- _react.useEffect(()=>()=>throttledSearchAddress.cancel(), [
105
- throttledSearchAddress
106
- ]);
107
- const onSearchAddress = (searchValue)=>throttledSearchAddress(searchValue) ?? Promise.resolve(null);
108
- const onGetAddressFromLocation = (location, address)=>{
109
- if (!mapsObject || !location) {
104
+ const onSearchAddress = throttledSearchAddress;
105
+ const onGetAddressFromLocation = _react.useCallback((location, address)=>{
106
+ const currentMapsObject = mapsObjectRef.current;
107
+ if (!currentMapsObject || !location) {
110
108
  return Promise.resolve('');
111
109
  }
112
110
  return new Promise((resolve)=>{
113
- const geocoder = new mapsObject.Geocoder();
111
+ const geocoder = new currentMapsObject.Geocoder();
114
112
  geocoder.geocode({
115
113
  location
116
114
  }, (result)=>{
@@ -124,11 +122,7 @@ function LocationEditor({ disabled, value, setValue, googleMapsKey, selectedView
124
122
  resolve('');
125
123
  });
126
124
  });
127
- };
128
- const onChangeLocation = (coords)=>{
129
- setLocalValue(coords);
130
- setValue(toLocationValue(coords));
131
- };
125
+ }, []);
132
126
  return /*#__PURE__*/ _react.createElement("div", {
133
127
  "data-test-id": "location-editor"
134
128
  }, /*#__PURE__*/ _react.createElement(_GoogleMapView.GoogleMapView, {
@@ -138,13 +132,21 @@ function LocationEditor({ disabled, value, setValue, googleMapsKey, selectedView
138
132
  onGoogleApiLoaded: ({ maps })=>{
139
133
  setMapsObject(maps);
140
134
  },
141
- onChangeLocation: onChangeLocation
135
+ onChangeLocation: (coords)=>{
136
+ setLocalValue(coords);
137
+ setValue(toLocationValue(coords));
138
+ }
142
139
  }), /*#__PURE__*/ _react.createElement(_LocationSelector.LocationSelector, {
143
140
  disabled: disabled || mapsObject === null,
144
141
  value: localValue,
145
142
  view: selectedView,
146
- onChangeView: setSelectedView,
147
- onChangeLocation: onChangeLocation,
143
+ onChangeView: (view)=>{
144
+ setSelectedView(view);
145
+ },
146
+ onChangeLocation: (coords)=>{
147
+ setLocalValue(coords);
148
+ setValue(toLocationValue(coords));
149
+ },
148
150
  onSearchAddress: onSearchAddress,
149
151
  onGetAddressFromLocation: onGetAddressFromLocation
150
152
  }));
@@ -12,11 +12,17 @@ const BerlinLocation = {
12
12
  lng: 13.41115439
13
13
  };
14
14
  export function GoogleMapView(props) {
15
- const [marker, setMarker] = React.useState();
16
- const [maps, setMaps] = React.useState();
15
+ const [googleMapsState, setGoogleMapsState] = React.useState({
16
+ marker: undefined,
17
+ maps: undefined
18
+ });
17
19
  const propsRef = React.useRef(props);
18
20
  propsRef.current = props;
19
- React.useEffect(()=>{
21
+ const googleMapsStateRef = React.useRef(googleMapsState);
22
+ googleMapsStateRef.current = googleMapsState;
23
+ const pendingLoadedMapsRef = React.useRef(null);
24
+ React.useLayoutEffect(()=>{
25
+ const { marker, maps } = googleMapsState;
20
26
  if (marker && maps) {
21
27
  if (props.location) {
22
28
  const latLng = new maps.LatLng(props.location.lat, props.location.lng);
@@ -28,13 +34,14 @@ export function GoogleMapView(props) {
28
34
  marker.setDraggable(!props.disabled);
29
35
  marker.setCursor(props.disabled ? 'not-allowed' : 'auto');
30
36
  }
31
- }, [
32
- marker,
33
- maps,
34
- props.disabled,
35
- props.location
36
- ]);
37
- const handleGoogleApiLoaded = (event)=>{
37
+ if (pendingLoadedMapsRef.current !== null && pendingLoadedMapsRef.current === maps) {
38
+ pendingLoadedMapsRef.current = null;
39
+ propsRef.current.onGoogleApiLoaded({
40
+ maps
41
+ });
42
+ }
43
+ });
44
+ const handleGoogleApiLoaded = React.useCallback((event)=>{
38
45
  const { maps: loadedMaps, map } = event;
39
46
  const loadedMarker = new loadedMaps.Marker({
40
47
  map,
@@ -44,11 +51,12 @@ export function GoogleMapView(props) {
44
51
  visible: Boolean(propsRef.current.location)
45
52
  });
46
53
  loadedMaps.event.addListener(map, 'click', (event)=>{
47
- if (propsRef.current.disabled) {
54
+ const { marker, maps } = googleMapsStateRef.current;
55
+ if (propsRef.current.disabled || !marker || !maps) {
48
56
  return;
49
57
  }
50
- loadedMarker.setPosition(event.latLng);
51
- loadedMarker.setVisible(true);
58
+ marker.setPosition(event.latLng);
59
+ marker.setVisible(true);
52
60
  propsRef.current.onChangeLocation({
53
61
  lat: event.latLng.lat(),
54
62
  lng: event.latLng.lng()
@@ -60,12 +68,12 @@ export function GoogleMapView(props) {
60
68
  lng: event.latLng.lng()
61
69
  });
62
70
  });
63
- setMarker(loadedMarker);
64
- setMaps(loadedMaps);
65
- propsRef.current.onGoogleApiLoaded({
71
+ pendingLoadedMapsRef.current = loadedMaps;
72
+ setGoogleMapsState({
73
+ marker: loadedMarker,
66
74
  maps: loadedMaps
67
75
  });
68
- };
76
+ }, []);
69
77
  return /*#__PURE__*/ React.createElement("div", {
70
78
  className: styles.root
71
79
  }, /*#__PURE__*/ React.createElement(GoogleMapReact, {
@@ -37,16 +37,14 @@ export function LocationEditor({ disabled, value, setValue, googleMapsKey, selec
37
37
  });
38
38
  });
39
39
  }, 300), []);
40
- React.useEffect(()=>()=>throttledSearchAddress.cancel(), [
41
- throttledSearchAddress
42
- ]);
43
- const onSearchAddress = (searchValue)=>throttledSearchAddress(searchValue) ?? Promise.resolve(null);
44
- const onGetAddressFromLocation = (location, address)=>{
45
- if (!mapsObject || !location) {
40
+ const onSearchAddress = throttledSearchAddress;
41
+ const onGetAddressFromLocation = React.useCallback((location, address)=>{
42
+ const currentMapsObject = mapsObjectRef.current;
43
+ if (!currentMapsObject || !location) {
46
44
  return Promise.resolve('');
47
45
  }
48
46
  return new Promise((resolve)=>{
49
- const geocoder = new mapsObject.Geocoder();
47
+ const geocoder = new currentMapsObject.Geocoder();
50
48
  geocoder.geocode({
51
49
  location
52
50
  }, (result)=>{
@@ -60,11 +58,7 @@ export function LocationEditor({ disabled, value, setValue, googleMapsKey, selec
60
58
  resolve('');
61
59
  });
62
60
  });
63
- };
64
- const onChangeLocation = (coords)=>{
65
- setLocalValue(coords);
66
- setValue(toLocationValue(coords));
67
- };
61
+ }, []);
68
62
  return /*#__PURE__*/ React.createElement("div", {
69
63
  "data-test-id": "location-editor"
70
64
  }, /*#__PURE__*/ React.createElement(GoogleMapView, {
@@ -74,13 +68,21 @@ export function LocationEditor({ disabled, value, setValue, googleMapsKey, selec
74
68
  onGoogleApiLoaded: ({ maps })=>{
75
69
  setMapsObject(maps);
76
70
  },
77
- onChangeLocation: onChangeLocation
71
+ onChangeLocation: (coords)=>{
72
+ setLocalValue(coords);
73
+ setValue(toLocationValue(coords));
74
+ }
78
75
  }), /*#__PURE__*/ React.createElement(LocationSelector, {
79
76
  disabled: disabled || mapsObject === null,
80
77
  value: localValue,
81
78
  view: selectedView,
82
- onChangeView: setSelectedView,
83
- onChangeLocation: onChangeLocation,
79
+ onChangeView: (view)=>{
80
+ setSelectedView(view);
81
+ },
82
+ onChangeLocation: (coords)=>{
83
+ setLocalValue(coords);
84
+ setValue(toLocationValue(coords));
85
+ },
84
86
  onSearchAddress: onSearchAddress,
85
87
  onGetAddressFromLocation: onGetAddressFromLocation
86
88
  }));
@@ -25,7 +25,7 @@ type LocationEditorProps = {
25
25
  selectedView: ViewType;
26
26
  setSelectedView: (view: ViewType) => void;
27
27
  };
28
- export declare function LocationEditor({ disabled, value, setValue, googleMapsKey, selectedView, setSelectedView, }: LocationEditorProps): React.JSX.Element;
28
+ export declare function LocationEditor({ disabled, value, setValue, googleMapsKey, selectedView, setSelectedView }: LocationEditorProps): React.JSX.Element;
29
29
  export declare function LocationEditorConnected(props: LocationEditorConnectedProps): React.JSX.Element;
30
30
  export declare namespace LocationEditorConnected {
31
31
  var defaultProps: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-location",
3
- "version": "2.2.2-canary.29+3ee64435",
3
+ "version": "2.2.2-canary.sha-cf70e54f.30+cf70e54f",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "types": "dist/types/index.d.ts",
@@ -36,14 +36,14 @@
36
36
  "dependencies": {
37
37
  "@contentful/f36-components": "^6.7.1",
38
38
  "@contentful/f36-tokens": "^6.1.2",
39
- "@contentful/field-editor-shared": "^4.5.2-canary.29+3ee64435",
39
+ "@contentful/field-editor-shared": "^4.5.2-canary.sha-cf70e54f.30+cf70e54f",
40
40
  "@emotion/css": "^11.13.5",
41
41
  "@types/google-map-react": "2.1.10",
42
42
  "google-map-react": "2.2.1",
43
43
  "lodash": "^4.17.15"
44
44
  },
45
45
  "devDependencies": {
46
- "@contentful/field-editor-test-utils": "^3.1.2",
46
+ "@contentful/field-editor-test-utils": "^3.1.3-canary.sha-cf70e54f.34+cf70e54f",
47
47
  "@lingui/core": "5.3.0"
48
48
  },
49
49
  "peerDependencies": {
@@ -54,5 +54,5 @@
54
54
  "publishConfig": {
55
55
  "registry": "https://npm.pkg.github.com/"
56
56
  },
57
- "gitHead": "3ee64435a6b1f0cb073174e7de406dc8cb4a74ad"
57
+ "gitHead": "cf70e54f33a6ecad25723afee834f3fe75bef531"
58
58
  }