Created
May 5, 2018 21:03
-
-
Save jtmthf/0af7a8fcd5ea889a2215d5acefb4d5c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
import { Component, ComponentClass, SFC } from 'react'; | |
type Constructor<T = {}> = new (...args: any[]) => T; | |
type TagProps<Tag extends keyof JSX.IntrinsicElements> = { | |
is: Tag; | |
innerRef?: JSX.IntrinsicElements[Tag]['ref']; | |
} & JSX.IntrinsicElements[Tag]; | |
type StatelessComponentProps<P> = { | |
is: SFC<P>; | |
} & P; | |
type ComponentClassProps<T, P> = { | |
is: ComponentClass<P>; | |
innerRef?: (instance: T | null) => any; | |
} & P; | |
type Props<T> = T extends 'div' | |
? JSX.IntrinsicElements['div'] | |
: T extends keyof JSX.IntrinsicElements | |
? TagProps<T> | |
: T extends Component<infer P> | |
? ComponentClassProps<T, P> | |
: T extends SFC<infer P> ? StatelessComponentProps<P> : never; | |
class Any<T = 'div'> extends Component<Props<T>> { | |
static ofType<T>() { | |
return Any as Constructor<Any<T>>; | |
} | |
render() { | |
const { is: Cmp = 'div', innerRef, ...props } = this.props as any; | |
return <Cmp ref={innerRef} {...props} />; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment