-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.tsx
39 lines (30 loc) · 768 Bytes
/
index.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
/* eslint-disable react/jsx-no-useless-fragment */
import * as React from 'react';
type WhenProps = {
children: React.ReactNode,
isTrue?: boolean,
limit?: number,
};
const RenderWhen = ({ limit, isTrue, children }:WhenProps) => {
const list:React.ReactNode[] = [];
if (isTrue !== true) {
return null;
}
React.Children.map(children, (child:any) => {
const { isTrue: isChildTrue } = child?.props || {};
if (isChildTrue === true && list.length < limit) {
list.push(child);
}
});
return (
<>
{list}
</>
);
};
RenderWhen.defaultProps = {
limit: 1,
isTrue: true,
};
RenderWhen.If = ({ children, isTrue }) => children;
export default RenderWhen;