Update withLeaflet and add tests for viewport example, withLeaflet, createLeafletElement, and updateLeafletElement

This commit is contained in:
Henry Wu
2019-02-11 00:00:27 -08:00
parent 3b11b16d95
commit a74b997f94
2 changed files with 72 additions and 4 deletions
+2 -2
View File
@@ -444,8 +444,8 @@ export class LeafletConsumer extends React.Component<React.ConsumerProps<Leaflet
export class LeafletProvider extends React.Component<React.ProviderProps<LeafletContext>> {}
export interface ContextProps {
leaflet: LeafletContext;
leaflet?: LeafletContext;
}
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export function withLeaflet<T extends ContextProps>(WrappedComponent: React.Component<T>): React.Component<Omit<T, 'leaflet'>>;
export function withLeaflet<T extends ContextProps>(WrappedComponent: React.ComponentType<T>): React.Component<Omit<T, 'leaflet'>>;
+70 -2
View File
@@ -16,8 +16,10 @@ import {
MapProps,
Marker,
MarkerProps,
Path,
Pane,
Polygon,
PolygonProps,
Polyline,
Popup,
PopupProps,
@@ -25,7 +27,10 @@ import {
TileLayer,
Tooltip,
WMSTileLayer,
ZoomControl
ZoomControl,
LeafletProvider,
withLeaflet,
Viewport
} from 'react-leaflet';
const { BaseLayer, Overlay } = LayersControl;
@@ -207,7 +212,7 @@ export class CustomComponent extends Component<undefined, CustomComponentState>
}
}
// SOURCE ???
// Similar to custom-icons.js
export class MarkerWithDivIconExample extends Component<undefined, undefined> {
render() {
return (
@@ -628,6 +633,44 @@ export class VectorLayersExample extends Component<undefined, undefined> {
}
}
// viewport.js
const DEFAULT_VIEWPORT = {
center: [51.505, -0.09] as [number, number],
zoom: 13
};
export class ViewportExample extends Component<undefined, { viewport: Viewport }> {
state = {
viewport: {
center: [51.505, -0.09] as [number, number],
zoom: 13
}
};
onClickReset = () => {
this.setState({ viewport: DEFAULT_VIEWPORT });
}
onViewportChanged = (viewport: Viewport) => {
this.setState({ viewport });
}
render() {
return (
<Map
onClick={this.onClickReset}
onViewportChanged={this.onViewportChanged}
viewport={this.state.viewport}>
<TileLayer
attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
</Map>
);
}
}
// wms-tile-layer.js
interface WMSTileLayerExampleState {
lat: number;
@@ -729,6 +772,8 @@ class LegendControl extends MapControl<MapControlProps & { className?: string }>
}
}
const legendControlComponent = withLeaflet<MapControlProps>(LegendControl);
const LegendControlExample = () => (
<Map className="map" center={mapControlCenter} zoom={13} style={{ height: "300px" }}>
<TileLayer
@@ -745,3 +790,26 @@ const LegendControlExample = () => (
</LegendControl>
</Map>
);
class CustomPolygon extends Path<PolygonProps, L.Polygon> {
createLeafletElement(props: PolygonProps) {
const el = new L.Polygon(props.positions, this.getOptions(props));
this.contextValue = { ...props.leaflet, popupContainer: el };
return el;
}
updateLeafletElement(fromProps: PolygonProps, toProps: PolygonProps) {
if (toProps.positions !== fromProps.positions) {
this.leafletElement.setLatLngs(toProps.positions);
}
this.setStyleIfChanged(fromProps, toProps);
}
render() {
const { children } = this.props;
return children == null || this.contextValue == null ? null : (
<LeafletProvider value={this.contextValue}>{children}</LeafletProvider>
);
}
}
const leafletComponent = withLeaflet<PolygonProps>(CustomPolygon);