Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move zoom and coordinates as separate component #309

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions public/components/map_container/map_container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

import React, { useEffect, useRef, useState } from 'react';
import { EuiPanel } from '@elastic/eui';
import { LngLat, Map as Maplibre, NavigationControl, Popup, MapEventType } from 'maplibre-gl';
import { debounce, throttle } from 'lodash';
import { LayerControlPanel } from '../layer_control_panel';
Expand Down Expand Up @@ -34,6 +33,7 @@ import {
getReferenceLayers,
referenceLayerTypeLookup,
} from '../../model/layersFunctions';
import { MapsFooter } from './mapsFooter';

interface MapContainerProps {
setLayers: (layers: MapLayerSpecification[]) => void;
Expand Down Expand Up @@ -298,19 +298,7 @@ export const MapContainer = ({

return (
<div className="map-main">
<EuiPanel
hasShadow={false}
hasBorder={false}
color="transparent"
className="zoombar"
data-test-subj="mapStatusBar"
>
<small>
{coordinates &&
`lat: ${coordinates.lat.toFixed(4)}, lon: ${coordinates.lng.toFixed(4)}, `}
zoom: {zoom}
</small>
</EuiPanel>
<MapsFooter coordinates={coordinates} zoom={zoom} />
{mounted && (
<LayerControlPanel
maplibreRef={maplibreRef}
Expand Down
33 changes: 33 additions & 0 deletions public/components/map_container/mapsFooter.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import { act } from 'react-dom/test-utils';

import { MapsFooter } from './mapsFooter';
import { LngLat } from 'maplibre-gl';

let container: Element | null;
beforeEach(() => {
// setup a DOM element as a render target
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
// cleanup on exiting
unmountComponentAtNode(container!);
container?.remove();
container = null;
});

it('renders with or without coordinates', () => {
act(() => {
render(<MapsFooter zoom={3} />, container);
});
expect(container?.textContent).toBe('zoom: 3');

act(() => {
const coordinates: LngLat = new LngLat(-73.974912, 40.773654);
render(<MapsFooter zoom={3} coordinates={coordinates} />, container);
});
expect(container?.textContent).toBe('lat: 40.7737, lon: -73.9749, zoom: 3');
});
35 changes: 35 additions & 0 deletions public/components/map_container/mapsFooter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { EuiPanel } from '@elastic/eui';
import { isEqual } from 'lodash';
import React, { memo } from 'react';
import { LngLat } from 'maplibre-gl';

const coordinatesRoundOffDigit = 4;
interface MapFooterProps {
coordinates?: LngLat;
zoom: number;
}

export const MapsFooter = memo(({ coordinates, zoom }: MapFooterProps) => {
return (
<EuiPanel
hasShadow={false}
hasBorder={false}
color="transparent"
className="zoombar"
data-test-subj="mapStatusBar"
>
<small>
{coordinates &&
`lat: ${coordinates.lat.toFixed(
coordinatesRoundOffDigit
)}, lon: ${coordinates.lng.toFixed(coordinatesRoundOffDigit)}, `}
zoom: {zoom}
</small>
</EuiPanel>
);
}, isEqual);