Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MF-647: Introduce PatientTable component #20

Merged
merged 5 commits into from
Jul 19, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add support for pagination and search
  • Loading branch information
samuelmale committed Jul 19, 2021
commit 7fdf50b657b2b43c2f9056871e57e1896bf4ba03
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.searchOverrides {
width: 50% !important;
}
Original file line number Diff line number Diff line change
@@ -7,57 +7,99 @@ import DataTable, {
TableHeader,
TableRow,
} from 'carbon-components-react/lib/components/DataTable';
import React from 'react';
import Pagination from 'carbon-components-react/lib/components/Pagination';
import Search from 'carbon-components-react/lib/components/Search';
import { debounce } from 'lodash';
import React, { useMemo } from 'react';
import styles from './patient-table.component.scss';

const PatientTable: React.FC<PatientTableProps> = ({ patients, columns, search, pagination }) => {
const rows: Array<any> = useMemo(
() =>
patients.map((patient) => {
const row = {};
columns.forEach((column) => {
row['id'] = patient['id'] || patient['uuid'];
row[column.key] = column.getValue ? column.getValue(patient) : patient[column.key];
});
return row;
}),
[patients, columns],
);
const handleSearch = React.useMemo(() => debounce((searchTerm) => search.onSearch(searchTerm), 300), []);

const PatientTable: React.FC<{ patients: Array<Object>; columns: Array<PatientTableColumn> }> = ({
patients,
columns,
}) => {
const rows = patients.map((patient) => {
const row = {};
columns.forEach((column) => {
row['id'] = patient['id'] || patient['uuid'];
row[column.key] = column.getValue ? column.getValue(patient) : patient[column.key];
});
return row;
});
return (
<TableContainer>
<DataTable rows={rows as any} headers={columns as any} isSortable={true} size="normal" useZebraStyles={true}>
<div>
<div id="table-tool-bar" style={{ display: 'flex', flexDirection: 'row-reverse' }}>
<Search
id="patient-list-search"
labelText=""
placeholder={search.placeHolder}
onChange={(evnt) => handleSearch(evnt.target.value)}
className={styles.searchOverrides}
/>
</div>
<DataTable rows={rows} headers={columns} isSortable={true} size="normal" useZebraStyles={true}>
{({ rows, headers, getHeaderProps, getTableProps }) => (
<Table {...getTableProps()}>
<TableHead>
<TableRow>
{headers.map((header) => (
<TableHeader
{...getHeaderProps({
header,
isSortable: header.isSortable,
})}>
{header.header?.content ?? header.header}
</TableHeader>
))}
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
<TableRow key={row.id}>
{row.cells.map((cell) => (
<TableCell key={cell.id}>{cell.value?.content ?? cell.value}</TableCell>
<TableContainer>
<Table {...getTableProps()}>
<TableHead>
<TableRow>
{headers.map((header) => (
<TableHeader
{...getHeaderProps({
header,
isSortable: header.isSortable,
})}>
{header.header?.content ?? header.header}
</TableHeader>
))}
</TableRow>
))}
</TableBody>
</Table>
</TableHead>
<TableBody>
{rows.map((row) => (
<TableRow key={row.id}>
{row.cells.map((cell) => (
<TableCell key={cell.id}>{cell.value?.content ?? cell.value}</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
)}
</DataTable>
</TableContainer>
{pagination.usePagination && (
<Pagination
page={pagination.currentPage}
pageSize={pagination.pageSize}
pageSizes={[10, 20, 30, 40, 50]}
totalItems={pagination.totalItems}
onChange={pagination.onChange}
/>
)}
</div>
);
};

interface PatientTableProps {
patients: Array<Object>;
columns: Array<PatientTableColumn>;
search: {
onSearch: (searchTerm: string) => {};
placeHolder: string;
};
pagination: {
usePagination: boolean;
currentPage: number;
onChange: (props: any) => {};
pageSize: number;
totalItems: number;
};
}
export interface PatientTableColumn {
key: string;
label: string;
header: string;
getValue?: (patient) => any;
}