1
1
import classNames from 'classnames' ;
2
2
import React from 'react' ;
3
3
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
+ {
60
59
bsPrefix,
61
60
className,
62
61
striped,
@@ -66,24 +65,25 @@ class Table extends React.Component {
66
65
size,
67
66
variant,
68
67
responsive,
69
- ...props
70
- } = this . props ;
71
-
68
+ ...otherProps
69
+ } ,
70
+ ref ,
71
+ ) => {
72
+ const decoratedBsPrefix = useBootstrapPrefix ( bsPrefix , 'table' ) ;
72
73
const classes = classNames (
73
- bsPrefix ,
74
74
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` ,
81
82
) ;
82
83
83
- const table = < table { ...props } className = { classes } /> ;
84
-
84
+ const table = < table { ...otherProps } className = { classes } ref = { ref } /> ;
85
85
if ( responsive ) {
86
- let responsiveClass = `${ bsPrefix } -responsive` ;
86
+ let responsiveClass = `${ decoratedBsPrefix } -responsive` ;
87
87
if ( typeof responsive === 'string' ) {
88
88
responsiveClass = `${ responsiveClass } -${ responsive } ` ;
89
89
}
@@ -92,7 +92,9 @@ class Table extends React.Component {
92
92
}
93
93
94
94
return table ;
95
- }
96
- }
95
+ } ,
96
+ ) ;
97
+
98
+ Table . propTypes = propTypes ;
97
99
98
- export default createBootstrapComponent ( Table , 'table' ) ;
100
+ export default Table ;
0 commit comments