Skip to content

Commit a819115

Browse files
committed
add data attributes
1 parent d508efa commit a819115

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,8 @@ export const Cell = /*#__PURE__*/ createLeafComponent(TableCellNode, (props: Cel
13731373
let {isFocused, isFocusVisible, focusProps} = useFocusRing();
13741374
let {hoverProps, isHovered} = useHover({});
13751375
let isSelected = cell.parentKey != null ? state.selectionManager.isSelected(cell.parentKey) : false;
1376+
// colIndex is null, when there is so span, falling back to using the index
1377+
let colIndex = cell.colIndex || cell.index;
13761378

13771379
let renderProps = useRenderProps({
13781380
...props,
@@ -1385,8 +1387,7 @@ export const Cell = /*#__PURE__*/ createLeafComponent(TableCellNode, (props: Cel
13851387
isHovered,
13861388
isSelected,
13871389
id: cell.key,
1388-
// colIndex is null, when there is so span, falling back to using the index
1389-
colIndex: cell.colIndex || cell.index
1390+
colIndex
13901391
}
13911392
});
13921393

@@ -1400,7 +1401,8 @@ export const Cell = /*#__PURE__*/ createLeafComponent(TableCellNode, (props: Cel
14001401
data-focused={isFocused || undefined}
14011402
data-focus-visible={isFocusVisible || undefined}
14021403
data-pressed={isPressed || undefined}
1403-
data-selected={isSelected || undefined}>
1404+
data-selected={isSelected || undefined}
1405+
data-col-index={colIndex}>
14041406
<CollectionRendererContext.Provider value={DefaultCollectionRenderer}>
14051407
{renderProps.children}
14061408
</CollectionRendererContext.Provider>

packages/react-aria-components/test/Table.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,31 @@ describe('Table', () => {
889889
expect(cells[2]).toHaveTextContent('cell index: 3');
890890
});
891891

892+
it('should support data-col-index attribute', () => {
893+
let {getAllByRole} = render(
894+
<Table aria-label="Search results">
895+
<TableHeader>
896+
<Column isRowHeader>Name</Column>
897+
<Column isRowHeader>Type</Column>
898+
<Column isRowHeader>Price</Column>
899+
<Column isRowHeader>Total</Column>
900+
</TableHeader>
901+
<TableBody>
902+
<Row>
903+
<Cell colSpan={2}>Foo</Cell>
904+
<Cell>Bar</Cell>
905+
<Cell>Baz</Cell>
906+
</Row>
907+
</TableBody>
908+
</Table>
909+
);
910+
911+
let cells = getAllByRole('rowheader');
912+
expect(cells[0]).toHaveAttribute('data-col-index', '0');
913+
expect(cells[1]).toHaveAttribute('data-col-index', '2');
914+
expect(cells[2]).toHaveAttribute('data-col-index', '3');
915+
});
916+
892917
it('should support columnHeader typeahead', async () => {
893918
let {getAllByRole} = render(
894919
<Table aria-label="Files">

0 commit comments

Comments
 (0)