All files / copyright-control/src index.tsx

55.55% Statements 5/9
0% Branches 0/2
33.33% Functions 1/3
62.5% Lines 5/8

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                                        1x       1x 1x 1x                                         1x      
/// <reference types="@uiw/react-baidu-map-types" />
import React, { useImperativeHandle, Fragment } from 'react';
import { OverlayProps } from '@uiw/react-baidu-map-map';
import { useCopyrightControl } from './useCopyrightControl';
import { CopyrightControlItem } from './Item';
 
export * from './Item';
export * from './useCopyrightControl';
 
export interface CopyrightControlProps extends OverlayProps, BMap.CopyrightControlOptions {
  /**
   * 自定义 DOM 元素。
   */
  children?: React.ReactNode;
}
 
type RefCopyrightControl = React.ForwardRefExoticComponent<
  CopyrightControlProps & React.RefAttributes<CopyrightControlProps>
> & { Item: typeof CopyrightControlItem };
 
const CopyrightControl: RefCopyrightControl = React.forwardRef<
  CopyrightControlProps & { copyrightControl?: BMap.CopyrightControl },
  CopyrightControlProps
>((props, ref) => {
  const { copyrightControl } = useCopyrightControl(props);
  useImperativeHandle(ref, () => ({ ...props, copyrightControl }), [copyrightControl, props]);
  return (
    <Fragment>
      {React.Children.toArray(props.children).map((child, index) => {
        if (React.isValidElement(child)) {
          return React.cloneElement(
            child as React.ReactElement<
              { control: BMap.CopyrightControl; id: number },
              string | React.JSXElementConstructor<any>
            >,
            {
              control: copyrightControl,
              id: index + 1,
            },
          );
        }
        return child;
      })}
    </Fragment>
  );
}) as unknown as RefCopyrightControl;
 
CopyrightControl.Item = CopyrightControlItem;
 
export default CopyrightControl;