Skip to content

Commit 97195df

Browse files
author
David Emory
committed
fix(map): Add component dimensions wrapper to BaseMap
Ensures proper map behavior on startup when bundled via webpack
1 parent 429b9d8 commit 97195df

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

lib/components/map/base-map.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import objectPath from 'object-path'
21
import React, { Component, PropTypes } from 'react'
32
import { connect } from 'react-redux'
43

54
import { Map, TileLayer, LayersControl, Popup, Marker } from 'react-leaflet'
5+
import ContainerDimensions from 'react-container-dimensions'
66

77
import { setLocation } from '../../actions/map'
88
import Icon from '../narrative/icon'
@@ -156,18 +156,8 @@ class BaseMap extends Component {
156156
})
157157
}
158158

159-
resized () {
160-
this.refs.map.leafletElement.invalidateSize()
161-
if (this.props.itinerary) {
162-
this.refs.map &&
163-
this.refs.map.leafletElement.fitBounds(getItineraryBounds(this.props.itinerary), {
164-
padding: [3, 3]
165-
})
166-
}
167-
}
168-
169159
render () {
170-
const { config, children, diagramLeg, elevationPoint } = this.props
160+
const { config, children, diagramLeg, elevationPoint, width, height } = this.props
171161
const { baseLayers } = this.props.config.map
172162

173163
const userControlledOverlays = []
@@ -230,6 +220,7 @@ class BaseMap extends Component {
230220
ref='map'
231221
className='map'
232222
center={center}
223+
style={{ width, height }}
233224
zoom={config.map.initZoom || 13}
234225
onClick={this._onLeftClick}
235226
onContextMenu={this._onRightClick}
@@ -301,6 +292,22 @@ class BaseMap extends Component {
301292
}
302293
}
303294

295+
// ContainerDimensions-wrapped BaseMap
296+
297+
class MapContainer extends Component {
298+
render () {
299+
return (
300+
<ContainerDimensions>
301+
{({ width, height }) => {
302+
// Do not render map if width or height is undefined or 0
303+
if (!width || !height) return null
304+
return <BaseMap width={width} height={height} {...this.props} />
305+
}}
306+
</ContainerDimensions>
307+
)
308+
}
309+
}
310+
304311
// connect to the redux store
305312

306313
const mapStateToProps = (state, ownProps) => {
@@ -329,7 +336,4 @@ const mapDispatchToProps = {
329336
setLocation
330337
}
331338

332-
// allow access to the wrapped BaseMap (for access to resized())
333-
export default connect(mapStateToProps, mapDispatchToProps, null, {
334-
withRef: true
335-
})(BaseMap)
339+
export default connect(mapStateToProps, mapDispatchToProps)(MapContainer)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"react": "^15.4.2",
5454
"react-addons-shallow-compare": "^15.4.2",
5555
"react-bootstrap": "^0.30.7",
56+
"react-container-dimensions": "^1.3.4",
5657
"react-dates": "^6.0.2",
5758
"react-dom": "^15.4.2",
5859
"react-fontawesome": "^1.5.0",

yarn.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,10 @@ base@^0.11.1:
14651465
mixin-deep "^1.2.0"
14661466
pascalcase "^0.1.1"
14671467

1468+
batch-processor@^1.0.0:
1469+
version "1.0.0"
1470+
resolved "https://registry.yarnpkg.com/batch-processor/-/batch-processor-1.0.0.tgz#75c95c32b748e0850d10c2b168f6bdbe9891ace8"
1471+
14681472
bcrypt-pbkdf@^1.0.0:
14691473
version "1.0.1"
14701474
resolved "https://registry.yarnpkg.com/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.1.tgz#63bc5dcb61331b92bc05fd528953c33462a06f8d"
@@ -3014,6 +3018,12 @@ electron-to-chromium@^1.3.30:
30143018
version "1.3.55"
30153019
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.55.tgz#f150e10b20b77d9d41afcca312efe0c3b1a7fdce"
30163020

3021+
element-resize-detector@^1.1.10:
3022+
version "1.1.14"
3023+
resolved "https://registry.yarnpkg.com/element-resize-detector/-/element-resize-detector-1.1.14.tgz#af064a0a618a820ad570a95c5eec5b77be0128c1"
3024+
dependencies:
3025+
batch-processor "^1.0.0"
3026+
30173027
elliptic@^6.0.0:
30183028
version "6.4.0"
30193029
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.4.0.tgz#cac9af8762c85836187003c8dfe193e5e2eae5df"
@@ -7583,6 +7593,14 @@ react-bootstrap@^0.30.7:
75837593
uncontrollable "^4.0.1"
75847594
warning "^3.0.0"
75857595

7596+
react-container-dimensions@^1.3.4:
7597+
version "1.3.4"
7598+
resolved "https://registry.yarnpkg.com/react-container-dimensions/-/react-container-dimensions-1.3.4.tgz#6407cc2e1260bb208bf5e8fd6f2c53c8f4c82595"
7599+
dependencies:
7600+
element-resize-detector "^1.1.10"
7601+
invariant "^2.2.2"
7602+
prop-types "^15.5.8"
7603+
75867604
react-dates@^6.0.2:
75877605
version "6.1.0"
75887606
resolved "https://registry.yarnpkg.com/react-dates/-/react-dates-6.1.0.tgz#8f655f71db429229a414ca13a876713519d4e0ac"

0 commit comments

Comments
 (0)