Skip to content

Commit

Permalink
Add empty state to table (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavorosolem authored Sep 1, 2021
1 parent cbcecd2 commit 16a12a0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
10 changes: 10 additions & 0 deletions styleguide/src/Components/Table/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ export default {
// console.log('Selected rows data: ', selectedData)
// }
},
argTypes: {
emptyText: {
control: { type: 'text' }
}
},
parameters: {
layout: 'padded',
}
Expand All @@ -135,3 +140,8 @@ export const Loading = Template.bind({})
Loading.args = {
isLoading: true,
}

export const Empty = Template.bind({})
Empty.args = {
rows: undefined,
}
23 changes: 18 additions & 5 deletions styleguide/src/Components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface TdWrapperProps {
props?: { [key: string]: unknown }
}

const TdClasses = `p-0 break-words border-t border-card-stroke first:rounded-bl last:rounded-br`
const TdWrapperClasses = `block p-4 no-underline`
const TdWrapper = ({ Wrapper, props = {}, children }: TdWrapperProps) =>
Wrapper ? (
Expand All @@ -38,6 +39,7 @@ const TableComponent = ({
// placeholderLength,
// placeholderSize,
isLoading = false,
emptyText = 'Nenhum registro encontrado',
}: TableProps) => {
const rowsPropsMemoized = React.useMemo(() => rowsProps, [rowsProps])

Expand Down Expand Up @@ -67,9 +69,6 @@ const TableComponent = ({
prepareRow,
} = useTable({ columns, data: rowsPropsMemoized })

// console.log('AAAAA', rows)
// console.log('BBBBB', columnsProps)

return (
<div className="max-w-full overflow-x-auto">
<table
Expand Down Expand Up @@ -109,12 +108,22 @@ const TableComponent = ({
<tbody {...getTableBodyProps()} className="text-sm text-on-base">
{isLoading ? (
<tr>
<td>
<td colSpan={columns.length || 1} className={`${TdClasses}`}>
<TdWrapper>
<LoadingPlaceholder />
</TdWrapper>
</td>
</tr>
) : !rows || !rows.length ? (
<tr>
<td colSpan={columns.length || 1} className={`${TdClasses}`}>
<TdWrapper>
<div className="text-center py-16 text-xl font-semibold text-on-base-2">
{emptyText}
</div>
</TdWrapper>
</td>
</tr>
) : (
rows.map((row) => {
prepareRow(row)
Expand All @@ -129,7 +138,7 @@ const TableComponent = ({
<td
key={key}
{...restCellProps}
className={`p-0 break-words border-t border-card-stroke first:rounded-bl last:rounded-br ${textAligns[textAlign]}`}
className={`${TdClasses} ${textAligns[textAlign]}`}
>
{cell.render('Cell', {
cellWrapperProps: cell.row.original.cellWrapperProps,
Expand Down Expand Up @@ -194,6 +203,10 @@ export interface TableProps {
* @default false
*/
isLoading?: boolean
/** Content to show when table is empty
* @default 'Nenhum registro encontrado'
*/
emptyText?: string | React.ReactNode

// #TODO:
/** Mapped rows data returned on select change. */
Expand Down

0 comments on commit 16a12a0

Please sign in to comment.