Skip to content

Commit e163b86

Browse files
guidariGururajj77
andauthored
feat: added forwardRef to TableRow (#18415)
Co-authored-by: Gururaj J <89023023+Gururajj77@users.noreply.github.com>
1 parent 0d507e8 commit e163b86

File tree

3 files changed

+62
-36
lines changed

3 files changed

+62
-36
lines changed

packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,7 @@ Map {
21292129
],
21302130
},
21312131
"TableRow": Object {
2132+
"$$typeof": Symbol(react.forward_ref),
21322133
"propTypes": Object {
21332134
"className": Object {
21342135
"type": "string",
@@ -2137,6 +2138,7 @@ Map {
21372138
"type": "bool",
21382139
},
21392140
},
2141+
"render": [Function],
21402142
},
21412143
"TableSelectAll": Object {
21422144
"propTypes": Object {
@@ -8227,6 +8229,7 @@ Map {
82278229
],
82288230
},
82298231
"TableRow" => Object {
8232+
"$$typeof": Symbol(react.forward_ref),
82308233
"propTypes": Object {
82318234
"className": Object {
82328235
"type": "string",
@@ -8235,6 +8238,7 @@ Map {
82358238
"type": "bool",
82368239
},
82378240
},
8241+
"render": [Function],
82388242
},
82398243
"TableSelectAll" => Object {
82408244
"propTypes": Object {

packages/react/src/components/DataTable/TableRow.tsx

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,51 @@ export interface TableRowProps extends ReactAttr<HTMLTableRowElement> {
2222
isSelected?: boolean;
2323
}
2424

25-
const TableRow = (props: TableRowProps) => {
26-
const prefix = usePrefix();
25+
const TableRow = React.forwardRef<HTMLTableCellElement, TableRowProps>(
26+
(props, ref) => {
27+
const prefix = usePrefix();
2728

28-
let rowHasAILabel;
29-
if (props?.children) {
30-
React.Children.toArray(props.children).map((child: any) => {
31-
if (
32-
child.type?.displayName === 'TableSlugRow' ||
33-
child.type?.displayName === 'TableDecoratorRow'
34-
) {
29+
let rowHasAILabel;
30+
if (props?.children) {
31+
React.Children.toArray(props.children).map((child: any) => {
3532
if (
36-
child.props.slug ||
37-
child.props.decorator?.type.displayName === 'AILabel'
33+
child.type?.displayName === 'TableSlugRow' ||
34+
child.type?.displayName === 'TableDecoratorRow'
3835
) {
39-
rowHasAILabel = true;
36+
if (
37+
child.props.slug ||
38+
child.props.decorator?.type.displayName === 'AILabel'
39+
) {
40+
rowHasAILabel = true;
41+
}
4042
}
41-
}
43+
});
44+
}
45+
// Remove unnecessary props if provided to this component, these are
46+
// only useful in `TableExpandRow`
47+
const className = cx(props.className, {
48+
[`${prefix}--data-table--selected`]: props.isSelected,
49+
[`${prefix}--data-table--slug-row ${prefix}--data-table--ai-label-row`]:
50+
rowHasAILabel,
4251
});
43-
}
44-
// Remove unnecessary props if provided to this component, these are
45-
// only useful in `TableExpandRow`
46-
const className = cx(props.className, {
47-
[`${prefix}--data-table--selected`]: props.isSelected,
48-
[`${prefix}--data-table--slug-row ${prefix}--data-table--ai-label-row`]:
49-
rowHasAILabel,
50-
});
5152

52-
const {
53-
ariaLabel,
54-
'aria-label': ariaLabelAlt,
55-
'aria-controls': ariaControls,
56-
onExpand,
57-
isExpanded,
58-
isSelected,
59-
...cleanProps
60-
} = props as any;
53+
const {
54+
ariaLabel,
55+
'aria-label': ariaLabelAlt,
56+
'aria-controls': ariaControls,
57+
onExpand,
58+
isExpanded,
59+
isSelected,
60+
...cleanProps
61+
} = props as any;
6162

62-
if (className) {
63-
cleanProps.className = className;
64-
}
63+
if (className) {
64+
cleanProps.className = className;
65+
}
6566

66-
return <tr {...cleanProps} />;
67-
};
67+
return <tr ref={ref} {...cleanProps} />;
68+
}
69+
);
6870

6971
TableRow.propTypes = {
7072
/**

packages/react/src/components/DataTable/__tests__/TableRow-test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import { render, screen } from '@testing-library/react';
99
import React from 'react';
10-
import { Table, TableBody, TableRow } from '../';
10+
import { Table, TableBody, TableCell, TableRow } from '../';
1111

1212
describe('TableRow', () => {
1313
it('should support a custom className on the outermost element', () => {
@@ -34,4 +34,24 @@ describe('TableRow', () => {
3434
'test'
3535
);
3636
});
37+
38+
it('should forward refs to the rendered Tablerow element', () => {
39+
let tr = null;
40+
const ref = jest.fn((node) => {
41+
tr = node;
42+
});
43+
const { container } = render(
44+
<Table>
45+
<TableBody>
46+
<TableRow ref={ref} data-testid="tr" className="custom-class">
47+
<TableCell />
48+
</TableRow>
49+
</TableBody>
50+
</Table>
51+
);
52+
expect(ref).toHaveBeenCalled();
53+
expect(tr).not.toBeNull();
54+
expect(tr).toEqual(container.querySelector('tr'));
55+
expect(tr).toHaveClass('custom-class');
56+
});
3757
});

0 commit comments

Comments
 (0)