All files / control/src useControl.tsx

50% Statements 8/16
0% Branches 0/10
33.33% Functions 1/3
50% Lines 8/16

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                  1x 1x 1x 1x   1x                           1x 1x 1x            
import { useState, useEffect } from 'react';
import { useMapContext } from '@uiw/react-baidu-map-map';
import { useProperties, useVisiable, usePortal } from '@uiw/react-baidu-map-utils';
import getControl from './getControl';
import { ControlProps } from './';
 
export interface UseControl extends ControlProps {}
 
export function useControl(props = {} as UseControl) {
  const { container, Portal } = usePortal();
  const [control, setControl] = useState<BMap.Control>();
  const { offset, anchor } = props;
  const { map } = useMapContext();
 
  useEffect(() => {
    if (map && !control && container) {
      const Control = getControl();
      const instance = new Control(container, anchor, offset);
      setControl(instance);
      map.addOverlay(instance);
    }
    return () => {
      if (map && control) {
        map.removeControl(control);
      }
    };
  }, [map, control, anchor, offset, container]);
 
  useVisiable(control!, props);
  useProperties<BMap.Control, UseControl>(control!, props, ['Anchor', 'Offset']);
  return {
    control,
    ControlPortal: Portal,
    setControl,
  };
}