UNPKG

@uiw/react-amap-map

Version:

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

73 lines (72 loc) 2.29 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 _useReducer = useReducer(reducer, initialState), state = _useReducer[0], dispatch = _useReducer[1]; return /*#__PURE__*/_jsx(Context.Provider, { value: _extends({}, state, { state, dispatch }), children: props.children }); }; export var Map = /*#__PURE__*/forwardRef((_ref, ref) => { var className = _ref.className, children = _ref.children, props = _objectWithoutPropertiesLoose(_ref, _excluded); var AMap = window.AMap; var _useReducer2 = useReducer(reducer, initialState), state = _useReducer2[0], dispatch = _useReducer2[1]; var elmRef = useRef(null); var _useMap = useMap(_extends({ container: props.container || elmRef.current }, props)), setContainer = _useMap.setContainer, container = _useMap.container, map = _useMap.map; 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] }); });