import React, { PropTypes } from "react"


export const nameWithContext = (Lower, prop = "name") => {
  const getDisplayName = (Lower) => ((Lower.displayName || Lower.name).replace(/Tag$/, ""))

  const buildInputName = (namespaces, name = "") => (
    [ ...namespaces, name ].map((field, index) => ( index === 0 ? field : `[${field}]` )).join("")
  )

  const higher = (props, context) => {
    const replacedProp = buildInputName(context.railsFormNamespaces, props[prop])
    const replacedProps = Object.assign({}, props, { [prop]: replacedProp })
    return <Lower {...replacedProps} />
  }

  higher.displayName = getDisplayName(Lower)
  higher.contextTypes = { railsFormNamespaces: PropTypes.arrayOf(PropTypes.string) }

  return higher
}

export const whitelistProps = (props, ...omit) => {
  const alwaysOmit = [ "key", "ref", ...omit ]
  const cloned = { ...props }
  alwaysOmit.forEach((key) => delete cloned[key])
  return cloned
}

import React, { PropTypes } from "react"


export const nameWithContext = (Lower, prop = "name") => {
  const getDisplayName = (component) => component.displayName || component.name || "Component"

  const buildInputName = (namespaces, name = "") => (
    [ ...namespaces, name ].map((field, index) => ( index === 0 ? field : `[${field}]` )).join("")
  )

  const higher = (props, context) => {
    const replacedProp = buildInputName(context.railsFormNamespaces, props[prop])
    const replacedProps = Object.assign({}, props, { [prop]: replacedProp })
    return <Lower {...replacedProps} />
  }

  higher.displayName = getDisplayName(Lower).replace(/Tag$/, "")
  higher.contextTypes = { railsFormNamespaces: PropTypes.arrayOf(PropTypes.string) }

  return higher
}

export const whitelistProps = (props, ...omit) => {
  const alwaysOmit = [ "key", "ref", ...omit ]
  const cloned = { ...props }
  alwaysOmit.forEach((key) => delete cloned[key])
  return cloned
}

Fix IE bug when calculating displayName
Fix IE bug when calculating displayName

IE does not have Function.prototype.bind. Supply a super-generic
fallback of "Component" so that a String is always returned.
