All files / src/utils createHook.tsx

100% Statements 4/4
100% Branches 5/5
100% Functions 2/2
100% Lines 4/4

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                      557x 14864x 14864x   14864x      
import * as React from 'react';
import _get from 'lodash/get';
import _merge from 'lodash/merge';
import _omit from 'lodash/omit';
 
import { useDefaultProps } from './useDefaultProps';
 
export function createHook<P>(
  useHook: (props: Partial<P>, options: { themeKey: string; themeKeyOverride: string }) => Partial<P>,
  config?: { defaultProps?: Partial<P>; themeKey?: string }
) {
  return (props: Partial<P>, { themeKey: themeKeyOverride = undefined } = {}) => {
    const themeKey = themeKeyOverride || _get(props, 'themeKey') || _get(config, 'themeKey');
    const { props: newProps, themeKey: newThemeKey } = useDefaultProps(props, { ...config, themeKey });
    // @ts-ignore
    return useHook(_omit(newProps, 'themeKey'), { themeKey: _get(config, 'themeKey'), themeKeyOverride: newThemeKey });
  };
}