Skip to content

Instantly share code, notes, and snippets.

@tanisha03
Created January 6, 2022 13:06
Show Gist options
  • Select an option

  • Save tanisha03/3ddb142ffff5d93bb55963209a89868c to your computer and use it in GitHub Desktop.

Select an option

Save tanisha03/3ddb142ffff5d93bb55963209a89868c to your computer and use it in GitHub Desktop.
import Pagination from 'components/Pagination';
import PropTypes from 'prop-types';
import React from 'react';
import styled from 'styled-components';
const Th = styled.th`
padding: 13px 10px;
border: 1px solid #e1dddd;
font-size: 14px;
text-align: ${props =>
props.ui ? (props.ui.align ? props.ui.align : 'left') : 'left'};
font-weight: 700;
width: ${props =>
props.ui ? (props.ui.width ? props.ui.width : 'auto') : 'auto'};
`;
const Tr = styled.tr``;
const Tbody = styled.tbody``;
const Td = styled.td`
padding: 13px 10px;
border: 1px solid #e1dddd;
background-color: #eff6ff;
font-size: 14px;
text-align: ${props =>
props.ui ? (props.ui.align ? props.ui.align : 'left') : 'left'};
width: ${props =>
props.ui ? (props.ui.width ? props.ui.width : 'auto') : 'auto'};
`;
const Table = styled.table`
width: 100%;
border: 1px solid;
border-collapse: collapse;
border-color: #e1dddd;
background-color: #fff;
a {
color: #0366d6;
}
${Tr} {
&:nth-child(2n + 1) {
${Td} {
background-color: ${props => (props.striped ? '#fff' : 'eff6ff')};
}
}
&:nth-child(2n) {
${Td} {
background-color: ${props => (props.striped ? '#f4f4f4' : 'eff6ff')};
}
}
}
`;
const Thead = styled.thead`
& > tr {
background: #f4f4f4;
color: #24292e;
}
`;
export function SimpleTable({ columns, data, isLoaded, striped }) {
const thead = columns.map((item, i) => (
<Th ui={item.ui} key={`th-${i}`}>
{item.name}
</Th>
));
return (
<Table striped={striped}>
<Thead>
<Tr>{thead}</Tr>
</Thead>
<Tbody>
{data.map((row, ri) => {
return (
<Tr key={`tr-${ri}`}>
{columns.map((col, ci) => {
return (
<Td ui={col.ui} key={`td-${ci}`}>
{col.mapping !== null
? col.fn
? col.fn(row)
: row[col.mapping]
: col.fn(row)}
</Td>
);
})}
</Tr>
);
})}
{isLoaded === true && data.length === 0 && (
<Tr>
<Td colSpan={columns.length}>No data available</Td>
</Tr>
)}
{isLoaded === false && (
<Tr>
<Td colSpan={columns.length}>Loading...</Td>
</Tr>
)}
</Tbody>
</Table>
);
}
export function TableWithPagination(props) {
return (
<div style={{ width: props.width || '100%' }}>
<SimpleTable {...props} />
<Pagination
align={props.paginationAlign}
activePage={props.activePage}
itemsCountPerPage={props.limit}
totalItemsCount={props.count}
pageRangeDisplayed={5}
onChange={props.onPaginationChange}
/>
</div>
);
}
Table.propTypes = {
columns: PropTypes.arrayOf(PropTypes.object),
data: PropTypes.arrayOf(PropTypes.object),
isLoaded: PropTypes.bool
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment