DefinitelyTyped/types/react-map-gl/react-map-gl-tests.tsx
Otto Urpelainen 32b6d5ef2a Fix callback handler inputs to use ViewportProps instead of ViewState
* Fix callback handler inputs

Based on both react-map-gl documentation and trying things out, types
ViewportChangeHandler and ViewStrateChangeHandler both receive a
ViewportProps as input, rather than ViewState as was defined. This
commit fixes that problem.

Fields that were missing include at least 'width' and 'height',
important for working with an auto-resizing map.

* [react-map-gl] ExtraState interface fix

Add missing parameters to ExtraState interface, mark all as optional.
Any or none may be included in a ExtraState object when such is used.

* [react-map-gl] New onViewStateChange inputs

Added new input parameters 'interactionState' and 'oldViewState' to
callbacks 'onViewportChange' and 'onViewStateChange' in
'MapControllerOptions', 'InteractiveMaps' and 'MapContextProps'. There
are also other places where callbacks with similar names are used. But
in those cases, the extra parameters are never used. Because of this,
there are now two different types for callback functions.
2019-10-03 15:49:20 -07:00

131 lines
4.2 KiB
TypeScript

import * as React from "react";
import {
InteractiveMap,
CanvasOverlay,
SVGOverlay,
HTMLOverlay,
FullscreenControl,
GeolocateControl,
CanvasRedrawOptions,
HTMLRedrawOptions,
SVGRedrawOptions,
StaticMap,
ViewportProps
} from 'react-map-gl';
import * as MapboxGL from "mapbox-gl";
interface State {
viewport: ViewportProps;
}
class MyMap extends React.Component<{}, State> {
readonly state: State = {
viewport: {
width: 400,
height: 400,
bearing: 0,
latitude: 0,
longitude: 0,
zoom: 3,
pitch: 0,
altitude: 1.5,
maxZoom: 20,
minZoom: 0,
maxPitch: 60,
minPitch: 0,
},
};
private map: MapboxGL.Map;
render() {
return (
<div>
<InteractiveMap
{...this.state.viewport}
mapboxApiAccessToken="pk.test"
ref={this.setRefInteractive}
onViewportChange={viewport => this.setState({ viewport })}
onViewStateChange={({ viewState }) => this.setState({ viewport: viewState })}
>
<FullscreenControl className="test-class" container={document.querySelector('body')} />
<GeolocateControl className="test-class" style={{ marginTop: "8px" }} />
<CanvasOverlay
redraw={opts => {
const {
ctx,
height,
project,
unproject,
width,
} = opts;
const xy: number[] = unproject(project([20, 20]));
ctx.clearRect(0, 0, width, height);
}}
/>
<CanvasOverlay
redraw={opts => {}}
captureScroll={true}
captureDrag={true}
captureClick={true}
captureDoubleClick={true}
/>
<SVGOverlay
redraw={() => {}}
/>
<SVGOverlay
redraw={opts => {
const {
height,
project,
unproject,
width,
} = opts;
const xy: number[] = unproject(project([20, 20]));
}}
captureScroll={true}
captureDrag={true}
captureClick={true}
captureDoubleClick={true}
/>
<HTMLOverlay
redraw={() => {}}
/>
<HTMLOverlay
redraw={opts => {
const {
height,
project,
unproject,
width,
} = opts;
const xy: number[] = unproject(project([20, 20]));
}}
style={{
border: "2px solid black"
}}
captureScroll={true}
captureDrag={true}
captureClick={true}
captureDoubleClick={true}
/>
</InteractiveMap>
<StaticMap
{...this.state.viewport}
mapboxApiAccessToken="pk.test"
height={400}
width={400}
ref={this.setRefStatic}
/>
</div>
);
}
private readonly setRefInteractive = (el: InteractiveMap) => {
this.map = el.getMap();
}
private readonly setRefStatic = (el: StaticMap) => {
this.map = el.getMap();
}
}