Skip to content

Commit ae7b75f

Browse files
Merge pull request #867 from rebeccaalpert/table
fix(TableMessage): Handle inline code
2 parents ba585b9 + 295f2da commit ae7b75f

4 files changed

Lines changed: 46 additions & 2 deletions

File tree

packages/module/src/Message/Message.test.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ const TABLE = `
118118
119119
`;
120120

121+
const TABLE_WITH_INLINE_CODE = `
122+
| Status | Finding |
123+
|---|---|
124+
| \`1/1 Running\`, 0 restarts | Healthy — no crashes |
125+
| \`1/1 Ready\` | Desired replicas met |
126+
`;
127+
121128
const ONE_COLUMN_TABLE = `
122129
123130
| Column 1 |
@@ -1041,6 +1048,24 @@ describe('Message', () => {
10411048
render(<Message avatar="./img" role="user" name="User" content={TABLE} tableProps={{ 'aria-label': 'Test' }} />);
10421049
expect(screen.getByRole('grid', { name: /Test/i })).toBeTruthy();
10431050
});
1051+
it('should wrap table cell content so inline code does not break grid layout', () => {
1052+
render(
1053+
<Message
1054+
avatar="./img"
1055+
role="user"
1056+
name="User"
1057+
content={TABLE_WITH_INLINE_CODE}
1058+
tableProps={{ 'aria-label': 'Inline code table' }}
1059+
/>
1060+
);
1061+
const statusCell = screen.getByRole('cell', { name: /1\/1 Running/i });
1062+
expect(statusCell.querySelector(':scope > .pf-chatbot__message-table-cell-content')).toBeTruthy();
1063+
expect(statusCell.querySelector(':scope > code')).toBeFalsy();
1064+
expect(statusCell.querySelector('.pf-chatbot__message-inline-code')).toBeTruthy();
1065+
expect(
1066+
screen.getByRole('columnheader', { name: /Status/i }).querySelector('.pf-chatbot__message-table-cell-content')
1067+
).toBeTruthy();
1068+
});
10441069
it('should render footnote correctly', () => {
10451070
render(<Message avatar="./img" role="user" name="User" content={FOOTNOTE} />);
10461071
expect(screen.getByText(/This is some text with a footnote/i)).toBeTruthy();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Wraps table cell content in a single element so phrasing content (e.g. inline
3+
* code mixed with text) is not a direct child of td/th.
4+
*/
5+
const TableCellContent = ({ children }: { children?: React.ReactNode }) => (
6+
<span className="pf-chatbot__message-table-cell-content">{children}</span>
7+
);
8+
9+
export default TableCellContent;

packages/module/src/Message/TableMessage/TdMessage.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
import { ExtraProps } from 'react-markdown';
66
import { Td, TdProps } from '@patternfly/react-table';
7+
import TableCellContent from './TableCellContent';
78

8-
const TdMessage = ({ children, ...props }: Omit<TdProps, 'ref'> & ExtraProps) => <Td {...props}>{children}</Td>;
9+
const TdMessage = ({ children, ...props }: Omit<TdProps, 'ref'> & ExtraProps) => (
10+
<Td {...props}>
11+
<TableCellContent>{children}</TableCellContent>
12+
</Td>
13+
);
914

1015
export default TdMessage;

packages/module/src/Message/TableMessage/ThMessage.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
import { ExtraProps } from 'react-markdown';
66
import { Th, ThProps } from '@patternfly/react-table';
7+
import TableCellContent from './TableCellContent';
78

8-
const ThMessage = ({ children, ...props }: Omit<ThProps, 'ref'> & ExtraProps) => <Th {...props}>{children}</Th>;
9+
const ThMessage = ({ children, ...props }: Omit<ThProps, 'ref'> & ExtraProps) => (
10+
<Th {...props}>
11+
<TableCellContent>{children}</TableCellContent>
12+
</Th>
13+
);
914

1015
export default ThMessage;

0 commit comments

Comments
 (0)