All files / label/src useLabel.ts

45.83% Statements 11/24
5.55% Branches 1/18
25% Functions 1/4
47.82% Lines 11/23

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                            1x 1x 1x   1x   1x                                 1x             1x 1x                   1x 1x   1x            
import { useEffect, useState } from 'react';
import {
  usePortal,
  useEnableProperties,
  useProperties,
  useVisiable,
  useEventProperties,
} from '@uiw/react-baidu-map-utils';
import { useMapContext } from '@uiw/react-baidu-map-map';
import { LabelProps } from './';
 
export interface UseLabel extends LabelProps {}
 
export function useLabel(props = {} as UseLabel) {
  const [label, setLabel] = useState<BMap.Label>();
  const { offset, style, content = '', position, enableMassClear } = props;
  const { container, Portal } = usePortal();
  // const { container } = useRenderDom({ children: props.children });
  const { map } = useMapContext();
 
  useEffect(() => {
    if (!BMap || !map) return;
    if (!label) {
      const opts = { offset, position, enableMassClear };
      const instance = new BMap.Label(props.children ? container.innerHTML : content, opts);
      instance.setStyle({ ...style });
      map.addOverlay(instance);
      setLabel(instance);
    }
    return () => {
      if (map && label) {
        map.removeOverlay(label);
      }
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [map]);
 
  useEffect(() => {
    if (label) {
      label.setContent(props.children ? container.innerHTML : content);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [props.children, container, label]);
 
  useVisiable(label!, props);
  useEventProperties<BMap.Label, UseLabel>(label!, props, [
    'Click',
    'DblClick',
    'MouseDo',
    'MouseUp',
    'MouseOout',
    'MouseO',
    'Remove',
    'RightClick',
  ]);
  useProperties<BMap.Label, UseLabel>(label!, props, ['Style', 'Position', 'Offset', 'Title', 'ZIndex']);
  useEnableProperties<BMap.Label, UseLabel>(label!, props, ['MassClear']);
 
  return {
    label,
    setLabel,
    Portal,
  };
}