Skip to content

Commit 0e10671

Browse files
DoctorHowsermxschmitt
authored andcommitted
fix(Table): allow passed in refs to be properly forwarded (#4592)
1 parent 9afbac2 commit 0e10671

File tree

1 file changed

+74
-72
lines changed

1 file changed

+74
-72
lines changed

src/Table.js

+74-72
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,61 @@
11
import classNames from 'classnames';
22
import React from 'react';
33
import PropTypes from 'prop-types';
4-
5-
import { createBootstrapComponent } from './ThemeProvider';
6-
7-
class Table extends React.Component {
8-
static propTypes = {
9-
/**
10-
* @default 'table'
11-
*/
12-
bsPrefix: PropTypes.string,
13-
14-
/**
15-
* Adds zebra-striping to any table row within the `<tbody>`.
16-
*/
17-
striped: PropTypes.bool,
18-
19-
/**
20-
* Adds borders on all sides of the table and cells.
21-
*/
22-
bordered: PropTypes.bool,
23-
24-
/**
25-
* Removes all borders on the table and cells, including table header.
26-
*/
27-
borderless: PropTypes.bool,
28-
29-
/**
30-
* Enable a hover state on table rows within a `<tbody>`.
31-
*/
32-
hover: PropTypes.bool,
33-
34-
/**
35-
* Make tables more compact by cutting cell padding in half by setting
36-
* size as `sm`.
37-
*/
38-
size: PropTypes.string,
39-
40-
/**
41-
* Invert the colors of the table — with light text on dark backgrounds
42-
* by setting variant as `dark`.
43-
*/
44-
variant: PropTypes.string,
45-
46-
/**
47-
* Responsive tables allow tables to be scrolled horizontally with ease.
48-
* Across every breakpoint, use `responsive` for horizontally
49-
* scrolling tables. Responsive tables are wrapped automatically in a `div`.
50-
* Use `responsive="sm"`, `responsive="md"`, `responsive="lg"`, or
51-
* `responsive="xl"` as needed to create responsive tables up to
52-
* a particular breakpoint. From that breakpoint and up, the table will
53-
* behave normally and not scroll horizontally.
54-
*/
55-
responsive: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
56-
};
57-
58-
render() {
59-
const {
4+
import { useBootstrapPrefix } from './ThemeProvider';
5+
6+
const propTypes = {
7+
/**
8+
* @default 'table'
9+
*/
10+
bsPrefix: PropTypes.string,
11+
12+
/**
13+
* Adds zebra-striping to any table row within the `<tbody>`.
14+
*/
15+
striped: PropTypes.bool,
16+
17+
/**
18+
* Adds borders on all sides of the table and cells.
19+
*/
20+
bordered: PropTypes.bool,
21+
22+
/**
23+
* Removes all borders on the table and cells, including table header.
24+
*/
25+
borderless: PropTypes.bool,
26+
27+
/**
28+
* Enable a hover state on table rows within a `<tbody>`.
29+
*/
30+
hover: PropTypes.bool,
31+
32+
/**
33+
* Make tables more compact by cutting cell padding in half by setting
34+
* size as `sm`.
35+
*/
36+
size: PropTypes.string,
37+
38+
/**
39+
* Invert the colors of the table — with light text on dark backgrounds
40+
* by setting variant as `dark`.
41+
*/
42+
variant: PropTypes.string,
43+
44+
/**
45+
* Responsive tables allow tables to be scrolled horizontally with ease.
46+
* Across every breakpoint, use `responsive` for horizontally
47+
* scrolling tables. Responsive tables are wrapped automatically in a `div`.
48+
* Use `responsive="sm"`, `responsive="md"`, `responsive="lg"`, or
49+
* `responsive="xl"` as needed to create responsive tables up to
50+
* a particular breakpoint. From that breakpoint and up, the table will
51+
* behave normally and not scroll horizontally.
52+
*/
53+
responsive: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
54+
};
55+
56+
const Table = React.forwardRef(
57+
(
58+
{
6059
bsPrefix,
6160
className,
6261
striped,
@@ -66,24 +65,25 @@ class Table extends React.Component {
6665
size,
6766
variant,
6867
responsive,
69-
...props
70-
} = this.props;
71-
68+
...otherProps
69+
},
70+
ref,
71+
) => {
72+
const decoratedBsPrefix = useBootstrapPrefix(bsPrefix, 'table');
7273
const classes = classNames(
73-
bsPrefix,
7474
className,
75-
variant && `${bsPrefix}-${variant}`,
76-
size && `${bsPrefix}-${size}`,
77-
striped && `${bsPrefix}-striped`,
78-
bordered && `${bsPrefix}-bordered`,
79-
borderless && `${bsPrefix}-borderless`,
80-
hover && `${bsPrefix}-hover`,
75+
decoratedBsPrefix,
76+
variant && `${decoratedBsPrefix}-${variant}`,
77+
size && `${decoratedBsPrefix}-${size}`,
78+
striped && `${decoratedBsPrefix}-striped`,
79+
bordered && `${decoratedBsPrefix}-bordered`,
80+
borderless && `${decoratedBsPrefix}-borderless`,
81+
hover && `${decoratedBsPrefix}-hover`,
8182
);
8283

83-
const table = <table {...props} className={classes} />;
84-
84+
const table = <table {...otherProps} className={classes} ref={ref} />;
8585
if (responsive) {
86-
let responsiveClass = `${bsPrefix}-responsive`;
86+
let responsiveClass = `${decoratedBsPrefix}-responsive`;
8787
if (typeof responsive === 'string') {
8888
responsiveClass = `${responsiveClass}-${responsive}`;
8989
}
@@ -92,7 +92,9 @@ class Table extends React.Component {
9292
}
9393

9494
return table;
95-
}
96-
}
95+
},
96+
);
97+
98+
Table.propTypes = propTypes;
9799

98-
export default createBootstrapComponent(Table, 'table');
100+
export default Table;

0 commit comments

Comments
 (0)