Skip to content

Instantly share code, notes, and snippets.

@HenryNguyen5
Created October 20, 2017 20:38
Show Gist options
  • Save HenryNguyen5/bfab95b8ed30ec33706f369569122a63 to your computer and use it in GitHub Desktop.
Save HenryNguyen5/bfab95b8ed30ec33706f369569122a63 to your computer and use it in GitHub Desktop.
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