forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRow.tsx
158 lines (135 loc) · 3.68 KB
/
Row.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import classNames from 'classnames';
import PropTypes from 'prop-types';
import * as React from 'react';
import {
useBootstrapPrefix,
useBootstrapBreakpoints,
useBootstrapMinBreakpoint,
} from './ThemeProvider';
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
type RowColWidth =
| number
| '1'
| '2'
| '3'
| '4'
| '5'
| '6'
| '7'
| '8'
| '9'
| '10'
| '11'
| '12'
| 'auto';
type RowColumns = RowColWidth | { cols?: RowColWidth };
export interface RowProps
extends BsPrefixProps,
React.HTMLAttributes<HTMLElement> {
xs?: RowColumns;
sm?: RowColumns;
md?: RowColumns;
lg?: RowColumns;
xl?: RowColumns;
xxl?: RowColumns;
[key: string]: any;
}
const rowColWidth = PropTypes.oneOfType([PropTypes.number, PropTypes.string]);
const rowColumns = PropTypes.oneOfType([
rowColWidth,
PropTypes.shape({
cols: rowColWidth,
}),
]);
const propTypes = {
/**
* @default 'row'
*/
bsPrefix: PropTypes.string,
as: PropTypes.elementType,
/**
* The number of columns that will fit next to each other on extra small devices (<576px).
* Use `auto` to give columns their natural widths.
*
* @type {(number|'auto'|{ cols: number|'auto' })}
*/
xs: rowColumns,
/**
* The number of columns that will fit next to each other on small devices (≥576px).
* Use `auto` to give columns their natural widths.
*
* @type {(number|'auto'|{ cols: number|'auto' })}
*/
sm: rowColumns,
/**
* The number of columns that will fit next to each other on medium devices (≥768px).
* Use `auto` to give columns their natural widths.
*
* @type {(number|'auto'|{ cols: number|'auto' })}
*/
md: rowColumns,
/**
* The number of columns that will fit next to each other on large devices (≥992px).
* Use `auto` to give columns their natural widths.
*
* @type {(number|'auto'|{ cols: number|'auto' })}
*/
lg: rowColumns,
/**
* The number of columns that will fit next to each other on extra large devices (≥1200px).
* Use `auto` to give columns their natural widths.
*
* @type {(number|'auto'|{ cols: number|'auto' })}
*/
xl: rowColumns,
/**
* The number of columns that will fit next to each other on extra extra large devices (≥1400px).
* Use `auto` to give columns their natural widths.
*
* @type {(number|'auto'|{ cols: number|'auto' })}
*/
xxl: rowColumns,
};
const Row: BsPrefixRefForwardingComponent<'div', RowProps> = React.forwardRef<
HTMLDivElement,
RowProps
>(
(
{
bsPrefix,
className,
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
as: Component = 'div',
...props
}: RowProps,
ref,
) => {
const decoratedBsPrefix = useBootstrapPrefix(bsPrefix, 'row');
const breakpoints = useBootstrapBreakpoints();
const minBreakpoint = useBootstrapMinBreakpoint();
const sizePrefix = `${decoratedBsPrefix}-cols`;
const classes: string[] = [];
breakpoints.forEach((brkPoint) => {
const propValue = props[brkPoint];
delete props[brkPoint];
let cols;
if (propValue != null && typeof propValue === 'object') {
({ cols } = propValue);
} else {
cols = propValue;
}
const infix = brkPoint !== minBreakpoint ? `-${brkPoint}` : '';
if (cols != null) classes.push(`${sizePrefix}${infix}-${cols}`);
});
return (
<Component
ref={ref}
{...props}
className={classNames(className, decoratedBsPrefix, ...classes)}
/>
);
},
);
Row.displayName = 'Row';
Row.propTypes = propTypes;
export default Row;