Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/modules-modules / observable-utils / decorateReactOnlyStatics.ts
Size: Mime:
import { ComponentType } from 'react'
import { isNil, hasOwnProp } from 'exotic'

// const isNotNil = x => isNil(x) === false
const REACT_STATICS = Object.freeze([
  'childContextTypes',
  'contextTypes',
  'defaultProps',
  'displayName',
  'getDefaultProps',
  'mixins',
  'propTypes',
  'type',
])

/**
 * @todo - move REACT_STATICS const out
 *
 * @description currently hoists only if they do not exist
 * @see ./decorateComponentStatics
 * @see ./decorateHoistAllStatics
 */
function hoistReactOnlyStatics(
  targetComponent: ComponentType,
  sourceComponent: ComponentType
): void {
  // if (isString(sourceComponent) === false) return targetComponent

  for (let index = 0; index < REACT_STATICS.length; index++) {
    const STATIC_NAME = REACT_STATICS[index]

    // target already has it
    // isNotNil
    if (hasOwnProp(targetComponent, STATIC_NAME) === true) {
      continue
    }
    // if we have it on source
    if (hasOwnProp(sourceComponent, STATIC_NAME) === true) {
      targetComponent[STATIC_NAME] = sourceComponent[STATIC_NAME]
    }
  }
}

export { hoistReactOnlyStatics }
export default hoistReactOnlyStatics