All files / src Container.tsx

100% Statements 10/10
100% Branches 2/2
100% Functions 3/3
100% Lines 10/10

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                                  22x                       70x 68x 68x 68x 68x 18x 4x   68x                                               22x  
import React, { forwardRef } from 'react';
import { NestedClose } from './comps/NestedClose';
import { NestedOpen } from './comps/NestedOpen';
import { KeyValues } from './comps/KeyValues';
import { useIdCompat } from './comps/useIdCompat';
import { useShowToolsDispatch } from './store/ShowTools';
 
export interface ContainerProps<T extends object> extends React.HTMLAttributes<HTMLDivElement> {
  keyName?: string | number;
  keyid?: string;
  parentValue?: T;
  level?: number;
  value?: T;
  initialValue?: T;
  /** Index of the parent `keyName` */
  keys?: (string | number)[];
}
export const Container = forwardRef(<T extends object>(props: ContainerProps<T>, ref: React.Ref<HTMLDivElement>) => {
  const {
    className = '',
    children,
    parentValue,
    keyid,
    level = 1,
    value,
    initialValue,
    keys,
    keyName,
    ...elmProps
  } = props;
  const dispatch = useShowToolsDispatch();
  const subkeyid = useIdCompat();
  const defaultClassNames = [className, 'w-rjv-inner'].filter(Boolean).join(' ');
  const reset: React.HTMLAttributes<HTMLDivElement> = {
    onMouseEnter: () => dispatch({ [subkeyid]: true }),
    onMouseLeave: () => dispatch({ [subkeyid]: false }),
  };
  return (
    <div className={defaultClassNames} ref={ref} {...elmProps} {...reset}>
      <NestedOpen
        expandKey={subkeyid}
        value={value}
        level={level}
        keys={keys}
        parentValue={parentValue}
        keyName={keyName}
        initialValue={initialValue}
      />
      <KeyValues
        expandKey={subkeyid}
        value={value}
        level={level}
        keys={keys}
        parentValue={parentValue}
        keyName={keyName}
      />
      <NestedClose expandKey={subkeyid} value={value} level={level} keys={keys} />
    </div>
  );
});
 
Container.displayName = 'JVR.Container';