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

feat: Update Table to 2.10.0 implementation #1110

Merged
merged 31 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
54ad493
Add logic and Storybook examples for scrollable, compact, and striped…
SirenaBorracha Apr 9, 2021
ead0ccf
Add logic, Storybook examples, and tests for stacked styles; remove c…
SirenaBorracha Apr 9, 2021
9914f93
Remove superfluous boolean variable
SirenaBorracha Apr 9, 2021
e6c9cc9
Add div to stacked styles in Storybook to apply width-mobile classNam…
SirenaBorracha Apr 9, 2021
d8c56a5
Remove is false default assignments from passed in args to Table
SirenaBorracha Apr 11, 2021
292e26b
Combine stackedStyles storybook examples by adding Storybook controls…
SirenaBorracha Apr 11, 2021
a05bcc6
Add test for borderless stacked table, remove queryByTestId in favor …
SirenaBorracha Apr 11, 2021
696bbf6
Merge branch 'main' into ak-update-table-to-2.10-implementation-991
SirenaBorracha Apr 12, 2021
b18a879
Merge branch 'main' into ak-update-table-to-2.10-implementation-991
SirenaBorracha Apr 12, 2021
47fd854
Factor out repeated table code
SirenaBorracha Apr 12, 2021
77d7cd5
Respond to PR feedback, update data-testid to more accurately reflect…
SirenaBorracha Apr 12, 2021
c490bf3
Update scrollable storybook example to have a border
SirenaBorracha Apr 12, 2021
6001abe
Merge branch 'main' into ak-update-table-to-2.10-implementation-991
SirenaBorracha Apr 12, 2021
9eef34b
Fix scrollable Storybook example data formatting
SirenaBorracha Apr 12, 2021
17235c4
Continue to fix scrollable Storybook example data formatting
SirenaBorracha Apr 12, 2021
4687b9a
Add footnote p tag in scrollable storybook example, pass argTypes int…
SirenaBorracha Apr 13, 2021
0ecc919
Add console warning for compact and stackStyle combination, add argTy…
SirenaBorracha Apr 13, 2021
da6ce9d
Use Storybook viewport addon to default to mobile to show stacked sty…
SirenaBorracha Apr 15, 2021
af2e7a8
Remove incorrect className from scrollableTestContent subheadings
SirenaBorracha Apr 15, 2021
8da9840
Merge branch 'main' into ak-update-table-to-2.10-implementation-991
SirenaBorracha Apr 19, 2021
86c63af
Merge branch 'main' into ak-update-table-to-2.10-implementation-991
SirenaBorracha Apr 20, 2021
5d2981f
Merge branch 'main' into ak-update-table-to-2.10-implementation-991
SirenaBorracha Apr 23, 2021
4b54e81
Remove whitespace from package.json
SirenaBorracha Apr 23, 2021
3b3a6f0
Resolve conflicts in yarn.lock
SirenaBorracha Apr 26, 2021
f7906ff
Merge branch 'main' into ak-update-table-to-2.10-implementation-991
SirenaBorracha Apr 26, 2021
9695740
Resolve conflicts in yarn.lock
SirenaBorracha Apr 26, 2021
ed99123
Regenerate yarn.lock
SirenaBorracha Apr 26, 2021
df5009e
Merge branch 'main' into ak-update-table-to-2.10-implementation-991
SirenaBorracha Apr 26, 2021
2406586
Deleting yarn.lock
SirenaBorracha Apr 27, 2021
529008f
Regenerate yarn.lock
SirenaBorracha Apr 27, 2021
236652e
Merge branch 'main' into ak-update-table-to-2.10-implementation-991
brandonlenz Apr 27, 2021
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
232 changes: 223 additions & 9 deletions src/components/Table/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ Source: https://designsystem.digital.gov/components/table/
},
},
},
argTypes: {
bordered: {
control: {
type: 'boolean',
},
},
striped: {
control: {
type: 'boolean',
},
},
stackedStyle: {
control: {
type: 'select',
options: ['default', 'headers'],
},
defaultValue: 'default',
},
},
}

const testContent = (
Expand Down Expand Up @@ -63,8 +82,138 @@ const testContent = (
</>
)

const scrollableTestContent = (
<>
<thead>
<tr>
<th rowSpan={2}>
Federal Budget <br /> Baseline Projections
</th>
<th colSpan={2} scope="colgroup" className="text-center">
2017
</th>
<th colSpan={2} scope="colgroup" className="text-center">
2018
</th>
<th colSpan={2} scope="colgroup" className="text-center">
2019
</th>
<th colSpan={2} scope="colgroup" className="text-center">
2020
</th>
<th colSpan={2} scope="colgroup" className="text-center">
2021
</th>
<th colSpan={2} scope="colgroup" className="text-center">
Hist. Avg.
</th>
</tr>
<tr>
<th scope="col" className="text-right">
%GDP
</th>
<th scope="col" className="text-right">
USD*
</th>
<th scope="col" className="text-right">
%GDP
</th>
<th scope="col" className="text-right">
USD*
</th>
<th scope="col" className="text-right">
%GDP
</th>
<th scope="col" className="text-right">
USD*
</th>
<th scope="col" className="text-right">
%GDP
</th>
<th scope="col" className="text-right">
USD*
</th>
<th scope="col" className="text-right">
%GDP
</th>
<th scope="col" className="text-right">
USD*
</th>
<th scope="col" className="text-right">
%GDP
</th>
<th scope="col" className="text-right">
USD*
</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Revenue</th>
<td className="font-mono-sm text-tabular text-right">17.2%</td>
<td className="font-mono-sm text-tabular text-right">3,316</td>
<td className="font-mono-sm text-tabular text-right">16.4%</td>
<td className="font-mono-sm text-tabular text-right">3,338</td>
<td className="font-mono-sm text-tabular text-right">16.3%</td>
<td className="font-mono-sm text-tabular text-right">3,490</td>
<td className="font-mono-sm text-tabular text-right">16.7%</td>
<td className="font-mono-sm text-tabular text-right">3,678</td>
<td className="font-mono-sm text-tabular text-right">16.7%</td>
<td className="font-mono-sm text-tabular text-right">3,827</td>
<td className="font-mono-sm text-tabular text-right">17.4%</td>
<td className="font-mono-sm text-tabular text-right">3,381</td>
</tr>
<tr>
<th scope="row">Outlays</th>
<td className="font-mono-sm text-tabular text-right">20.6%</td>
<td className="font-mono-sm text-tabular text-right">3,982</td>
<td className="font-mono-sm text-tabular text-right">20.2%</td>
<td className="font-mono-sm text-tabular text-right">4,142</td>
<td className="font-mono-sm text-tabular text-right">21.0%</td>
<td className="font-mono-sm text-tabular text-right">4,470</td>
<td className="font-mono-sm text-tabular text-right">21.3%</td>
<td className="font-mono-sm text-tabular text-right">4,685</td>
<td className="font-mono-sm text-tabular text-right">21.6%</td>
<td className="font-mono-sm text-tabular text-right">4,949</td>
<td className="font-mono-sm text-tabular text-right">20.3%</td>
<td className="font-mono-sm text-tabular text-right">4,198</td>
</tr>
<tr>
<th scope="row">Budget Deficit</th>
<td className="font-mono-sm text-tabular text-right">-3.5%</td>
<td className="font-mono-sm text-tabular text-right">-665</td>
<td className="font-mono-sm text-tabular text-right">-3.8%</td>
<td className="font-mono-sm text-tabular text-right">-804</td>
<td className="font-mono-sm text-tabular text-right">-4.6%</td>
<td className="font-mono-sm text-tabular text-right">-981</td>
<td className="font-mono-sm text-tabular text-right">-4.6%</td>
<td className="font-mono-sm text-tabular text-right">-1,008</td>
<td className="font-mono-sm text-tabular text-right">-4.9%</td>
<td className="font-mono-sm text-tabular text-right">-1,123</td>
<td className="font-mono-sm text-tabular text-right">-2.9%</td>
<td className="font-mono-sm text-tabular text-right">-483</td>
</tr>
<tr>
<th scope="row">Debt Held by Public</th>
<td className="font-mono-sm text-tabular text-right">76.0%</td>
<td className="font-mono-sm text-tabular text-right">14,665</td>
<td className="font-mono-sm text-tabular text-right">77.4%</td>
<td className="font-mono-sm text-tabular text-right">15,688</td>
<td className="font-mono-sm text-tabular text-right">79.2%</td>
<td className="font-mono-sm text-tabular text-right">16,762</td>
<td className="font-mono-sm text-tabular text-right">80.9%</td>
<td className="font-mono-sm text-tabular text-right">17,872</td>
<td className="font-mono-sm text-tabular text-right">83.1%</td>
<td className="font-mono-sm text-tabular text-right">18,998</td>
<td className="font-mono-sm text-tabular text-right">41.7%</td>
<td className="font-mono-sm text-tabular text-right">8,065</td>
</tr>
</tbody>
</>
)

export const bordered = (): React.ReactElement => (
<Table bordered={true}>{testContent}</Table>
<Table bordered>{testContent}</Table>
)

export const borderless = (): React.ReactElement => (
Expand All @@ -84,9 +233,7 @@ export const borderlessWithCaption = (): React.ReactElement => (
)

export const contentWidth = (): React.ReactElement => (
<Table
bordered={true}
caption="This table demonstrates the default layout (width)">
<Table bordered caption="This table demonstrates the default layout (width)">
<thead>
<tr>
<th scope="col">Document title</th>
Expand Down Expand Up @@ -116,9 +263,9 @@ export const contentWidth = (): React.ReactElement => (

export const fullWidth = (): React.ReactElement => (
<Table
bordered={true}
bordered
caption="This table uses the fullWidth prop to increase to 100% width"
fullWidth={true}>
fullWidth>
<thead>
<tr>
<th scope="col">Document title</th>
Expand Down Expand Up @@ -148,10 +295,10 @@ export const fullWidth = (): React.ReactElement => (

export const fixed = (): React.ReactElement => (
<Table
bordered={true}
bordered
caption="This table uses the fixed prop to force equal width columns"
fullWidth={true}
fixed={true}>
fullWidth
fixed>
<thead>
<tr>
<th scope="col">Document title</th>
Expand All @@ -178,3 +325,70 @@ export const fixed = (): React.ReactElement => (
</tbody>
</Table>
)

export const scrollable = (): React.ReactElement => (
<>
<Table scrollable bordered striped caption="This is a scrollable table">
{scrollableTestContent}
</Table>
<p>* in billions of dollars. Data for illustration purposes only.</p>
</>
)

export const striped = (argTypes): React.ReactElement => (
<Table striped bordered={argTypes.bordered} caption="This is a striped table">
{testContent}
</Table>
)

export const compact = (argTypes): React.ReactElement => (
<Table
compact
bordered={argTypes.bordered}
striped={argTypes.striped}
caption="This is a compact table">
{testContent}
</Table>
)

export const stackedStylesDemo = (argTypes): React.ReactElement => (
<Table
bordered={argTypes.bordered}
stackedStyle={argTypes.stackedStyle}
caption="This is a stacked table (when on a mobile-width screen)">
<thead>
<tr>
<th scope="col">Document title</th>
<th scope="col">Description</th>
<th scope="col">Year</th>
</tr>
</thead>
<tbody>
<tr>
<th data-label="Document title" scope="row">
Declaration of Independence
</th>
<td data-label="Description">
Statement adopted by the Continental Congress declaring independence
from the British Empire.
</td>
<td data-label="Year">1776</td>
</tr>
<tr>
<th data-label="Document title" scope="row">
Bill of Rights
</th>
<td data-label="Description">
The first ten amendments of the U.S. Constitution guaranteeing rights
and freedoms.
</td>
<td data-label="Year">1791</td>
</tr>
</tbody>
</Table>
)
stackedStylesDemo.parameters = {
viewport: {
defaultViewport: 'iphone6',
},
}
brandonlenz marked this conversation as resolved.
Show resolved Hide resolved
47 changes: 45 additions & 2 deletions src/components/Table/Table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,50 @@ const testContent = (

describe('Table component', () => {
it('renders without errors', () => {
const { queryByTestId } = render(<Table>{testContent}</Table>)
expect(queryByTestId('table')).toBeInTheDocument()
const { getByRole } = render(<Table>{testContent}</Table>)

expect(getByRole('table')).toBeInTheDocument()
})

it('renders striped table', () => {
const { getByRole } = render(<Table striped>{testContent}</Table>)

expect(getByRole('table')).toHaveClass('usa-table usa-table--striped')
})

it('renders compact table', () => {
const { getByRole } = render(<Table compact>{testContent}</Table>)

expect(getByRole('table')).toHaveClass('usa-table usa-table--compact')
})

it('renders scrollable table', () => {
const { queryByTestId } = render(<Table scrollable>{testContent}</Table>)

expect(queryByTestId('scrollable-table-container')).toHaveClass(
'usa-table-container--scrollable'
)
})

it('renders borderless stacked table', () => {
const { getByRole } = render(
<Table stackedStyle="default">{testContent}</Table>
)

expect(getByRole('table')).toHaveClass(
'usa-table usa-table--borderless usa-table--stacked'
)
})

it('renders stacked table with border and headers', () => {
const { getByRole } = render(
<Table stackedStyle="headers" bordered>
{testContent}
</Table>
)

expect(getByRole('table')).toHaveClass(
'usa-table usa-table--stacked-header'
)
})
})
34 changes: 33 additions & 1 deletion src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ interface TableProps {
children: React.ReactNode
fullWidth?: boolean
fixed?: boolean
scrollable?: boolean
striped?: boolean
compact?: boolean
stackedStyle?: 'none' | 'default' | 'headers'
}

export const Table = ({
Expand All @@ -17,19 +21,47 @@ export const Table = ({
children,
fullWidth,
fixed,
scrollable,
striped,
compact,
stackedStyle = 'none',
}: TableProps): React.ReactElement => {
const classes = classnames('usa-table', {
'usa-table--borderless': !bordered,
[`${styles.fullwidth}`]: fullWidth,
[`${styles.fixed}`]: fixed,
'usa-table--striped': striped,
'usa-table--compact': compact,
'usa-table--stacked': stackedStyle === 'default',
'usa-table--stacked-header': stackedStyle === 'headers',
})

return (
if (compact && stackedStyle !== 'none') {
console.warn(
'USWDS states that compact styling is not suitable for use with stacked variants. See USWDS Table component, Table variants for more information: https://designsystem.digital.gov/components/table'
)
}

const table = (
<table className={classes} data-testid="table">
{caption && <caption>{caption}</caption>}
{children}
</table>
)

return (
<>
{scrollable ? (
<div
data-testid="scrollable-table-container"
className="usa-table-container--scrollable">
{table}
</div>
) : (
table
)}
</>
)
}

export default Table
Loading