Skip to content

Commit 31f487c

Browse files
Khan GhaziKhan Ghazi
authored andcommitted
table component developed
1 parent 1c23bab commit 31f487c

File tree

10 files changed

+307
-85
lines changed

10 files changed

+307
-85
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.tabSize": 2
3+
}

README.md

Lines changed: 107 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,107 @@
1-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2-
3-
## Available Scripts
4-
5-
In the project directory, you can run:
6-
7-
### `npm start`
8-
9-
Runs the app in the development mode.<br />
10-
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11-
12-
The page will reload if you make edits.<br />
13-
You will also see any lint errors in the console.
14-
15-
### `npm test`
16-
17-
Launches the test runner in the interactive watch mode.<br />
18-
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19-
20-
### `npm run build`
21-
22-
Builds the app for production to the `build` folder.<br />
23-
It correctly bundles React in production mode and optimizes the build for the best performance.
24-
25-
The build is minified and the filenames include the hashes.<br />
26-
Your app is ready to be deployed!
27-
28-
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29-
30-
### `npm run eject`
31-
32-
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33-
34-
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35-
36-
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37-
38-
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39-
40-
## Learn More
41-
42-
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43-
44-
To learn React, check out the [React documentation](https://reactjs.org/).
45-
46-
### Code Splitting
47-
48-
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49-
50-
### Analyzing the Bundle Size
51-
52-
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53-
54-
### Making a Progressive Web App
55-
56-
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57-
58-
### Advanced Configuration
59-
60-
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61-
62-
### Deployment
63-
64-
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65-
66-
### `npm run build` fails to minify
67-
68-
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
1+
## Table Component
2+
3+
Usage:
4+
5+
```
6+
import Table from '/components/Table';
7+
8+
<Table data={data} cols={tableConstants(handleEdit)} isDark hoverable striped bordered={false} />
9+
```
10+
11+
Create a Table Constant which will be supplied to Table and data will be rendered automatically
12+
13+
```
14+
import React from 'react';
15+
16+
export const tableConstants = (handleEdit) => {
17+
return [
18+
{
19+
title: 'ID',
20+
key: 'id',
21+
render: rowData => {
22+
return <span>{rowData.id}</span>;
23+
},
24+
},
25+
{
26+
title: 'Name',
27+
key: 'name',
28+
render: rowData => {
29+
return <span>{rowData.name}</span>;
30+
},
31+
},
32+
{
33+
title: 'Category',
34+
key: 'category',
35+
render: rowData => {
36+
return <span>{rowData.category}</span>;
37+
},
38+
},
39+
{
40+
title: 'Country',
41+
key: 'country',
42+
render: rowData => {
43+
return <span>{rowData.country}</span>;
44+
},
45+
},
46+
{
47+
title: 'Action',
48+
key: 'action',
49+
render: rowData => {
50+
return <button onClick={() => handleEdit}>Edit</button>;
51+
},
52+
},
53+
];
54+
};
55+
```
56+
57+
Here `tableConstants()` is a javascript function which returns an array of objects or columns to be displayed on table. This function accepts parameters whatever you need to have in constant function to perform any additional task such as any action to be taken or some additional data to be displayed or anything. Making this as function gives developers more power to take control on table.
58+
59+
Let's have a look into a single item for the table constants array:
60+
61+
```
62+
{
63+
title: 'ID',
64+
key: 'id',
65+
render: rowData => {
66+
return <span>{rowData.id}</span>;
67+
},
68+
}
69+
```
70+
71+
Here render: () : receives 'rowData' which is the current iteration item for received from table component to this constants so you can dig down to any level of the JSON to display your desired input.
72+
73+
```
74+
Eg: You have a nested JSON data as below
75+
{
76+
name: 'Test',
77+
category: {
78+
name: 'Cateogory Name',
79+
subCategory: {
80+
name: 'Sub Category Name
81+
}
82+
}
83+
}
84+
85+
and you have to show Sub Category name to any column you will have to refer it constant item as below:
86+
87+
{
88+
title: 'Sub Category',
89+
key: 'sub-category',
90+
render: rowData => {
91+
return <span>{rowData.category.subCategory.name}</span>;
92+
},
93+
}
94+
95+
In this way you will not have to re-format the data everytime you send to table
96+
```
97+
98+
## Props
99+
100+
| Property | Description | Type | Default | Required |
101+
| ----------------- | ---------------------------------------------------------------------- | ------- | ------- | -------- |
102+
| data | Data array to be displayed on table | array[] | | Yes |
103+
| cols | table constant function which returns array of columns to be displayed | array[] | | Yes |
104+
| isDark | To enable dark mode to table | boolean | false | No |
105+
| hoverable | To hoverable bootstrap table | boolean | false | No |
106+
| striped | To striped bootstrap table | boolean | false | No |
107+
| bordered | To bordered bootstrap table | boolean | true | No |

package-lock.json

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

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"@testing-library/jest-dom": "^4.2.4",
77
"@testing-library/react": "^9.5.0",
88
"@testing-library/user-event": "^7.2.1",
9+
"bootstrap": "^4.4.1",
10+
"prop-types": "^15.7.2",
911
"react": "^16.13.1",
1012
"react-dom": "^16.13.1",
1113
"react-scripts": "3.4.1"

src/App.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
import React from 'react';
2-
import logo from './logo.svg';
3-
import './App.css';
2+
3+
import Dashboard from './pages/Dashboard';
44

55
function App() {
66
return (
7-
<div className="App">
8-
<header className="App-header">
9-
<img src={logo} className="App-logo" alt="logo" />
10-
<p>
11-
Edit <code>src/App.js</code> and save to reload.
12-
</p>
13-
<a
14-
className="App-link"
15-
href="https://reactjs.org"
16-
target="_blank"
17-
rel="noopener noreferrer"
18-
>
19-
Learn React
20-
</a>
21-
</header>
7+
<div className="container-fluid">
8+
<Dashboard />
229
</div>
2310
);
2411
}

src/components/Table/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types'
3+
4+
const Table = ({ cols, data, bordered, hoverable, striped, isDark }) => {
5+
return (
6+
<div class="table-responsive">
7+
<table className={`table ${bordered ? 'table-bordered' : 'table-borderless'} ${hoverable && 'table-hover'} ${striped && 'table-striped'} ${isDark && 'table-dark'}`}>
8+
<thead>
9+
<tr>
10+
{cols.map((headerItem, index) => (
11+
<th key={index}>{headerItem.title}</th>
12+
))}
13+
</tr>
14+
</thead>
15+
<tbody>
16+
{data.map((item, index) => (
17+
<tr key={index}>
18+
{cols.map((col, key) => (
19+
<td key={key}>{col.render(item)}</td>
20+
))}
21+
</tr>
22+
))}
23+
</tbody>
24+
</table>
25+
</div>
26+
)
27+
}
28+
29+
Table.propTypes = {
30+
cols: PropTypes.array.isRequired,
31+
data: PropTypes.array.isRequired,
32+
bordered: PropTypes.bool,
33+
hoverable: PropTypes.bool,
34+
striped: PropTypes.bool,
35+
isDark: PropTypes.bool,
36+
}
37+
38+
Table.defaultProps = {
39+
bordered: true,
40+
hoverable: false,
41+
striped: false,
42+
isDark: false,
43+
}
44+
45+
export default Table;

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import './index.css';
44
import App from './App';
55
import * as serviceWorker from './serviceWorker';
66

7+
import 'bootstrap/dist/css/bootstrap.css';
8+
79
ReactDOM.render(
810
<React.StrictMode>
911
<App />

src/pages/Dashboard.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from 'react';
2+
3+
import Table from '../components/Table';
4+
import { data } from './mockData';
5+
import { tableConstants } from './tableConstant';
6+
7+
8+
const Dashboard = () => {
9+
10+
const handleEdit = (item) => () => {
11+
// write your logic
12+
alert(JSON.stringify(item))
13+
}
14+
15+
return (
16+
<div className='row'>
17+
<div className='col-sm-6'>
18+
<h4>Default Table</h4>
19+
<Table cols={tableConstants(handleEdit)} data={data} />
20+
</div>
21+
22+
23+
<div className='col-sm-6'>
24+
25+
<h4>Dark Table</h4>
26+
<Table cols={tableConstants(handleEdit)} data={data} isDark />
27+
</div>
28+
29+
<div className='col-sm-6'>
30+
<h4>Borderless Table</h4>
31+
<Table cols={tableConstants(handleEdit)} data={data} bordered={false} />
32+
</div>
33+
34+
<div className='col-sm-6'>
35+
<h4>Striped Table</h4>
36+
<Table cols={tableConstants(handleEdit)} data={data} striped />
37+
</div>
38+
39+
<div className='col-sm-6'>
40+
<h4>Hoverable Table</h4>
41+
<Table cols={tableConstants(handleEdit)} data={data} hoverable />
42+
</div>
43+
</div >
44+
45+
)
46+
}
47+
48+
export default Dashboard;

src/pages/mockData.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
export const data = [
2+
{
3+
"id": 1,
4+
"name": "Leanne Graham",
5+
"username": "Bret",
6+
"email": "Sincere@april.biz",
7+
"phone": "1-770-736-8031 x56442",
8+
"website": "hildegard.org",
9+
},
10+
{
11+
"id": 2,
12+
"name": "Ervin Howell",
13+
"username": "Antonette",
14+
"email": "Shanna@melissa.tv",
15+
"phone": "010-692-6593 x09125",
16+
"website": "anastasia.net",
17+
},
18+
{
19+
"id": 3,
20+
"name": "Clementine Bauch",
21+
"username": "Samantha",
22+
"email": "Nathan@yesenia.net",
23+
"phone": "1-463-123-4447",
24+
"website": "ramiro.info",
25+
},
26+
{
27+
"id": 4,
28+
"name": "Patricia Lebsack",
29+
"username": "Karianne",
30+
"email": "Julianne.OConner@kory.org",
31+
"phone": "493-170-9623 x156",
32+
"website": "kale.biz",
33+
},
34+
{
35+
"id": 5,
36+
"name": "Chelsey Dietrich",
37+
"username": "Kamren",
38+
"email": "Lucio_Hettinger@annie.ca",
39+
"phone": "(254)954-1289",
40+
"website": "demarco.info",
41+
}
42+
]

0 commit comments

Comments
 (0)