-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MapPreview updates to support drawing & editing on map (JAM-5013) (#84)
* Adding some new props to MapPreview to allow drawing, and then passing those down to the MapDraw component * Update MapPreview to allow for drawing & editing, and also an option to show an empty map by default. Included new storybook story for this * Added new MapPreviewDraw to avoid breaking changes on MapDraw * Update MapPreview & the Draw storybook story to allow draw shape options to be customized, and pass the leafletElement with the onCreated & onEdited callbacks instead of the FeatureGroup ref which is redundant/already available * Allow JSX objects to be passed for labels in FormInput & InputButtons for more custom labels * Add shapeOptions prop to MapPreview to allow customizing of shape styles, etc * add disableEdit prop to MapPreview, rename disableEdit to disableDraw so we can allow edit specifically to be disabled * Add key to EditControl so it re-renders when the edit prop changes --------- Co-authored-by: Ben Adams <[email protected]>
- Loading branch information
Showing
7 changed files
with
314 additions
and
9 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
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
51 changes: 51 additions & 0 deletions
51
src/components/MapPreview/stories/MapPreview.EmptyMapDraw.stories.js
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,51 @@ | ||
import React, { useRef } from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
import { action } from '@storybook/addon-actions'; | ||
import Story from '../../../../stories/helpers/Story'; | ||
|
||
import MapPreview from '..'; | ||
|
||
const STORY_COMPONENT = 'Map Preview'; | ||
const STORY_NAME = 'Empty Map & Draw Controls'; | ||
|
||
const stories = storiesOf(`Components/${STORY_COMPONENT}`, module); | ||
|
||
stories.add(STORY_NAME, () => { | ||
const featureRef = useRef(); | ||
|
||
function handleOnDraw (drawLayer, leafletElement) { | ||
action(`${STORY_COMPONENT}::onDraw`)(drawLayer, leafletElement); | ||
} | ||
|
||
function handleOnEdit (drawLayer, leafletElement) { | ||
action(`${STORY_COMPONENT}::onEdit`)(drawLayer, leafletElement); | ||
} | ||
|
||
return ( | ||
<Story component={STORY_COMPONENT} name={STORY_NAME}> | ||
<MapPreview | ||
center={{ | ||
lat: 38.8048, | ||
lng: -77.0469 | ||
}} | ||
emptyMap={true} | ||
fitGeoJson={true} | ||
displayAccessRequests={false} | ||
displayAOIDetails={false} | ||
disableDraw={false} | ||
disableEdit={false} | ||
onDrawCreated={handleOnDraw} | ||
onDrawEdited={handleOnEdit} | ||
featureRef={featureRef} | ||
drawControlOptions={{ | ||
circle: true, | ||
circlemarker: false, | ||
marker: false, | ||
polygon: true, | ||
polyline: false, | ||
rectangle: true | ||
}} | ||
/> | ||
</Story> | ||
); | ||
}); |
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,147 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import 'leaflet-draw/dist/leaflet.draw.css'; | ||
import { Popup } from 'react-leaflet'; | ||
import { EditControl } from 'react-leaflet-draw'; | ||
|
||
import { useMapMarkerIcon } from '../../hooks'; | ||
|
||
import FeatureGroup from '../FeatureGroup'; | ||
|
||
const DEFAULT_CONTROL_OPTIONS = { | ||
circle: false, | ||
circlemarker: false, | ||
marker: true, | ||
polygon: true, | ||
polyline: false, | ||
rectangle: true | ||
}; | ||
|
||
const SHAPE_CONTROLS = ['circle', 'polygon', 'polyline', 'rectangle']; | ||
|
||
const DEFAULT_SHAPE_OPTIONS = { | ||
opacity: 1, | ||
weight: 3 | ||
}; | ||
|
||
const MapPreviewDraw = ({ | ||
children, | ||
forwardedRef, | ||
onCreated, | ||
onEdited, | ||
disableDrawControls = false, | ||
disableEditControls = false, | ||
controlOptions, | ||
shapeOptions, | ||
PopupContent, | ||
featureGroup, | ||
featureRef, | ||
...rest | ||
}) => { | ||
const { icon } = useMapMarkerIcon(); | ||
|
||
const markerOptions = { | ||
marker: { | ||
icon | ||
} | ||
}; | ||
|
||
const drawOptions = { | ||
...DEFAULT_CONTROL_OPTIONS, | ||
...markerOptions, | ||
...controlOptions | ||
}; | ||
|
||
// Loop through all of our configured options and determine the | ||
// shape configuration for each if a valid shape | ||
|
||
Object.keys(drawOptions).forEach((key) => { | ||
// Check if the option is turned off or if it's a valid shape | ||
|
||
if (!drawOptions[key] || !SHAPE_CONTROLS.includes(key)) return; | ||
|
||
// If it's set to true, we want to initialize the object for the shape | ||
// to set our options on | ||
|
||
if (drawOptions[key] === true) { | ||
drawOptions[key] = {}; | ||
} | ||
|
||
drawOptions[key].shapeOptions = { | ||
...DEFAULT_SHAPE_OPTIONS, | ||
...drawOptions[key].shapeOptions, | ||
...(shapeOptions && shapeOptions.style) | ||
}; | ||
}); | ||
|
||
/** | ||
* handleOnCreated | ||
* @description Fires when a layer is created. Triggers callback if available. | ||
* Clears all other layers except the newly created one. | ||
*/ | ||
|
||
function handleOnCreated ({ layer } = {}) { | ||
if (typeof onCreated === 'function') { | ||
onCreated(layer, forwardedRef); | ||
} | ||
} | ||
|
||
/** | ||
* handleOnEdited | ||
* @description Fires when a layer is edited. Triggers callback if available. | ||
*/ | ||
|
||
function handleOnEdited ({ target } = {}) { | ||
if (typeof onCreated === 'function') { | ||
onEdited(target, forwardedRef); | ||
} | ||
} | ||
|
||
return ( | ||
<FeatureGroup featureGroup={featureGroup} ref={featureRef}> | ||
{children} | ||
{!disableDrawControls && ( | ||
<> | ||
<EditControl | ||
key={disableEditControls} | ||
position="bottomright" | ||
onCreated={handleOnCreated} | ||
onEdited={handleOnEdited} | ||
draw={drawOptions} | ||
edit={{ | ||
edit: !disableEditControls, | ||
remove: false | ||
}} | ||
/> | ||
{PopupContent && ( | ||
<Popup> | ||
<PopupContent {...rest} /> | ||
</Popup> | ||
)} | ||
</> | ||
)} | ||
</FeatureGroup> | ||
); | ||
}; | ||
|
||
MapPreviewDraw.propTypes = { | ||
children: PropTypes.node, | ||
forwardedRef: PropTypes.object, | ||
onCreated: PropTypes.func, | ||
onEdited: PropTypes.func, | ||
disableDrawControls: PropTypes.bool, | ||
disableEditControls: PropTypes.bool, | ||
controlOptions: PropTypes.object, | ||
shapeOptions: PropTypes.object, | ||
featureGroup: PropTypes.object, | ||
PopupContent: PropTypes.any, | ||
featureRef: PropTypes.object | ||
}; | ||
|
||
const MapPreviewDrawWithRefs = React.forwardRef(function mapDraw (props, ref) { | ||
return <MapPreviewDraw {...props} forwardedRef={ref} />; | ||
}); | ||
|
||
MapPreviewDrawWithRefs.displayName = 'MapDrawWithRefs'; | ||
|
||
export default MapPreviewDrawWithRefs; |
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,51 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
|
||
import MapPreviewDraw from '.'; | ||
|
||
describe('MapDraw', () => { | ||
describe('Render', () => { | ||
const testClass = 'test'; | ||
const testText = 'Hi'; | ||
|
||
const mapdraw = shallow( | ||
<MapPreviewDraw> | ||
<div className={testClass}>{testText}</div> | ||
</MapPreviewDraw> | ||
); | ||
const mapdrawDive = mapdraw.dive(); | ||
const editcontrol = mapdrawDive.find('ForwardRef(Leaflet(EditControl))'); | ||
|
||
it('should render with the position prop', () => { | ||
expect(editcontrol.prop('position')).toEqual('bottomright'); | ||
}); | ||
|
||
it('should render with the disabled shape features', () => { | ||
expect(editcontrol.prop('draw').circle).toEqual(false); | ||
expect(editcontrol.prop('draw').circlemarker).toEqual(false); | ||
expect(editcontrol.prop('draw').polyline).toEqual(false); | ||
}); | ||
|
||
it('should render children within the component', () => { | ||
expect(mapdraw.find(`.${testClass}`).text()).toEqual(testText); | ||
}); | ||
}); | ||
|
||
describe('Events', () => { | ||
const mapdraw = shallow(<MapPreviewDraw onCreated={handleOnCreated} />); | ||
const mapdrawDive = mapdraw.dive(); | ||
const editcontrol = mapdrawDive.find('ForwardRef(Leaflet(EditControl))'); | ||
|
||
let testCreated = 1; | ||
|
||
function handleOnCreated () { | ||
testCreated++; | ||
} | ||
|
||
editcontrol.prop('onCreated')(); | ||
|
||
it('should fire given onCreated event', () => { | ||
expect(testCreated).toEqual(2); | ||
}); | ||
}); | ||
}); |
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 @@ | ||
export { default } from './MapPreviewDraw'; |