11import React from 'react' ;
22import PropTypes from 'prop-types' ;
33import classNames from 'classnames' ;
4+ import isobject from 'lodash.isobject' ;
45import { mapToCssModules } from './utils' ;
56
6- const colSizes = [ 'xs' , 'sm' , 'md' , 'lg' , 'xl' ] ;
7+ const colWidths = [ 'xs' , 'sm' , 'md' , 'lg' , 'xl' ] ;
78
89const stringOrNumberProp = PropTypes . oneOfType ( [ PropTypes . number , PropTypes . string ] ) ;
910
@@ -34,17 +35,30 @@ const propTypes = {
3435 md : columnProps ,
3536 lg : columnProps ,
3637 xl : columnProps ,
38+ widths : PropTypes . array ,
3739} ;
3840
3941const defaultProps = {
4042 tag : 'label' ,
43+ widths : colWidths ,
44+ } ;
45+
46+ const getColumnSizeClass = ( isXs , colWidth , colSize ) => {
47+ if ( colSize === true || colSize === '' ) {
48+ return isXs ? 'col' : `col-${ colWidth } ` ;
49+ } else if ( colSize === 'auto' ) {
50+ return isXs ? 'col-auto' : `col-${ colWidth } -auto` ;
51+ }
52+
53+ return isXs ? `col-${ colSize } ` : `col-${ colWidth } -${ colSize } ` ;
4154} ;
4255
4356const Label = ( props ) => {
4457 const {
4558 className,
4659 cssModule,
4760 hidden,
61+ widths,
4862 tag : Tag ,
4963 check,
5064 inline,
@@ -56,19 +70,31 @@ const Label = (props) => {
5670
5771 const colClasses = [ ] ;
5872
59- colSizes . forEach ( colSize => {
60- const columnProp = props [ colSize ] ;
61- delete attributes [ colSize ] ;
73+ widths . forEach ( ( colWidth , i ) => {
74+ let columnProp = props [ colWidth ] ;
75+
76+ delete attributes [ colWidth ] ;
77+
78+ if ( ! columnProp ) {
79+ return ;
80+ }
81+
82+ const isXs = ! i ;
83+ let colClass ;
84+
85+ if ( isobject ( columnProp ) ) {
86+ const colSizeInterfix = isXs ? '-' : `-${ colWidth } -` ;
87+ colClass = getColumnSizeClass ( isXs , colWidth , columnProp . size ) ;
6288
63- if ( columnProp && columnProp . size ) {
6489 colClasses . push ( mapToCssModules ( classNames ( {
65- [ `col- ${ colSize } - ${ columnProp . size } ` ] : columnProp . size ,
66- [ `push- ${ colSize } - ${ columnProp . push } ` ] : columnProp . push ,
67- [ `pull- ${ colSize } - ${ columnProp . pull } ` ] : columnProp . pull ,
68- [ `offset- ${ colSize } - ${ columnProp . offset } ` ] : columnProp . offset ,
90+ [ colClass ] : columnProp . size || columnProp . size === '' ,
91+ [ `push${ colSizeInterfix } ${ columnProp . push } ` ] : columnProp . push || columnProp . push === 0 ,
92+ [ `pull${ colSizeInterfix } ${ columnProp . pull } ` ] : columnProp . pull || columnProp . pull === 0 ,
93+ [ `offset${ colSizeInterfix } ${ columnProp . offset } ` ] : columnProp . offset || columnProp . offset === 0
6994 } ) ) , cssModule ) ;
70- } else if ( columnProp ) {
71- colClasses . push ( `col-${ colSize } -${ columnProp } ` ) ;
95+ } else {
96+ colClass = getColumnSizeClass ( isXs , colWidth , columnProp ) ;
97+ colClasses . push ( colClass ) ;
7298 }
7399 } ) ;
74100
0 commit comments