forked from reactstrap/reactstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCol.js
More file actions
94 lines (77 loc) · 2.3 KB
/
Copy pathCol.js
File metadata and controls
94 lines (77 loc) · 2.3 KB
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
import isobject from 'lodash.isobject';
import React, { PropTypes } from 'react';
import classNames from 'classnames';
import { mapToCssModules } from './utils';
const colWidths = ['xs', 'sm', 'md', 'lg', 'xl'];
const stringOrNumberProp = PropTypes.oneOfType([PropTypes.number, PropTypes.string]);
const columnProps = PropTypes.oneOfType([
PropTypes.bool,
PropTypes.number,
PropTypes.string,
PropTypes.shape({
size: PropTypes.oneOfType([PropTypes.bool, PropTypes.number, PropTypes.string]),
push: stringOrNumberProp,
pull: stringOrNumberProp,
offset: stringOrNumberProp
})
]);
const propTypes = {
xs: columnProps,
sm: columnProps,
md: columnProps,
lg: columnProps,
xl: columnProps,
className: PropTypes.string,
cssModule: PropTypes.object,
};
const defaultProps = {
xs: true
};
const getColumnSizeClass = (isXs, colWidth, colSize) => {
if (colSize === true || colSize === '') {
return isXs ? 'col' : `col-${colWidth}`;
} else if (colSize === 'auto') {
return isXs ? 'col-auto' : `col-${colWidth}-auto`;
}
return isXs ? `col-${colSize}` : `col-${colWidth}-${colSize}`;
};
const Col = (props) => {
const {
className,
cssModule,
...attributes
} = props;
const colClasses = [];
colWidths.forEach(colWidth => {
const columnProp = props[colWidth];
delete attributes[colWidth];
if (!columnProp) {
return;
}
const isXs = colWidth === 'xs';
let colClass;
if (isobject(columnProp)) {
const colSizeInterfix = isXs ? '-' : `-${colWidth}-`;
colClass = getColumnSizeClass(isXs, colWidth, columnProp.size);
colClasses.push(mapToCssModules(classNames({
[colClass]: columnProp.size || columnProp.size === '',
[`push${colSizeInterfix}${columnProp.push}`]: columnProp.push,
[`pull${colSizeInterfix}${columnProp.pull}`]: columnProp.pull,
[`offset${colSizeInterfix}${columnProp.offset}`]: columnProp.offset
})), cssModule);
} else {
colClass = getColumnSizeClass(isXs, colWidth, columnProp);
colClasses.push(colClass);
}
});
const classes = mapToCssModules(classNames(
className,
colClasses
), cssModule);
return (
<div {...attributes} className={classes} />
);
};
Col.propTypes = propTypes;
Col.defaultProps = defaultProps;
export default Col;