File tree Expand file tree Collapse file tree 4 files changed +86
-3
lines changed Expand file tree Collapse file tree 4 files changed +86
-3
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,36 @@ import Only from '@uiw/react-only-when'
21
21
< / Only>
22
22
```
23
23
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
+
24
54
## Example
25
55
26
56
``` jsx
Original file line number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change 1
1
/* eslint-disable jest/no-conditional-expect */
2
2
import TestRenderer from 'react-test-renderer' ;
3
3
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
+ } )
4
45
5
46
describe ( '<Only />' , ( ) => {
6
47
it ( 'Not rendering children' , ( ) => {
Original file line number Diff line number Diff line change 1
- import React from 'react' ;
1
+ import React , { PropsWithChildren } from 'react' ;
2
+ import { If } from './If' ;
3
+
4
+ export * from './If' ;
2
5
3
6
export interface OnlyWhenProps {
4
7
/** A single child element */
@@ -17,7 +20,7 @@ export interface OnlyWhenProps {
17
20
hiddenMode ?: 'null' | 'display' | 'visibility' | 'css' ;
18
21
}
19
22
20
- export default function OnlyWhen ( props : OnlyWhenProps ) {
23
+ export default function OnlyWhen ( props : PropsWithChildren < OnlyWhenProps > ) {
21
24
const { children, when, hiddenMode, className } = props ;
22
25
const singleChild = React . Children . only ( children ) ;
23
26
const { style, ...restOfChildProps } = singleChild . props ;
@@ -36,7 +39,7 @@ export default function OnlyWhen(props: OnlyWhenProps) {
36
39
}
37
40
}
38
41
const cloned = React . cloneElement ( singleChild , extendedProps ) ;
39
- const toHide = keepNode ? cloned : null ;
42
+ const toHide = < If condition = { keepNode } > { cloned } </ If > ;
40
43
41
44
return when ? singleChild : toHide ;
42
45
}
You can’t perform that action at this time.
0 commit comments