@unikue/react-condition
Version:
Render components conditionally for react
305 lines (241 loc) ⢠6.74 kB
Markdown
# @unikue/react-condition
[](https://npmjs.org/package/@unikue/react-condition)
[](LICENSE.txt)
[](https://npmjs.org/package/@unikue/react-condition)
š
Render components conditionally for React š
## Features
ā
Supports 'If' conditions
ā
Supports 'If'-'Then' conditions
ā
Supports 'If'-'Else' conditions
ā
Supports 'If'-'Then'-Else' conditions
ā
Supports 'For' conditions
ā
Supports 'Do' conditions
ā
Supports 'While' conditions
ā
Supports 'MapIterator' conditions
ā
Supports 'SetIterator' conditions
ā
Supports 'ObjectIterator' conditions
## Quickstart
You can install this package in your React project as follows:
```bash
$ npm install @unikue/react-condition --save
```
Then, you may import components as follows:
```jsx | pure
import {If, For, Switch, Do, While} from '@unikue/react-condition';
import {MapIterator, SetIterator, ObjectIterator} from '@unikue/react-condition';
```
Enjoy your coding journey with `react-condition`. āļø
## Example
### If
> Both of the `If.Then` and `If.Else` have a `render` property (() => React.ReactNode), thus you can customize the rendering contents instead of the React `children`.
#### `If` statement
```jsx | pure
import React from 'react';
import {If} from '@unikue/react-condition';
export default () => {
const param = true;
return (
<If condition={param}>
<span>Hello World</span>
</If>
);
}
```
#### `If`-`Then` statement
```jsx | pure
import React from 'react';
import {If} from '@unikue/react-condition';
export default () => {
const param = 1;
return (
<If condition={param}>
<If.Then>
<span>Hello World</span>
</If.Then>
</If>
);
}
```
#### `If`-`Else` statement
```jsx | pure
import React from 'react';
import {If} from '@unikue/react-condition';
export default () => {
const param = false;
return (
<If condition={param}>
<span>Hello World</span>
<If.Else>
<span>Hello Unikue</span>
</If.Else>
</If>
);
}
```
#### `If`-`Then`-`Else` statement
```jsx | pure
import React from 'react';
import {If} from '@unikue/react-condition';
export default () => {
const param = false;
return (
<If condition={param}>
<If.Then>
<span>Hello World</span>
</If.Then>
<If.Else>
<span>Hello Unikue</span>
</If.Else>
</If>
);
}
```
### `For` statement
```jsx | pure
import React from 'react';
import {For} from '@unikue/react-condition';
export default () => {
return (
<For
of={['foo', 'bar']}
render={(item, index) => {
return (
<span key={index}>Hello, {item}</span>
);
}}
/>
);
}
```
### `Switch` statement
> Both of the `Switch.Case` and `Switch.Default` have a `render` property (() => React.ReactNode), thus you can customize the rendering contents instead of the React `children`.
```jsx | pure
import React from 'react';
import {Switch} from '@unikue/react-condition';
export default () => {
const username = 'admin';
return (
<Switch>
<Switch.Case condition={username.includes('admin')}>
<span>admin</span>
</Switch.Case>
<Switch.Case condition={username.includes('guest')}>
<span>guest</span>
</Switch.Case>
<Switch.Default>
<span>root</span>
</Switch.Default>
</Switch>
);
}
```
### `Do` statement
```jsx | pure
import React from 'react';
import {Do} from '@unikue/react-condition';
export default () => {
let param = 0;
return (
<Do
condition={() => {
return param < 2;
}}
render={(index) => {
param++;
return (
<span key={index}>Hello, {index}</span>
);
}}
/>
);
}
```
### `While` statement
```jsx | pure
import React from 'react';
import {While} from '@unikue/react-condition';
export default () => {
let param = 0;
return (
<While
condition={() => {
return param++ < 2;
}}
render={(index) => {
return (
<span key={index}>Hello, {index}</span>
);
}}
/>
);
}
```
### `MapIterator` statement
```jsx | pure
import React from 'react';
import {MapIterator} from '@unikue/react-condition';
export default () => {
const map = new Map([
['foo', 'bar'],
['hello', 'world'],
]);
return (
<MapIterator
of={map}
render={(value, key, index) => {
return (
<span key={index}>Hooray, {key}-{value}</span>
);
}}
/>
);
}
```
### `SetIterator` statement
```jsx | pure
import React from 'react';
import {SetIterator} from '@unikue/react-condition';
export default () => {
const set = new Set<string>([
'foo-bar',
'hello-world',
]);
return (
<SetIterator
of={set}
render={(item, index) => {
return (
<span key={index}>Hooray, {item}</span>
);
}}
/>
);
}
```
### `ObjectIterator` statement
```jsx | pure
import React from 'react';
import {ObjectIterator} from '@unikue/react-condition';
export default () => {
const param = {
'foo': 'bar',
'hello': 'world',
};
return (
<ObjectIterator
of={param}
render={(value, key, index) => {
return (
<span key={index}>Hooray, {key}-{value}</span>
);
}}
/>
);
}
```
## License
This project is under the [MIT License](https://mit-license.org/).
## Copyright
Beijing Unikue Network Technology Ltd.
## Website
- Unikue: [https://unikue.cn](https://unikue.cn)