Skip to content

Commit f973ceb

Browse files
committed
chore(Table): use React.forwardRef() (#4239)
* chore(Table): use React.forwardRef() * fix naming
1 parent 6200da0 commit f973ceb

File tree

16 files changed

+48
-28
lines changed

16 files changed

+48
-28
lines changed

src/collections/Table/Table.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import TableRow from './TableRow'
2525
/**
2626
* A table displays a collections of data grouped into rows.
2727
*/
28-
function Table(props) {
28+
const Table = React.forwardRef(function TableInner(props, ref) {
2929
const {
3030
attached,
3131
basic,
@@ -88,7 +88,7 @@ function Table(props) {
8888

8989
if (!childrenUtils.isNil(children)) {
9090
return (
91-
<ElementType {...rest} className={classes}>
91+
<ElementType {...rest} className={classes} ref={ref}>
9292
{children}
9393
</ElementType>
9494
)
@@ -104,7 +104,7 @@ function Table(props) {
104104
)
105105

106106
return (
107-
<ElementType {...rest} className={classes}>
107+
<ElementType {...rest} className={classes} ref={ref}>
108108
{headerElement}
109109
<TableBody>
110110
{renderBodyRow &&
@@ -113,8 +113,9 @@ function Table(props) {
113113
{footerRow && <TableFooter>{TableRow.create(footerRow)}</TableFooter>}
114114
</ElementType>
115115
)
116-
}
116+
})
117117

118+
Table.displayName = 'Table'
118119
Table.defaultProps = {
119120
as: 'table',
120121
}

src/collections/Table/TableBody.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ import React from 'react'
44

55
import { getElementType, getUnhandledProps } from '../../lib'
66

7-
function TableBody(props) {
7+
const TableBody = React.forwardRef(function TableBodyInner(props, ref) {
88
const { children, className } = props
99
const classes = cx(className)
1010
const rest = getUnhandledProps(TableBody, props)
1111
const ElementType = getElementType(TableBody, props)
1212

1313
return (
14-
<ElementType {...rest} className={classes}>
14+
<ElementType {...rest} className={classes} ref={ref}>
1515
{children}
1616
</ElementType>
1717
)
18-
}
18+
})
1919

20+
TableBody.displayName = 'TableBody'
2021
TableBody.defaultProps = {
2122
as: 'tbody',
2223
}

src/collections/Table/TableCell.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Icon from '../../elements/Icon'
2020
/**
2121
* A table row can have cells.
2222
*/
23-
function TableCell(props) {
23+
const TableCell = React.forwardRef(function TableCellInner(props, ref) {
2424
const {
2525
active,
2626
children,
@@ -60,24 +60,25 @@ function TableCell(props) {
6060

6161
if (!childrenUtils.isNil(children)) {
6262
return (
63-
<ElementType {...rest} className={classes}>
63+
<ElementType {...rest} className={classes} ref={ref}>
6464
{children}
6565
</ElementType>
6666
)
6767
}
6868

6969
return (
70-
<ElementType {...rest} className={classes}>
70+
<ElementType {...rest} className={classes} ref={ref}>
7171
{Icon.create(icon)}
7272
{content}
7373
</ElementType>
7474
)
75-
}
75+
})
7676

7777
TableCell.defaultProps = {
7878
as: 'td',
7979
}
8080

81+
TableCell.displayName = 'TableCell'
8182
TableCell.propTypes = {
8283
/** An element type to render as (string or function). */
8384
as: PropTypes.elementType,

src/collections/Table/TableFooter.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import TableHeader from './TableHeader'
77
/**
88
* A table can have a footer.
99
*/
10-
function TableFooter(props) {
10+
const TableFooter = React.forwardRef(function TableFooterInner(props, ref) {
1111
const { as } = props
1212
const rest = getUnhandledProps(TableFooter, props)
1313

14-
return <TableHeader {...rest} as={as} />
15-
}
14+
return <TableHeader {...rest} as={as} ref={ref} />
15+
})
1616

17+
TableFooter.displayName = 'TableFooter'
1718
TableFooter.propTypes = {
1819
/** An element type to render as (string or function). */
1920
as: PropTypes.elementType,

src/collections/Table/TableHeader.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,24 @@ import {
1313
/**
1414
* A table can have a header.
1515
*/
16-
function TableHeader(props) {
16+
const TableHeader = React.forwardRef(function TableHeaderInner(props, ref) {
1717
const { children, className, content, fullWidth } = props
1818
const classes = cx(useKeyOnly(fullWidth, 'full-width'), className)
1919
const rest = getUnhandledProps(TableHeader, props)
2020
const ElementType = getElementType(TableHeader, props)
2121

2222
return (
23-
<ElementType {...rest} className={classes}>
23+
<ElementType {...rest} className={classes} ref={ref}>
2424
{childrenUtils.isNil(children) ? content : children}
2525
</ElementType>
2626
)
27-
}
27+
})
2828

2929
TableHeader.defaultProps = {
3030
as: 'thead',
3131
}
3232

33+
TableHeader.displayName = 'TableHeader'
3334
TableHeader.propTypes = {
3435
/** An element type to render as (string or function). */
3536
as: PropTypes.elementType,

src/collections/Table/TableHeaderCell.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import TableCell from './TableCell'
88
/**
99
* A table can have a header cell.
1010
*/
11-
function TableHeaderCell(props) {
11+
const TableHeaderCell = React.forwardRef(function TableHeaderCellInner(props, ref) {
1212
const { as, className, sorted } = props
1313
const classes = cx(useValueAndKey(sorted, 'sorted'), className)
1414
const rest = getUnhandledProps(TableHeaderCell, props)
1515

16-
return <TableCell {...rest} as={as} className={classes} />
17-
}
16+
return <TableCell {...rest} as={as} className={classes} ref={ref} />
17+
})
1818

19+
TableHeaderCell.displayName = 'TableHeaderCell'
1920
TableHeaderCell.propTypes = {
2021
/** An element type to render as (string or function). */
2122
as: PropTypes.elementType,

src/collections/Table/TableRow.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import TableCell from './TableCell'
1919
/**
2020
* A table can have rows.
2121
*/
22-
function TableRow(props) {
22+
const TableRow = React.forwardRef(function TableRowInner(props, ref) {
2323
const {
2424
active,
2525
cellAs,
@@ -51,24 +51,25 @@ function TableRow(props) {
5151

5252
if (!childrenUtils.isNil(children)) {
5353
return (
54-
<ElementType {...rest} className={classes}>
54+
<ElementType {...rest} className={classes} ref={ref}>
5555
{children}
5656
</ElementType>
5757
)
5858
}
5959

6060
return (
61-
<ElementType {...rest} className={classes}>
61+
<ElementType {...rest} className={classes} ref={ref}>
6262
{_.map(cells, (cell) => TableCell.create(cell, { defaultProps: { as: cellAs } }))}
6363
</ElementType>
6464
)
65-
}
65+
})
6666

6767
TableRow.defaultProps = {
6868
as: 'tr',
6969
cellAs: 'td',
7070
}
7171

72+
TableRow.displayName = 'TableRow'
7273
TableRow.propTypes = {
7374
/** An element type to render as (string or function). */
7475
as: PropTypes.elementType,

test/specs/collections/Table/Table-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import * as _ from 'lodash'
1313

1414
describe('Table', () => {
1515
common.isConformant(Table)
16+
common.forwardsRef(Table, { tagName: 'table' })
17+
common.forwardsRef(Table, { requiredProps: { children: <tbody /> }, tagName: 'table' })
1618
common.hasSubcomponents(Table, [
1719
TableBody,
1820
TableCell,

test/specs/collections/Table/TableBody-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import TableBody from 'src/collections/Table/TableBody'
55

66
describe('TableBody', () => {
77
common.isConformant(TableBody)
8+
common.forwardsRef(TableBody, { tagName: 'tbody' })
89
common.rendersChildren(TableBody, {
910
rendersContent: false,
1011
})

test/specs/collections/Table/TableCell-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { SUI } from 'src/lib'
66

77
describe('TableCell', () => {
88
common.isConformant(TableCell)
9+
common.forwardsRef(TableCell, { tagName: 'td' })
10+
common.forwardsRef(TableCell, { requiredProps: { children: <span /> }, tagName: 'td' })
911
common.rendersChildren(TableCell)
1012

1113
common.implementsCreateMethod(TableCell)

test/specs/collections/Table/TableFooter-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import TableFooter from 'src/collections/Table/TableFooter'
55

66
describe('TableFooter', () => {
77
common.isConformant(TableFooter)
8+
common.forwardsRef(TableFooter, { tagName: 'tfoot' })
89

910
it('renders as a tfoot by default', () => {
1011
shallow(<TableFooter />).should.have.tagName('tfoot')

test/specs/collections/Table/TableHeader-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import TableHeader from 'src/collections/Table/TableHeader'
55

66
describe('TableHeader', () => {
77
common.isConformant(TableHeader)
8+
common.forwardsRef(TableHeader, { tagName: 'thead' })
89
common.rendersChildren(TableHeader)
910

1011
common.propKeyOnlyToClassName(TableHeader, 'fullWidth', {

test/specs/collections/Table/TableHeaderCell-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import TableHeaderCell from 'src/collections/Table/TableHeaderCell'
55

66
describe('TableHeaderCell', () => {
77
common.isConformant(TableHeaderCell)
8+
common.forwardsRef(TableHeaderCell, { tagName: 'th' })
89
common.propKeyAndValueToClassName(TableHeaderCell, 'sorted', ['ascending', 'descending'])
910

1011
it('renders as a th by default', () => {

test/specs/collections/Table/TableRow-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import TableRow from 'src/collections/Table/TableRow'
55

66
describe('TableRow', () => {
77
common.isConformant(TableRow)
8+
common.forwardsRef(TableRow, { tagName: 'tr' })
9+
common.forwardsRef(TableRow, { requiredProps: { children: <span /> }, tagName: 'tr' })
810
common.rendersChildren(TableRow, {
911
rendersContent: false,
1012
})

test/specs/commonTests/forwardsRef.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import React from 'react'
2-
import { sandbox } from 'test/utils'
2+
import { consoleUtil, sandbox } from 'test/utils'
33

44
/**
55
* Assert a Component correctly implements a shorthand create method.
66
* @param {React.Component} Component The component to test
7+
* @param {{ requiredProps?: Object, tagName?: string }} options Options for a test
78
*/
8-
export default function implementsCreateMethod(Component, options = {}) {
9+
export default function forwardsRef(Component, options = {}) {
910
describe('forwardsRef', () => {
1011
const { requiredProps = {}, tagName = 'div' } = options
1112

1213
it(`forwards ref to "${tagName}"`, () => {
1314
const ref = sandbox.spy()
1415

16+
// mount() can produce "validateNesting" error from React when elements like "td" are mounted
17+
consoleUtil.disableOnce()
1518
mount(<Component {...requiredProps} ref={ref} />)
1619

1720
ref.should.have.been.calledOnce()

test/specs/elements/Header/Header-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import * as common from 'test/specs/commonTests'
1010

1111
describe('Header', () => {
1212
common.hasUIClassName(Header)
13-
common.forwardsRef(Header, { children: <span /> })
14-
common.forwardsRef(Header, { icon: 'book' })
13+
common.forwardsRef(Header, { requiredProps: { children: <span /> } })
14+
common.forwardsRef(Header, { requiredProps: { icon: 'book' } })
1515
common.hasSubcomponents(Header, [HeaderContent, HeaderSubheader])
1616
common.rendersChildren(Header)
1717

0 commit comments

Comments
 (0)