-
Notifications
You must be signed in to change notification settings - Fork 0
/
Show.tsx
39 lines (33 loc) · 840 Bytes
/
Show.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import React, { Children, ReactNode } from 'react';
interface ShowProps {
children: ReactNode;
}
interface IfProps {
isTrue: boolean;
children: ReactNode;
}
interface ElseProps {
render: boolean;
children: ReactNode;
}
const Show = (props: ShowProps) => {
let when: ReactNode = null;
let otherwise = null;
Children.forEach(props.children, (child) => {
if (React.isValidElement(child)) {
if (!child.props.isTrue) {
otherwise = child;
} else if (!when && child.props.isTrue) {
when = child;
}
}
});
return when || otherwise;
}
Show.If = ({ isTrue, children }: IfProps) => {
return isTrue && children;
}
Show.Else = ({ render, children }: ElseProps) => {
return render && children;
}
export default Show;