Created
October 20, 2017 20:38
-
-
Save HenryNguyen5/bfab95b8ed30ec33706f369569122a63 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 React, { Component } from 'react'; | |
import NewTabLink from 'components/ui/NewTabLink'; | |
interface RequiredProps { | |
conditional: boolean; | |
conditionalProps: { | |
[key: string]: any; | |
}; | |
} | |
/** | |
* Optional | |
*/ | |
export const withConditional = <WrappedComponentProps extends {}>( | |
PassedComponent: React.ComponentType<WrappedComponentProps> | |
) => | |
class extends Component<WrappedComponentProps & RequiredProps, {}> { | |
public render() { | |
const { conditional, conditionalProps, ...passedProps } = this.props; | |
return conditional ? ( | |
<PassedComponent {...{ ...passedProps, ...(conditionalProps as object) }} /> | |
) : ( | |
<PassedComponent {...passedProps} /> | |
); | |
} | |
}; | |
export function test() { | |
const NewTabLinkWithConditional = withConditional(NewTabLink); | |
return ( | |
<NewTabLinkWithConditional | |
href="asd" | |
conditional={true} | |
conditionalProps={{ | |
onClick: (e: React.FormEvent<HTMLAnchorElement>) => { | |
console.log(e, 'Been clicked!'); | |
} | |
}} | |
/> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment