Skip to content

Commit c275d45

Browse files
committed
feat: add <If> component.
1 parent 4c60312 commit c275d45

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,36 @@ import Only from '@uiw/react-only-when'
2121
</Only>
2222
```
2323

24+
## \<If>
25+
26+
React component that renders the children if the `condition` prop is `true`.
27+
28+
```jsx
29+
import { If } from '@uiw/react-only-when';
30+
31+
<div>
32+
<If
33+
condition={props.error}
34+
render={() => (
35+
<h1>{props.error}</h1>
36+
)}
37+
/>
38+
<If condition={props.error}>
39+
<h1>{props.error}</h1>
40+
</If>
41+
</div>
42+
```
43+
44+
Or you could just use plain JavaScript:
45+
46+
```jsx
47+
<div>
48+
{props.error && (
49+
<h1>{props.error}</h1>
50+
)}
51+
</div>
52+
```
53+
2454
## Example
2555

2656
```jsx

src/If.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ReactElement } from "react";
2+
import { FC, PropsWithChildren } from "react";
3+
4+
export interface IfProps {
5+
readonly condition?: boolean;
6+
readonly render?: () => ReactElement;
7+
}
8+
9+
export const If: FC<PropsWithChildren<IfProps>> = (props) => props.condition ? (props.render ? props.render() : props.children as ReactElement) : null;

src/__test__/index.test.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,47 @@
11
/* eslint-disable jest/no-conditional-expect */
22
import TestRenderer from 'react-test-renderer';
33
import Only from '../';
4+
import { If } from '../';
5+
6+
describe('<If />', () => {
7+
8+
it('Not rendering children', () => {
9+
const component = TestRenderer.create(
10+
<If condition={false}>
11+
<span id="child" />
12+
</If>,
13+
);
14+
let only = component.toJSON();
15+
expect(only).toBeNull();
16+
});
17+
18+
it('rendering children', () => {
19+
const component = TestRenderer.create(
20+
<If condition={true}>
21+
<span id="child" />
22+
</If>,
23+
);
24+
let only = component.toJSON();
25+
26+
if (only && !Array.isArray(only)) {
27+
expect(only.type).toEqual('span');
28+
expect(only.props.id).toEqual('child');
29+
}
30+
});
31+
32+
it('render props', () => {
33+
const component = TestRenderer.create(
34+
<If condition={true} render={() => <span id="child" />} />,
35+
);
36+
let only = component.toJSON();
37+
38+
if (only && !Array.isArray(only)) {
39+
expect(only.type).toEqual('span');
40+
expect(only.props.id).toEqual('child');
41+
}
42+
});
43+
44+
})
445

546
describe('<Only />', () => {
647
it('Not rendering children', () => {

src/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import React from 'react';
1+
import React, { PropsWithChildren } from 'react';
2+
import { If } from './If';
3+
4+
export * from './If';
25

36
export interface OnlyWhenProps {
47
/** A single child element */
@@ -17,7 +20,7 @@ export interface OnlyWhenProps {
1720
hiddenMode?: 'null' | 'display' | 'visibility' | 'css';
1821
}
1922

20-
export default function OnlyWhen(props: OnlyWhenProps) {
23+
export default function OnlyWhen(props: PropsWithChildren<OnlyWhenProps>) {
2124
const { children, when, hiddenMode, className } = props;
2225
const singleChild = React.Children.only(children);
2326
const { style, ...restOfChildProps } = singleChild.props;
@@ -36,7 +39,7 @@ export default function OnlyWhen(props: OnlyWhenProps) {
3639
}
3740
}
3841
const cloned = React.cloneElement(singleChild, extendedProps);
39-
const toHide = keepNode ? cloned : null;
42+
const toHide = <If condition={keepNode}>{cloned}</If>;
4043

4144
return when ? singleChild : toHide;
4245
}

0 commit comments

Comments
 (0)