import * as React from 'react' import * as hoistNonReactStatics_ from 'hoist-non-react-statics' // Since rollup cannot deal with namespace being a function, // this is to interop with TypeScript since `invariant` // does not export a default // https://github.com/rollup/rollup/issues/1267 const hoistNonReactStatics: typeof hoistNonReactStatics_ = (hoistNonReactStatics_ as any).default || hoistNonReactStatics_ import {invariantIntlContext} from '../utils' import {IntlShape, Omit} from '../types' function getDisplayName(Component: React.ComponentType): string { return Component.displayName || Component.name || 'Component' } // TODO: We should provide initial value here const IntlContext = React.createContext(null as any) const {Consumer: IntlConsumer, Provider: IntlProvider} = IntlContext export const Provider = IntlProvider export const Context = IntlContext export interface Opts< IntlPropName extends string = 'intl', ForwardRef extends boolean = false > { intlPropName?: IntlPropName forwardRef?: ForwardRef enforceContext?: boolean } export type WrappedComponentProps = { [k in IntlPropName]: IntlShape } export type WithIntlProps

= Omit & { forwardedRef?: React.Ref } export default function injectIntl< IntlPropName extends string, P extends WrappedComponentProps = WrappedComponentProps >( WrappedComponent: React.ComponentType

, options?: Opts ): React.FC> & { WrappedComponent: React.ComponentType

} export default function injectIntl< IntlPropName extends string = 'intl', P extends WrappedComponentProps = WrappedComponentProps, T extends React.ComponentType

= any >( WrappedComponent: React.ComponentType

, options?: Opts ): React.ForwardRefExoticComponent< React.PropsWithoutRef> & React.RefAttributes > & { WrappedComponent: React.ComponentType

} export default function injectIntl< IntlPropName extends string = 'intl', P extends WrappedComponentProps = WrappedComponentProps, ForwardRef extends boolean = false, T extends React.ComponentType

= any >( WrappedComponent: React.ComponentType

, options?: Opts ): React.ForwardRefExoticComponent< React.PropsWithoutRef> & React.RefAttributes > & { WrappedComponent: React.ComponentType

} { const {intlPropName = 'intl', forwardRef = false, enforceContext = true} = options || {} const WithIntl: React.FC

}> & { WrappedComponent: React.ComponentType

} = props => ( {(intl): React.ReactNode => { if (enforceContext) { invariantIntlContext(intl) } const intlProp = {[intlPropName]: intl} return ( ) }} ) WithIntl.displayName = `injectIntl(${getDisplayName(WrappedComponent)})` WithIntl.WrappedComponent = WrappedComponent if (forwardRef) { return hoistNonReactStatics( React.forwardRef((props: P, ref) => ( )), WrappedComponent ) as any } return hoistNonReactStatics(WithIntl, WrappedComponent) as any }