Skip to content

Commit

Permalink
feat(Table): add "fixed" prop for fixed table layouts (#1708)
Browse files Browse the repository at this point in the history
Co-authored-by: Thayanan Tharmapalan <[email protected]>

Co-authored-by: Thayanan Tharmapalan <[email protected]>
  • Loading branch information
tujoworker and thaytharma authored Nov 8, 2022
1 parent 7429455 commit 241ee0f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export const TableSticky = () => (
<ComponentBox hideCode data-visual-test="table-sticky">
{
/* jsx */ `
<Table sticky={true} sticky_offset="4rem" className="dnb-table--fixed">
<Table sticky={true} stickyOffset="4rem" fixed>
<caption className="dnb-sr-only">A Table Caption</caption>
<thead>
<Tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The following table has a default style. But in future, there will be several ex

## Fixed layout

You may consider using `table-layout: fixed;`. You can use the modifier class in doing so: `.dnb-table--fixed`
You may consider using `table-layout: fixed;`. You can use the modifier property `fixed` for doing so.

## Accessibility

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ showTabs: true
| `stickyOffset` | _(optional)_ defines the offset (top) in `rem` from where the header should start to stick. You may define your app header height here, if you have a sticky header on your page. Defaults to `0`. |
| ~~`variant`~~ (not implemented yet) | _(coming)_ defines the style variant of the table. Options: `basis` . Defaults to `generic`. |
| ~~`size`~~ (not implemented yet) | _(coming)_ spacing size inside the table header and data. Options: `small` \| `medium` \| `large` \. Defaults to `large`. |
| `fixed` | _(optional)_ if set to `true`, the table will behave with a fixed table layout, using: `table-layout: fixed;`. Defaults to `false`. |
| `skeleton` | _(optional)_ if set to `true`, an overlaying skeleton with animation will be shown. |
| [Space](/uilib/components/space/properties) | _(optional)_ spacing properties like `top` or `bottom` are supported. |

Expand Down
8 changes: 8 additions & 0 deletions packages/dnb-eufemia/src/components/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export interface TableProps extends StickyTableHeaderProps {
* Default: generic.
*/
variant?: TableVariants

/**
* Defines if the table should behave with a fixed table layout, using: "table-layout: fixed;"
* Default: null.
*/
fixed?: boolean
}

export const defaultProps = {
Expand Down Expand Up @@ -84,6 +90,7 @@ const Table = (
variant,
sticky,
stickyOffset, // eslint-disable-line
fixed,
...props
} = allProps

Expand Down Expand Up @@ -112,6 +119,7 @@ const Table = (
variant && `dnb-table__variant--${variant}`,
size && `dnb-table__size--${size}`,
sticky && `dnb-table--sticky`,
fixed && `dnb-table--fixed`,
spacingClasses,
skeletonClasses,
className
Expand Down
31 changes: 28 additions & 3 deletions packages/dnb-eufemia/src/components/table/__tests__/Table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ describe('Table', () => {
])
})

it('should include custom className and HTML attributes', () => {
it('should include custom className', () => {
render(
<Table className="dnb-table--fixed" aria-label="custom-label">
<Table className="custom-class" aria-label="custom-label">
<BasicTable />
</Table>
)
Expand All @@ -84,15 +84,40 @@ describe('Table', () => {
'dnb-table',
'dnb-table__variant--generic',
'dnb-table__size--large',
'dnb-table--fixed',
'custom-class',
])
)
})

it('should include custom HTML attributes', () => {
render(
<Table aria-label="custom-label">
<BasicTable />
</Table>
)

expect(screen.queryByRole('table').getAttribute('aria-label')).toEqual(
'custom-label'
)
})

it('should set the fixed class', () => {
render(
<Table fixed>
<BasicTable />
</Table>
)

expect(Array.from(screen.queryByRole('table').classList)).toEqual(
expect.arrayContaining([
'dnb-table',
'dnb-table__variant--generic',
'dnb-table__size--large',
'dnb-table--fixed',
])
)
})

it('should support spacing props', () => {
render(
<Table top="2rem">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const StickyBasicTable = () => {
top="5rem"
// skeleton // toggle
sticky
// className="dnb-table--fixed"
// fixed
stickyOffset="4rem"
>
<caption className="dnb-sr-only">A Table Caption</caption>
Expand Down

0 comments on commit 241ee0f

Please sign in to comment.