Skip to content

Commit 539c1f5

Browse files
authored
feat(Table): add hasAction flag (#11877)
* feat(Table): add hasAction flag * rebase, add unit test
1 parent 7f92acb commit 539c1f5

File tree

5 files changed

+127
-1
lines changed

5 files changed

+127
-1
lines changed

packages/react-table/src/components/Table/Td.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ export interface TdProps extends BaseCellProps, Omit<React.HTMLProps<HTMLTableDa
4545
select?: TdSelectType;
4646
/** Turns the cell into an actions cell. Recommended to use an ActionsColumn component as a child of the Td rather than this prop. */
4747
actions?: TdActionsType;
48+
/** Indicates the cell contains an interactive element and prevents that element's padding from increasing row height. Recommended when other cells in the same row contains text. */
49+
hasAction?: boolean;
4850
/** Turns the cell into an expansion toggle and determines if the corresponding expansion row is open */
4951
expand?: TdExpandType;
5052
/** Turns the cell into a compound expansion toggle */
@@ -86,6 +88,7 @@ const TdBase: React.FunctionComponent<TdProps> = ({
8688
children,
8789
className,
8890
isActionCell = false,
91+
hasAction = false,
8992
component = 'td',
9093
dataLabel,
9194
textCenter = false,
@@ -314,6 +317,7 @@ const TdBase: React.FunctionComponent<TdProps> = ({
314317
styles.tableTd,
315318
className,
316319
isActionCell && styles.tableAction,
320+
hasAction && styles.modifiers.action,
317321
textCenter && styles.modifiers.center,
318322
noPadding && styles.modifiers.noPadding,
319323
isStickyColumn && scrollStyles.tableStickyCell,
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { render, screen } from '@testing-library/react';
2+
import styles from '@patternfly/react-styles/css/components/Table/table';
3+
import { Td } from '../Td';
4+
5+
test('Renders without children', () => {
6+
render(
7+
<table>
8+
<tbody>
9+
<tr>
10+
<Td />
11+
</tr>
12+
</tbody>
13+
</table>
14+
);
15+
16+
expect(screen.getByRole('cell')).toBeInTheDocument();
17+
});
18+
19+
test('Renders with children', () => {
20+
render(
21+
<table>
22+
<tbody>
23+
<tr>
24+
<Td>Cell content</Td>
25+
</tr>
26+
</tbody>
27+
</table>
28+
);
29+
30+
expect(screen.getByRole('cell')).toHaveTextContent('Cell content');
31+
});
32+
33+
test(`Applies ${styles.modifiers.action} when hasAction is true`, () => {
34+
render(
35+
<table>
36+
<tbody>
37+
<tr>
38+
<Td hasAction>Cell with action</Td>
39+
</tr>
40+
</tbody>
41+
</table>
42+
);
43+
44+
expect(screen.getByRole('cell')).toHaveClass('pf-m-action');
45+
});
46+
47+
test(`Does not apply ${styles.modifiers.action} when hasAction is false`, () => {
48+
render(
49+
<table>
50+
<tbody>
51+
<tr>
52+
<Td hasAction={false}>Cell without action</Td>
53+
</tr>
54+
</tbody>
55+
</table>
56+
);
57+
58+
expect(screen.getByRole('cell')).not.toHaveClass('pf-m-action');
59+
});
60+
61+
test('Matches snapshot without children', () => {
62+
const { asFragment } = render(
63+
<table>
64+
<tbody>
65+
<tr>
66+
<Td />
67+
</tr>
68+
</tbody>
69+
</table>
70+
);
71+
72+
expect(asFragment()).toMatchSnapshot();
73+
});
74+
75+
test('Matches snapshot with children', () => {
76+
const { asFragment } = render(
77+
<table>
78+
<tbody>
79+
<tr>
80+
<Td>Cell content</Td>
81+
</tr>
82+
</tbody>
83+
</table>
84+
);
85+
86+
expect(asFragment()).toMatchSnapshot();
87+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Matches snapshot with children 1`] = `
4+
<DocumentFragment>
5+
<table>
6+
<tbody>
7+
<tr>
8+
<td
9+
class="pf-v6-c-table__td"
10+
tabindex="-1"
11+
>
12+
Cell content
13+
</td>
14+
</tr>
15+
</tbody>
16+
</table>
17+
</DocumentFragment>
18+
`;
19+
20+
exports[`Matches snapshot without children 1`] = `
21+
<DocumentFragment>
22+
<table>
23+
<tbody>
24+
<tr>
25+
<td
26+
class="pf-v6-c-table__td"
27+
tabindex="-1"
28+
/>
29+
</tr>
30+
</tbody>
31+
</table>
32+
</DocumentFragment>
33+
`;

packages/react-table/src/components/Table/examples/Table.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ To make a cell an action cell, render an `ActionsColumn` component inside a row'
184184

185185
If actions menus are getting clipped by other items on the page, such as sticky columns or rows, the `ActionsColumn` can be passed a `menuAppendTo` prop to adjust where the actions menu is appended.
186186

187+
When a table row contains mixed content of text and interactive elements, the `hasAction` property may be passed to a `Td` which contains interactive content like the below example's start `Button`. This will align buttons and other elements with other cells' text. Note that `hasAction` should not be used with `Td`s in an `ActionsColumn` because that comes with it's own spacing.
188+
187189
```ts file="TableActions.tsx"
188190

189191
```

packages/react-table/src/components/Table/examples/TableActions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export const TableActions: React.FunctionComponent = () => {
146146
<Td dataLabel={columnNames.prs}>{repo.prs}</Td>
147147
<Td dataLabel={columnNames.workspaces}>{repo.workspaces}</Td>
148148
<Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td>
149-
<Td dataLabel={columnNames.singleAction} modifier="fitContent">
149+
<Td dataLabel={columnNames.singleAction} modifier="fitContent" hasAction>
150150
{singleActionButton}
151151
</Td>
152152
<Td isActionCell>

0 commit comments

Comments
 (0)