@uiw/react-baidu-map-map
Version:
Baidu Map Components for React.
93 lines • 2.9 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
var _excluded = ["className", "style", "children"];
/// <reference types="@uiw/react-baidu-map-types" />
import React, { useRef, useEffect, useImperativeHandle, useReducer, useMemo } from 'react';
import { useMap } from "./useMap.js";
import { Context, reducer, initialState } from "./context.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export * from "./useMap.js";
export * from "./context.js";
export var Provider = props => {
var [state, dispatch] = useReducer(reducer, initialState);
return /*#__PURE__*/_jsx(Context.Provider, {
value: {
state,
dispatch
},
children: props.children
});
};
export default /*#__PURE__*/React.forwardRef((_ref, ref) => {
var {
className,
style,
children
} = _ref,
props = _objectWithoutPropertiesLoose(_ref, _excluded);
var [state, dispatch] = useReducer(reducer, initialState);
window.BMap = window.BMap || window.BMapGL;
var elmRef = useRef(null);
var {
setContainer,
container,
setCenter,
setAutoLocalCity,
map
} = useMap(_extends({
container: elmRef.current
}, props));
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => setContainer(elmRef.current), [elmRef.current]);
// eslint-disable-next-line react-hooks/exhaustive-deps
useMemo(() => props.center && setCenter(props.center), [props.center]);
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => setAutoLocalCity(props.autoLocalCity), [props.autoLocalCity]);
// eslint-disable-next-line react-hooks/exhaustive-deps
useImperativeHandle(ref, () => _extends({}, props, {
map,
BMap,
container: elmRef.current
}), [map]);
var chields = typeof children === 'function' ? [children] : React.Children.toArray(children);
useEffect(() => {
if (map) {
dispatch({
map,
container: elmRef.current,
BMap
});
}
}, [map]);
return /*#__PURE__*/_jsxs(Context.Provider, {
value: {
state,
dispatch
},
children: [/*#__PURE__*/_jsx("div", {
ref: elmRef,
className: className,
style: _extends({
fontSize: 1,
height: '100%'
}, style)
}), BMap && map && typeof children === 'function' && children({
BMap,
map,
container
}), BMap && map && chields.map((child, key) => {
if (! /*#__PURE__*/React.isValidElement(child)) return null;
if (child.type && typeof child.type === 'string') {
return /*#__PURE__*/React.cloneElement(child, {
key
});
}
return /*#__PURE__*/React.cloneElement(child, _extends({}, child.props, {
BMap,
map,
container,
key
}));
})]
});
});