Skip to content

Commit a72b43a

Browse files
authored
fix(table): add pf-m-small to expandable toggle on small tables (#11947)
* fix(table): add pf-m-small to expandable toggle on small tables * fix(table): add pf-m-small to expandable toggle on Th in compact table * fix(table): add sm button variant in place of hardcode classname
1 parent c3b3ab0 commit a72b43a

File tree

7 files changed

+84
-9
lines changed

7 files changed

+84
-9
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@ export interface CollapseColumnProps {
1111
onToggle?: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
1212
isOpen?: boolean;
1313
'aria-label'?: string;
14+
variant?: 'compact';
1415
}
1516

1617
export const CollapseColumn: React.FunctionComponent<CollapseColumnProps> = ({
1718
className = '' as string,
1819
children = null as React.ReactNode,
1920
isOpen,
2021
onToggle,
22+
variant,
2123
...props
2224
}: CollapseColumnProps) => (
2325
<Fragment>
2426
{isOpen !== undefined && (
2527
<Button
2628
className={css(className, isOpen && styles.modifiers.expanded)}
29+
size={variant === 'compact' ? 'sm' : undefined}
2730
{...props}
2831
variant="plain"
2932
aria-label={props['aria-label'] || 'Details'}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ export interface TableProps extends React.HTMLProps<HTMLTableElement>, OUIAProps
7878
interface TableContextProps {
7979
registerSelectableRow?: () => void;
8080
hasAnimations?: boolean;
81+
variant?: TableVariant | 'compact';
8182
}
8283

8384
export const TableContext = createContext<TableContextProps>({
8485
registerSelectableRow: () => {},
85-
hasAnimations: false
86+
hasAnimations: false,
87+
variant: undefined
8688
});
8789

8890
const TableBase: React.FunctionComponent<TableProps> = ({
@@ -204,7 +206,7 @@ const TableBase: React.FunctionComponent<TableProps> = ({
204206
};
205207

206208
return (
207-
<TableContext.Provider value={{ registerSelectableRow, hasAnimations }}>
209+
<TableContext.Provider value={{ registerSelectableRow, hasAnimations, variant }}>
208210
<table
209211
aria-label={ariaLabel}
210212
role={role}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export interface IColumn {
109109
isHeaderSelectDisabled?: boolean;
110110
onFavorite?: OnFavorite;
111111
favoriteButtonProps?: ButtonProps;
112+
variant?: 'compact';
112113
};
113114
}
114115

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ const TdBase: React.FunctionComponent<TdProps> = ({
182182
}
183183
})
184184
: null;
185+
186+
const { hasAnimations, variant } = useContext(TableContext);
187+
185188
const expandableParams =
186189
expand !== null
187190
? collapsible(null, {
@@ -193,13 +196,12 @@ const TdBase: React.FunctionComponent<TdProps> = ({
193196
column: {
194197
extraParams: {
195198
onCollapse: expand?.onToggle,
196-
expandId: expand?.expandId
199+
expandId: expand?.expandId,
200+
variant
197201
}
198202
}
199203
})
200204
: null;
201-
202-
const { hasAnimations } = useContext(TableContext);
203205
const updateAnimationClass = () => {
204206
const ancestorControlRow = (cellRef as React.RefObject<HTMLElement | null>)?.current?.closest(
205207
`.${styles.tableTr}.${styles.tableControlRow}`

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createRef, forwardRef, useEffect, useState } from 'react';
1+
import { createRef, forwardRef, useEffect, useState, useContext } from 'react';
22
import { css } from '@patternfly/react-styles';
33
import styles from '@patternfly/react-styles/css/components/Table/table';
44
import scrollStyles from '@patternfly/react-styles/css/components/Table/table-scrollable';
@@ -8,7 +8,7 @@ import { ThInfoType, ThSelectType, ThExpandType, ThSortType, formatterValueType
88
import { mergeProps } from './base/merge-props';
99
import { IVisibility } from './utils/decorators/classNames';
1010
import { Tooltip, TooltipProps } from '@patternfly/react-core/dist/esm/components/Tooltip';
11-
import { BaseCellProps } from './Table';
11+
import { BaseCellProps, TableContext } from './Table';
1212
import { IFormatterValueType, IColumn } from './TableTypes';
1313
import cssStickyCellMinWidth from '@patternfly/react-tokens/dist/esm/c_table__sticky_cell_MinWidth';
1414
import cssStickyCellInlineStart from '@patternfly/react-tokens/dist/esm/c_table__sticky_cell_InsetInlineStart';
@@ -96,6 +96,8 @@ const ThBase: React.FunctionComponent<ThProps> = ({
9696
'aria-label': ariaLabel,
9797
...props
9898
}: ThProps) => {
99+
const { variant } = useContext(TableContext);
100+
99101
if (!children && !screenReaderText && !ariaLabel) {
100102
// eslint-disable-next-line no-console
101103
console.warn(
@@ -167,7 +169,8 @@ const ThBase: React.FunctionComponent<ThProps> = ({
167169
extraParams: {
168170
onCollapse: collapse?.onToggle,
169171
allRowsExpanded: !collapse.areAllExpanded,
170-
collapseAllAriaLabel: ''
172+
collapseAllAriaLabel: '',
173+
variant
171174
}
172175
}
173176
})

packages/react-table/src/components/Table/__tests__/Table.test.tsx

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { render, screen } from '@testing-library/react';
22
import { Table } from '../Table';
3+
import { Td } from '../Td';
4+
import { Th } from '../Th';
35
import styles from '@patternfly/react-styles/css/components/Table/table';
46

57
test('Renders without children', () => {
@@ -60,3 +62,63 @@ test('Matches snapshot without children', () => {
6062

6163
expect(asFragment()).toMatchSnapshot();
6264
});
65+
66+
test('Renders expandable toggle button with pf-m-small class when variant is compact', () => {
67+
render(
68+
<Table variant="compact" isExpandable aria-label="Test table">
69+
<tbody>
70+
<tr>
71+
<Td
72+
expand={{
73+
rowIndex: 0,
74+
isExpanded: false,
75+
onToggle: () => {}
76+
}}
77+
/>
78+
<Td>Test content</Td>
79+
</tr>
80+
</tbody>
81+
</Table>
82+
);
83+
84+
const toggleButton = screen.getByRole('button', { name: 'Details' });
85+
expect(toggleButton).toHaveClass('pf-m-small');
86+
});
87+
88+
test('Renders expandable toggle button in Th with pf-m-small class when variant is compact', () => {
89+
render(
90+
<Table variant="compact" isExpandable aria-label="Test table">
91+
<thead>
92+
<tr>
93+
<Th
94+
expand={{
95+
areAllExpanded: false,
96+
collapseAllAriaLabel: 'Expand all',
97+
onToggle: () => {}
98+
}}
99+
aria-label="Row expansion"
100+
/>
101+
<Th>Name</Th>
102+
</tr>
103+
</thead>
104+
<tbody>
105+
<tr>
106+
<Td
107+
expand={{
108+
rowIndex: 0,
109+
isExpanded: false,
110+
onToggle: () => {}
111+
}}
112+
/>
113+
<td>Test content</td>
114+
</tr>
115+
</tbody>
116+
</Table>
117+
);
118+
119+
const toggleButtons = screen.getAllByRole('button');
120+
expect(toggleButtons).toHaveLength(2); // One in Th, one in Td
121+
toggleButtons.forEach((button) => {
122+
expect(button).toHaveClass('pf-m-small');
123+
});
124+
});

packages/react-table/src/components/Table/utils/decorators/collapsible.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export const collapsible: IFormatter = (
1414
rowLabeledBy = 'simple-node',
1515
expandId = 'expand-toggle',
1616
allRowsExpanded,
17-
collapseAllAriaLabel
17+
collapseAllAriaLabel,
18+
variant
1819
}
1920
} = column;
2021
const extraData = {
@@ -55,6 +56,7 @@ export const collapsible: IFormatter = (
5556
aria-labelledby={`${rowLabeledBy}${rowId} ${expandId}${rowId}`}
5657
onToggle={onToggle}
5758
id={expandId + rowId}
59+
variant={variant}
5860
{...customProps}
5961
>
6062
{value as React.ReactNode}

0 commit comments

Comments
 (0)