Skip to content

Instantly share code, notes, and snippets.

@QuadFlask
Created November 15, 2018 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuadFlask/0574218bb7de76907188d86f6a22520a to your computer and use it in GitHub Desktop.
Save QuadFlask/0574218bb7de76907188d86f6a22520a to your computer and use it in GitHub Desktop.
interface WithLoadingProps {
    loading: boolean;
}

const withMaybe = <P extends object>(predicate: (props: P) => boolean) =>
    (Component: React.ComponentType<P>) =>
        (props: P) => predicate(props) ? <Component {...props}/> : null;

const withEither = <P extends object>(predicate: (props: P) => boolean, EitherComponent: React.ComponentType) =>
    (Component: React.ComponentType<P>) =>
        (props: P) => predicate(props) ? <EitherComponent/> : <Component {...props}/>;

const withLoading = <P extends WithLoadingProps>(Component: React.ComponentType<P>) =>
    withEither((props: P) => props.loading, () => <ActivityIndicator size="small" color="tomato"/>)(Component);


interface ITest {
    loading: boolean
    text: string
}

class Test extends React.Component<ITest> {
    render() {
        return <View>
            <Text>{this.props.text}</Text>
        </View>
    }
}

const LoadingTest = withEither((props: ITest) => props.loading, () => <ActivityIndicator size="small" color="tomato"/>)(Test);

const LoadingTest2 = withLoading(Test);
const withMaybe = (predicate) =>
    (Component) =>
        (props) => predicate(props) ? <Component {...props}/> : null;

const withEither = (predicate, EitherComponent) =>
    (Component) =>
        (props) => predicate(props) ? <EitherComponent/> : <Component {...props}/>;

const withLoading = (Component) =>
    withEither((props) => props.loading, () => <div>loading...</div>)(Component);


class Test extends React.Component {
    render() {
        return <div>
            <div>contents:</div>
            <div>{this.props.text}</div>
        </div>
    }
}

const LoadingTest = withEither((props) => props.loading, () => <div>loading...</div>)(Test);

const LoadingTest2 = withLoading(Test);


@withLoading
class Test3 extends React.Component {
    render() {
        return <div>
            this is test3!!
        </div>
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment