All files / app/pages/stacking/utils use-stacking-form-step.ts

0% Statements 0/15
0% Branches 0/2
0% Functions 0/6
0% Lines 0/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                                                                   
import { mapObjIndexed } from 'ramda';
import { useState } from 'react';
 
/**
 * Accepts an object, keyed by form step, where the
 * property describes when that step is complete.
 * The step being `closed` is implicitly required for being complete
 */
export function useStackingFormStep<T>(stepComplete: Record<string, boolean>) {
  const keys = Object.keys(stepComplete);
 
  const [stepState] = useState(() =>
    keys.reduce((acc, val) => {
      (acc as any)[val as any] = val === keys[0] ? 'open' : 'closed';
      return acc;
    }, {})
  );
 
  const stepCompleteMap = mapObjIndexed(val => Boolean(val), stepComplete);
 
  const getIsComplete = (step: T) => (stepCompleteMap as any)[step];
 
  const allComplete = Object.values(stepCompleteMap).every(value => value);
 
  return {
    stepState,
    getIsComplete,
    open,
    close,
    stepCompleteMap,
    allComplete,
  };
}