All files / src/utils useDefaultProps.ts

100% Statements 8/8
50% Branches 1/2
100% Functions 1/1
100% Lines 8/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                  21296x 21296x 21296x 21296x 21296x       21296x 21296x             21296x    
import * as React from 'react';
import _get from 'lodash/get';
import _merge from 'lodash/merge';
import _omitBy from 'lodash/omitBy';
import _isUndefined from 'lodash/isUndefined';
 
import { ThemeContext } from '../styled';
 
export function useDefaultProps(props: any = {}, config: any = {}) {
  const { themeKey } = config;
  const theme = React.useContext(ThemeContext);
  const configDefaultProps = _omitBy(_get(config, 'defaultProps', {}), _isUndefined);
  const themeDefaultProps = _omitBy(_get(theme, `${themeKey}.defaultProps`, {}), _isUndefined);
  const themeVariantDefaultProps = _omitBy(
    _get(theme, `${themeKey}.variants.${props.variant}.defaultProps`, {}),
    _isUndefined
  );
  const overridesDefaultProps = _omitBy(_get(props, `overrides.${themeKey}.defaultProps`, {}), _isUndefined);
  const newProps = {
    ...configDefaultProps,
    ...themeDefaultProps,
    ...themeVariantDefaultProps,
    ...overridesDefaultProps,
    ..._omitBy(props, _isUndefined)
  };
  return { props: newProps, themeKey };
}