Skip to content

Commit

Permalink
feat: add empty state to table
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-foucault committed Jan 10, 2022
1 parent 25b7a7a commit 5bf2869
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions app/components/Table/Table.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import React from "react";

interface Column {
title: string;
}

interface Props {
columns: Column[];
emptyStateContents?: React.ReactElement;
}

const Table: React.FC<Props> = ({ columns, children }) => {
const Table: React.FC<Props> = ({
columns,
children,
emptyStateContents = <span className="no-results">No results found.</span>,
}) => {
const rows = React.Children.toArray(children);

return (
<table className="bc-table">
{/* class name is used to increase specificity of CSS selectors and override defaults */}
Expand All @@ -17,7 +26,15 @@ const Table: React.FC<Props> = ({ columns, children }) => {
))}
</tr>
</thead>
<tbody>{children}</tbody>
<tbody>
{rows.length > 0 ? (
rows
) : (
<tr>
<td colSpan={columns.length}>{emptyStateContents}</td>
</tr>
)}
</tbody>
<style jsx>{`
table.bc-table {
margin-top: 1rem;
Expand Down Expand Up @@ -67,6 +84,13 @@ const Table: React.FC<Props> = ({ columns, children }) => {
:global(td:first-child) {
border-left: 1px solid #939393;
}
:global(.no-results) {
width: 100%;
display: inline-block;
text-align: center;
font-style: italic;
}
`}</style>
</table>
);
Expand Down

0 comments on commit 5bf2869

Please sign in to comment.