All files / copyright-control/src Item.tsx

8.33% Statements 1/12
0% Branches 0/7
0% Functions 0/3
8.33% Lines 1/12

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        1x                                                                              
import React from 'react';
import { useEffect, useState } from 'react';
import { usePrevious, usePortal } from '@uiw/react-baidu-map-utils';
 
let uid: number = 1;
 
export interface CopyrightControlItemProps {
  /**
   * 自定义 DOM 元素。
   */
  children?: React.ReactNode;
  /**
   * 该版权信息所适用的地理区域
   */
  bounds?: BMap.Bounds;
  /**
   * 表示版权控件实例,`CopyrightControl` 给子组件注入的一个版权控件实例属性。
   */
  control?: BMap.CopyrightControl;
}
 
export function CopyrightControlItem(props: CopyrightControlItemProps = {}) {
  const { children, control, bounds } = props;
  const [id] = useState(uid++);
  const { container, Portal } = usePortal();
  const prevId = usePrevious(id);
  useEffect(() => {
    if (control) {
      control.addCopyright({
        id,
        bounds,
        content: container!.innerHTML,
      });
    }
    return function () {
      if (prevId && control) {
        control.removeCopyright(prevId);
      }
    };
  }, [bounds, children, container, control, id, prevId]);
 
  return <Portal>{children}</Portal>;
}