Skip to content

Commit eead5f3

Browse files
authored
feat: updates ReactIf component to support else and NULL condition (#27)
2 parents ea68194 + c30eadc commit eead5f3

File tree

8 files changed

+120
-23
lines changed

8 files changed

+120
-23
lines changed

apps/react-kit-demo/src/app/all-books/AllBooks.tsx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Grid, Paper } from '@mui/material';
1+
import { Divider, Grid, Paper } from '@mui/material';
22
import { DataGrid, GridCellParams, GridColDef } from '@mui/x-data-grid';
33
import { useEffect, useState } from 'react';
44
import { Link } from 'react-router-dom';
@@ -23,23 +23,27 @@ export default function AllBooks() {
2323
}, []);
2424

2525
return (
26-
<Grid container style={{ height: 'auto', width: '100%', textAlign: 'center' }}>
27-
<Grid item xs={12}>
28-
<h2>All Books</h2>
26+
<Grid container sx={{ m: 3 }}>
27+
<Grid size={10} sx={{ flexGrow: 1 }}>
28+
<h2 style={{ textAlign: 'center' }}>Books Demo</h2>
29+
<Divider sx={{ mb: 4 }} />
2930

30-
<Paper elevation={24} style={{ marginTop: '2rem', display: 'flex', justifyContent: 'center' }}>
31-
<DataGrid
32-
initialState={{ pagination: { paginationModel: { pageSize: 5 } } }}
33-
getRowId={(row) => row.id}
34-
pageSizeOptions={[5, 10, 20, 50, 100]}
35-
disableRowSelectionOnClick={true}
36-
density={'comfortable'}
37-
loading={loading}
38-
rows={data ?? []}
39-
columns={columns}
40-
autoHeight={true}
41-
/>
31+
<Paper elevation={24}>
32+
<div style={{ display: 'flex', flexDirection: 'column' }}>
33+
<DataGrid
34+
initialState={{ pagination: { paginationModel: { pageSize: 5 } } }}
35+
getRowId={(row) => row.id}
36+
pageSizeOptions={[5, 10, 20, 50, 100]}
37+
disableRowSelectionOnClick={true}
38+
density={'comfortable'}
39+
loading={loading}
40+
rows={data ?? []}
41+
columns={columns}
42+
/>
43+
</div>
4244
</Paper>
45+
46+
<Divider sx={{ mb: 3 }} />
4347
</Grid>
4448
</Grid>
4549
);

apps/react-kit-demo/src/app/app.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default function App() {
1212
<Link to="/dialog">Dialog</Link> <br />
1313
<Link to="/circular-progress">Circular Progress</Link> <br />
1414
<Link to="/books">All Books</Link> <br />
15+
<Link to="/react-if">React If Demo</Link> <br />
1516
</ul>
1617
</div>
1718

apps/react-kit-demo/src/app/buttons/ButtonsDemo.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import {
99
SuccessButton,
1010
} from '@react-kit/react-kit';
1111
import { useNavigate } from 'react-router-dom';
12+
import { Divider } from '@mui/material';
1213

1314
export default function ButtonsDemo() {
1415
const navigate = useNavigate();
1516
return (
1617
<div style={{ marginInline: '1rem', textAlign: 'center' }}>
18+
<h2>Buttons Demo</h2>
19+
<Divider sx={{ mb: 3 }} />
1720
Cancel Button:
1821
<CancelButton onClick={() => console.log('Clicked Cancel Button')} name={'Cancel'}></CancelButton> <br />
1922
Delete Button:
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React, { useState } from 'react';
2+
import { DismissibleAlert, ManageButton, ReactIf } from '@react-kit/react-kit';
3+
import { Card, Divider } from '@mui/material';
4+
5+
export default function ReactIfDemo() {
6+
const [show, setShow] = useState(true);
7+
8+
return (
9+
<div style={{ textAlign: 'center' }}>
10+
<h2>React If Demo</h2>
11+
12+
<Divider sx={{ mb: 3 }} />
13+
<ManageButton startIcon={''} onClick={() => setShow((prev) => !prev)}>
14+
Click to toggle
15+
</ManageButton>
16+
17+
<Card sx={{ m: 5, mb: 5 }} elevation={24}>
18+
{/* Section 1: ReactIf with only condition */}
19+
<div style={{ marginTop: 32 }}>
20+
<h3>
21+
1. ReactIf with <code>condition</code> only
22+
</h3>
23+
<ReactIf condition={show}>
24+
<DismissibleAlert message={'Main content (condition is true)'} severity={'success'} dismissOnTimeOut={false} />
25+
</ReactIf>
26+
</div>
27+
</Card>
28+
29+
<Card sx={{ m: 3 }} elevation={24}>
30+
{/* Section 2: ReactIf with condition and else */}
31+
<div style={{ marginTop: 32 }}>
32+
<h3>
33+
2. ReactIf with <code>condition</code> and <code>else</code>
34+
</h3>
35+
<ReactIf
36+
condition={show}
37+
else={<DismissibleAlert message={'Else content (condition is false)'} severity={'warning'} dismissOnTimeOut={false} />}>
38+
<DismissibleAlert message={'Main content (condition is true)'} severity={'success'} dismissOnTimeOut={false} />
39+
</ReactIf>
40+
</div>
41+
</Card>
42+
</div>
43+
);
44+
}

apps/react-kit-demo/src/routes/Routes.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import DialogDemo from '../app/dialog/DialogDemo';
66
import CenterCircularProgressDemo from '../app/progress-bar/CenterCircularProgressDemo';
77
import AllBooks from '../app/all-books/AllBooks';
88
import App from '../app/app';
9+
import ReactIfDemo from '../app/react-if/ReactIfDemo';
910

1011
export const router = createBrowserRouter([
1112
{
@@ -40,6 +41,10 @@ export const router = createBrowserRouter([
4041
path: '/books',
4142
element: <AllBooks />,
4243
},
44+
{
45+
path: '/react-if',
46+
element: <ReactIfDemo />,
47+
},
4348
],
4449
},
4550
]);

pnpm-lock.yaml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

react-kit/src/lib/components/DismissibleAlert.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type DismissibleAlertProps = {
1717
dismissTimeOut?: number;
1818
};
1919

20-
export function DismissibleAlert(props: DismissibleAlertProps) {
20+
export function DismissibleAlert(props: Readonly<DismissibleAlertProps>) {
2121
const [open, setOpen] = useState(true);
2222
const [dismissible] = useState(props.dismissible ?? true);
2323
const [dismissOnTimeOut] = useState(props.dismissOnTimeOut ?? true);
Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,53 @@
1-
import React from 'react';
1+
import React, { ReactNode } from 'react';
2+
3+
type ReactIfProps = {
4+
/**
5+
* The condition that determines whether children should be rendered
6+
*/
7+
condition: boolean | null | undefined;
8+
9+
/**
10+
* Content to render when condition is true.
11+
* Can be either a ReactNode or a function returning a ReactNode
12+
*/
13+
children: ReactNode | (() => ReactNode);
14+
15+
/**
16+
* Optional content to render when condition is false
17+
*/
18+
else?: ReactNode | (() => ReactNode);
19+
};
220

321
/**
422
* Reusable If component, that renders content if the condition is true. Similar to *ngIf from Angular
523
*
6-
* @param props Properties of the React Node
24+
* @param props Properties of the component
725
*
26+
* @example
27+
* ```tsx
28+
* <ReactIf condition={isVisible}>
29+
* <div>Visible content</div>
30+
* </ReactIf>
31+
*
32+
* <ReactIf condition={isVisible} else={<div>Alternative content</div>}>
33+
* <div>Main content</div>
34+
* </ReactIf>
35+
*```
836
* @author Pavan Kumar Jadda
937
* @since 0.1.0
1038
*/
11-
export function ReactIf(props: { condition: boolean | undefined; children: React.ReactNode }): React.ReactNode {
12-
return props.condition === undefined || !props.condition ? null : props.children;
39+
export function ReactIf(props: ReactIfProps): React.ReactNode {
40+
const { condition, children, else: elseContent } = props;
41+
42+
// Check if condition is true
43+
if (condition) {
44+
return typeof children === 'function' ? children() : children;
45+
}
46+
47+
// Render else content or null
48+
if (elseContent) {
49+
return typeof elseContent === 'function' ? elseContent() : elseContent;
50+
} else {
51+
return null;
52+
}
1353
}

0 commit comments

Comments
 (0)