UNPKG

@uiw/react-baidu-map-info-window

Version:

Baidu Map info-window Components for React.

86 lines (85 loc) 2.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useInfoWindow = useInfoWindow; var _react = require("react"); var _reactBaiduMapMap = require("@uiw/react-baidu-map-map"); var _reactBaiduMapUtils = require("@uiw/react-baidu-map-utils"); function useInfoWindow() { let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; const { position, ...opts } = props; const { container, Portal } = (0, _reactBaiduMapUtils.usePortal)(); const { container: title, Portal: PortalTitle } = (0, _reactBaiduMapUtils.usePortal)(); const { map } = (0, _reactBaiduMapMap.useMapContext)(); const [infoWindow, setInfoWindow] = (0, _react.useState)(); (0, _react.useMemo)(() => { if (!infoWindow && map) { opts.title = title; const win = new BMap.InfoWindow(props.children ? container : opts.content || '', { ...opts }); setInfoWindow(win); } return () => { if (infoWindow) { infoWindow.restore(); } }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [map]); const [isOpen, setIsOpen] = (0, _react.useState)(opts.isOpen === undefined ? true : opts.isOpen); (0, _react.useEffect)(() => { if (map && BMap && infoWindow) { if (!isOpen) { map.closeInfoWindow(); } else if (position) { const point = new BMap.Point(position.lng, position.lat); map.openInfoWindow(infoWindow, point); } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [isOpen, infoWindow]); (0, _react.useEffect)(() => { if (infoWindow) { infoWindow.setContent(props.children ? container : opts.content || ''); } }, [props.content, props.children, infoWindow, container, opts.content]); (0, _react.useEffect)(() => { if (infoWindow) { infoWindow.setTitle(title); } }, [infoWindow, props.content, title]); (0, _reactBaiduMapUtils.useVisiable)(infoWindow, props); (0, _reactBaiduMapUtils.useEventProperties)(infoWindow, props, ['Close', 'Open', 'Maximize', 'Restore', 'ClickClose']); (0, _reactBaiduMapUtils.useProperties)(infoWindow, props, ['Width', 'Height', // 'Title', // 'Content', 'MaxContent']); (0, _reactBaiduMapUtils.useEnableProperties)(infoWindow, props, ['CloseOnClick', 'Maximize', 'AutoPan']); return { /** * 信息窗口实例对象 */ infoWindow, /** * 更新 信息窗口实例对象 */ setInfoWindow, isOpen, setIsOpen, Portal, PortalTitle }; }