Skip to content

Commit

Permalink
feat: add <If> component.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 8, 2023
1 parent 4c60312 commit c275d45
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,36 @@ import Only from '@uiw/react-only-when'
</Only>
```

## \<If>

React component that renders the children if the `condition` prop is `true`.

```jsx
import { If } from '@uiw/react-only-when';

<div>
<If
condition={props.error}
render={() => (
<h1>{props.error}</h1>
)}
/>
<If condition={props.error}>
<h1>{props.error}</h1>
</If>
</div>
```

Or you could just use plain JavaScript:

```jsx
<div>
{props.error && (
<h1>{props.error}</h1>
)}
</div>
```

## Example

```jsx
Expand Down
9 changes: 9 additions & 0 deletions src/If.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ReactElement } from "react";
import { FC, PropsWithChildren } from "react";

export interface IfProps {
readonly condition?: boolean;
readonly render?: () => ReactElement;
}

export const If: FC<PropsWithChildren<IfProps>> = (props) => props.condition ? (props.render ? props.render() : props.children as ReactElement) : null;
41 changes: 41 additions & 0 deletions src/__test__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
/* eslint-disable jest/no-conditional-expect */
import TestRenderer from 'react-test-renderer';
import Only from '../';
import { If } from '../';

describe('<If />', () => {

it('Not rendering children', () => {
const component = TestRenderer.create(
<If condition={false}>
<span id="child" />
</If>,
);
let only = component.toJSON();
expect(only).toBeNull();
});

it('rendering children', () => {
const component = TestRenderer.create(
<If condition={true}>
<span id="child" />
</If>,
);
let only = component.toJSON();

if (only && !Array.isArray(only)) {
expect(only.type).toEqual('span');
expect(only.props.id).toEqual('child');
}
});

it('render props', () => {
const component = TestRenderer.create(
<If condition={true} render={() => <span id="child" />} />,
);
let only = component.toJSON();

if (only && !Array.isArray(only)) {
expect(only.type).toEqual('span');
expect(only.props.id).toEqual('child');
}
});

})

describe('<Only />', () => {
it('Not rendering children', () => {
Expand Down
9 changes: 6 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import React from 'react';
import React, { PropsWithChildren } from 'react';
import { If } from './If';

export * from './If';

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

export default function OnlyWhen(props: OnlyWhenProps) {
export default function OnlyWhen(props: PropsWithChildren<OnlyWhenProps>) {
const { children, when, hiddenMode, className } = props;
const singleChild = React.Children.only(children);
const { style, ...restOfChildProps } = singleChild.props;
Expand All @@ -36,7 +39,7 @@ export default function OnlyWhen(props: OnlyWhenProps) {
}
}
const cloned = React.cloneElement(singleChild, extendedProps);
const toHide = keepNode ? cloned : null;
const toHide = <If condition={keepNode}>{cloned}</If>;

return when ? singleChild : toHide;
}

0 comments on commit c275d45

Please sign in to comment.