All files / src Render.tsx

100% Statements 8/8
71.42% Branches 10/14
100% Functions 2/2
100% Lines 7/7

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              9x 16x 16x 16x             2x 1x                 9x  
import { FC, Fragment, Children, cloneElement, isValidElement } from 'react';
import { useStore, RenderStateProps, InitialState } from './store';
 
export type RenderChildren =
  | { children?: (props: Required<RenderStateProps>, data: InitialState['data']) => React.ReactNode }
  | { children?: React.ReactNode };
 
export const Render: FC<RenderChildren> = ({ children }) => {
  const { fields = {}, buttons = {}, extra = {}, $$index = {}, blocks = {}, data } = useStore();
  const childs = typeof children === 'function' ? [] : Children.toArray(children);
  return (
    <Fragment>
      {typeof children === 'function' &&
        !isValidElement(children) &&
        children({ fields, buttons, blocks, extra, $$index }, { ...data })}
      {typeof children !== 'function' &&
        childs.map((child, key) => {
          if (!isValidElement(child)) return null;
          return cloneElement(child, {
            ...child.props,
            key,
          });
        })}
    </Fragment>
  );
};
 
Render.displayName = 'Login.Render';