UNPKG

@uiw/react-amap-map

Version:

基于 React 封装的高德地图组件。AMap Component Based On React.

72 lines (71 loc) 2.1 kB
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose"; import _extends from "@babel/runtime/helpers/extends"; var _excluded = ["className", "children"]; /// <reference types="@uiw/react-amap-types" /> import React, { useRef, useEffect, useImperativeHandle, forwardRef, useReducer } 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: _extends({}, state, { state, dispatch }), children: props.children }); }; export var Map = /*#__PURE__*/forwardRef((_ref, ref) => { var { className, children } = _ref, props = _objectWithoutPropertiesLoose(_ref, _excluded); var AMap = window.AMap; var [state, dispatch] = useReducer(reducer, initialState); var elmRef = useRef(null); var { setContainer, container, map } = useMap(_extends({ container: props.container || elmRef.current }, props)); useEffect(() => setContainer(elmRef.current), [elmRef.current]); useImperativeHandle(ref, () => _extends({}, props, { map, AMap, container: props.container || elmRef.current }), [map]); useEffect(() => { if (map) { dispatch({ map, container: elmRef.current, AMap }); } }, [map]); return /*#__PURE__*/_jsxs(Context.Provider, { value: _extends({}, state, { state, dispatch }), children: [!props.container && /*#__PURE__*/_jsx("div", { ref: elmRef, className: className, style: _extends({ fontSize: 1, width: '100%', height: '100%' }, props.style) }), AMap && map && typeof children === 'function' && children({ AMap, map, container }), AMap && map && typeof children !== 'function' && children] }); });