All files / info-window/src useInfoWindow.ts

53.12% Statements 17/32
13.33% Branches 4/30
33.33% Functions 2/6
53.12% Lines 17/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93                            1x 1x 1x 1x 1x 1x 1x             1x               1x 1x                       1x           1x           1x 1x             1x             1x   1x                              
import { useState, useEffect, useMemo, PropsWithChildren } from 'react';
import { useMapContext } from '@uiw/react-baidu-map-map';
import {
  useEnableProperties,
  useProperties,
  useVisiable,
  useEventProperties,
  usePortal,
} from '@uiw/react-baidu-map-utils';
import { InfoWindowProps } from '.';
 
export interface UseInfoWindow extends PropsWithChildren<InfoWindowProps> {}
 
export function useInfoWindow(props = {} as UseInfoWindow) {
  const { position, ...opts } = props;
  const { container, Portal } = usePortal();
  const { container: title, Portal: PortalTitle } = usePortal();
  const { map } = useMapContext();
  const [infoWindow, setInfoWindow] = useState<BMap.InfoWindow>();
  useMemo(() => {
    Iif (!infoWindow && map) {
      opts.title = title;
      const win = new BMap.InfoWindow(props.children ? container : opts.content || '', {
        ...(opts as BMap.InfoWindowOptions),
      });
      setInfoWindow(win);
    }
    return () => {
      if (infoWindow) {
        infoWindow.restore();
      }
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [map]);
 
  const [isOpen, setIsOpen] = useState(opts.isOpen === undefined ? true : opts.isOpen);
  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]);
 
  useEffect(() => {
    if (infoWindow) {
      infoWindow.setContent(props.children ? container : opts.content || '');
    }
  }, [props.content, props.children, infoWindow, container, opts.content]);
 
  useEffect(() => {
    if (infoWindow) {
      infoWindow.setTitle(title);
    }
  }, [infoWindow, props.content, title]);
 
  useVisiable(infoWindow!, props);
  useEventProperties<BMap.InfoWindow, UseInfoWindow>(infoWindow!, props, [
    'Close',
    'Open',
    'Maximize',
    'Restore',
    'ClickClose',
  ]);
  useProperties<BMap.InfoWindow, UseInfoWindow>(infoWindow!, props, [
    'Width',
    'Height',
    // 'Title',
    // 'Content',
    'MaxContent',
  ]);
  useEnableProperties<BMap.InfoWindow, UseInfoWindow>(infoWindow!, props, ['CloseOnClick', 'Maximize', 'AutoPan']);
 
  return {
    /**
     * 信息窗口实例对象
     */
    infoWindow,
    /**
     * 更新 信息窗口实例对象
     */
    setInfoWindow,
    isOpen,
    setIsOpen,
    Portal,
    PortalTitle,
  };
}