@@ -111,50 +111,74 @@ const propTypes = {
111
111
xxl : column ,
112
112
} ;
113
113
114
+ export interface UseColMetadata {
115
+ as ?: React . ElementType ;
116
+ bsPrefix : string ;
117
+ spans : string [ ] ;
118
+ }
119
+
120
+ export function useCol ( {
121
+ as,
122
+ bsPrefix,
123
+ className,
124
+ ...props
125
+ } : ColProps ) : [ any , UseColMetadata ] {
126
+ bsPrefix = useBootstrapPrefix ( bsPrefix , 'col' ) ;
127
+
128
+ const spans : string [ ] = [ ] ;
129
+ const classes : string [ ] = [ ] ;
130
+
131
+ DEVICE_SIZES . forEach ( ( brkPoint ) => {
132
+ const propValue = props [ brkPoint ] ;
133
+ delete props [ brkPoint ] ;
134
+
135
+ let span : ColSize | undefined ;
136
+ let offset : NumberAttr | undefined ;
137
+ let order : ColOrder | undefined ;
138
+
139
+ if ( typeof propValue === 'object' && propValue != null ) {
140
+ ( { span = true , offset, order } = propValue ) ;
141
+ } else {
142
+ span = propValue ;
143
+ }
144
+
145
+ const infix = brkPoint !== 'xs' ? `-${ brkPoint } ` : '' ;
146
+
147
+ if ( span )
148
+ spans . push (
149
+ span === true ? `${ bsPrefix } ${ infix } ` : `${ bsPrefix } ${ infix } -${ span } ` ,
150
+ ) ;
151
+
152
+ if ( order != null ) classes . push ( `order${ infix } -${ order } ` ) ;
153
+ if ( offset != null ) classes . push ( `offset${ infix } -${ offset } ` ) ;
154
+ } ) ;
155
+
156
+ return [
157
+ { ...props , className : classNames ( className , ...classes , ...spans ) } ,
158
+ {
159
+ as,
160
+ bsPrefix,
161
+ spans,
162
+ } ,
163
+ ] ;
164
+ }
165
+
114
166
const Col : BsPrefixRefForwardingComponent < 'div' , ColProps > = React . forwardRef <
115
167
HTMLElement ,
116
168
ColProps
117
169
> (
118
170
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
119
- ( { bsPrefix, className, as : Component = 'div' , ...props } , ref ) => {
120
- const prefix = useBootstrapPrefix ( bsPrefix , 'col' ) ;
121
- const spans : string [ ] = [ ] ;
122
- const classes : string [ ] = [ ] ;
123
-
124
- DEVICE_SIZES . forEach ( ( brkPoint ) => {
125
- const propValue = props [ brkPoint ] ;
126
- delete props [ brkPoint ] ;
127
-
128
- let span : ColSize | undefined ;
129
- let offset : NumberAttr | undefined ;
130
- let order : ColOrder | undefined ;
131
-
132
- if ( typeof propValue === 'object' && propValue != null ) {
133
- ( { span = true , offset, order } = propValue ) ;
134
- } else {
135
- span = propValue ;
136
- }
137
-
138
- const infix = brkPoint !== 'xs' ? `-${ brkPoint } ` : '' ;
139
-
140
- if ( span )
141
- spans . push (
142
- span === true ? `${ prefix } ${ infix } ` : `${ prefix } ${ infix } -${ span } ` ,
143
- ) ;
144
-
145
- if ( order != null ) classes . push ( `order${ infix } -${ order } ` ) ;
146
- if ( offset != null ) classes . push ( `offset${ infix } -${ offset } ` ) ;
147
- } ) ;
148
-
149
- if ( ! spans . length ) {
150
- spans . push ( prefix ) ; // plain 'col'
151
- }
171
+ ( props , ref ) => {
172
+ const [
173
+ { className, ...colProps } ,
174
+ { as : Component = 'div' , bsPrefix, spans } ,
175
+ ] = useCol ( props ) ;
152
176
153
177
return (
154
178
< Component
155
- { ...props }
179
+ { ...colProps }
156
180
ref = { ref }
157
- className = { classNames ( className , ... spans , ... classes ) }
181
+ className = { classNames ( className , ! spans . length && bsPrefix ) }
158
182
/>
159
183
) ;
160
184
} ,
0 commit comments