@actinc/dls
Version:
Design Language System (DLS) for ACT & Encoura front-end projects.
98 lines • 5 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
/**
* Copyright (c) ACT, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { useTheme } from '@mui/material/styles';
import bbox from '@turf/bbox';
import isString from 'lodash/isString';
import numeral from 'numeral';
import React from 'react';
import Map from "../Map";
import MapPopup from "../MapPopup";
/**
* StateMap component which under the hood uses mapbox and react-map-gl. For this to work it's necessary
* to add the link bellow in the head of your page: <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.css' rel='stylesheet' />
* For more information: https://visgl.github.io/react-map-gl/docs/get-started/get-started#styling
*/
export var StateMap = function (_a) {
var _b, _c, _d;
var data = _a.data, geoJSONPath = _a.geoJSONPath, mapboxAccessToken = _a.mapboxAccessToken, mapPopupProps = _a.mapPopupProps, mapProps = _a.mapProps, onHoverInfo = _a.onHoverInfo, processDataFn = _a.processDataFn, selectedState = _a.selectedState, setOnHoverInfo = _a.setOnHoverInfo, tooltipElement = _a.tooltipElement;
var _e = useTheme(), breakpoints = _e.breakpoints, spacing = _e.spacing;
var _f = React.useState(), statesJSON = _f[0], setStatesJSON = _f[1];
var _g = React.useState(), hoverInfo = _g[0], setHoverInfo = _g[1];
var finalHoverInfo = onHoverInfo || hoverInfo;
React.useEffect(function () {
fetch(geoJSONPath)
.then(function (resp) { return resp.json(); })
.then(function (json) {
return setStatesJSON(json);
})
.catch(function (err) { return console.error('Could not load data', err); }); // eslint-disable-line
}, []);
var processedData = React.useMemo(function () {
return statesJSON && processDataFn && processDataFn(statesJSON, data);
}, [data, statesJSON, processDataFn]);
var initialBoundsPosition = React.useMemo(function () {
if (selectedState && processedData) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
var features = processedData.features.filter(function (f) {
return selectedState.includes(f.id);
});
if (features.length > 0) {
var _a = bbox({
features: features,
type: 'FeatureCollection',
}), minLng = _a[0], minLat = _a[1], maxLng = _a[2], maxLat = _a[3];
return {
id: features.length > 1 ? undefined : selectedState[0],
position: [
[minLng, minLat],
[maxLng, maxLat],
],
};
}
}
return undefined;
}, [processedData, selectedState]);
// eslint-disable-next-line react/jsx-no-useless-fragment
if (!processedData)
return _jsx(_Fragment, {});
var parentHeight = (mapProps === null || mapProps === void 0 ? void 0 : mapProps.height) || 450;
var parentWidth = (mapProps === null || mapProps === void 0 ? void 0 : mapProps.width) || '100%';
return (_jsx(Map, __assign({ data: processedData, height: parentHeight, initialBoundsPosition: initialBoundsPosition, mapboxAccessToken: mapboxAccessToken, setHoverInfo: setOnHoverInfo || setHoverInfo, width: parentWidth }, mapProps, { children: tooltipElement ||
(finalHoverInfo &&
(isString(parentWidth) ||
parentWidth > breakpoints.values.sm - parseInt(spacing(12), 10)) ? (_jsx(MapPopup, __assign({ popupProps: {
latitude: finalHoverInfo.lat,
longitude: finalHoverInfo.lng,
}, rows: [
{
title: 'State',
value: (_b = finalHoverInfo.feature.properties) === null || _b === void 0 ? void 0 : _b.name,
},
{
title: 'Code',
value: (_c = finalHoverInfo.feature.properties) === null || _c === void 0 ? void 0 : _c.abbr,
},
{
title: 'Volume',
value: numeral((_d = finalHoverInfo.feature.properties) === null || _d === void 0 ? void 0 : _d.value).format('0,0'),
},
] }, mapPopupProps))) : undefined) })));
};
export default StateMap;
//# sourceMappingURL=index.js.map