1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
8 |
|
9 | var _lodash = require('lodash.isobject');
|
10 |
|
11 | var _lodash2 = _interopRequireDefault(_lodash);
|
12 |
|
13 | var _react = require('react');
|
14 |
|
15 | var _react2 = _interopRequireDefault(_react);
|
16 |
|
17 | var _classnames = require('classnames');
|
18 |
|
19 | var _classnames2 = _interopRequireDefault(_classnames);
|
20 |
|
21 | var _utils = require('./utils');
|
22 |
|
23 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
24 |
|
25 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
26 |
|
27 | function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
|
28 |
|
29 | var colWidths = ['xs', 'sm', 'md', 'lg', 'xl'];
|
30 | var stringOrNumberProp = _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]);
|
31 |
|
32 | var columnProps = _react.PropTypes.oneOfType([_react.PropTypes.bool, _react.PropTypes.number, _react.PropTypes.string, _react.PropTypes.shape({
|
33 | size: _react.PropTypes.oneOfType([_react.PropTypes.bool, _react.PropTypes.number, _react.PropTypes.string]),
|
34 | push: stringOrNumberProp,
|
35 | pull: stringOrNumberProp,
|
36 | offset: stringOrNumberProp
|
37 | })]);
|
38 |
|
39 | var propTypes = {
|
40 | tag: _react.PropTypes.oneOfType([_react.PropTypes.func, _react.PropTypes.string]),
|
41 | xs: columnProps,
|
42 | sm: columnProps,
|
43 | md: columnProps,
|
44 | lg: columnProps,
|
45 | xl: columnProps,
|
46 | className: _react.PropTypes.string,
|
47 | cssModule: _react.PropTypes.object,
|
48 | widths: _react.PropTypes.array
|
49 | };
|
50 |
|
51 | var defaultProps = {
|
52 | tag: 'div',
|
53 | widths: colWidths
|
54 | };
|
55 |
|
56 | var getColumnSizeClass = function getColumnSizeClass(isXs, colWidth, colSize) {
|
57 | if (colSize === true || colSize === '') {
|
58 | return isXs ? 'col' : 'col-' + colWidth;
|
59 | } else if (colSize === 'auto') {
|
60 | return isXs ? 'col-auto' : 'col-' + colWidth + '-auto';
|
61 | }
|
62 |
|
63 | return isXs ? 'col-' + colSize : 'col-' + colWidth + '-' + colSize;
|
64 | };
|
65 |
|
66 | var Col = function Col(props) {
|
67 | var className = props.className,
|
68 | cssModule = props.cssModule,
|
69 | widths = props.widths,
|
70 | Tag = props.tag,
|
71 | attributes = _objectWithoutProperties(props, ['className', 'cssModule', 'widths', 'tag']);
|
72 |
|
73 | var colClasses = [];
|
74 |
|
75 | widths.forEach(function (colWidth, i) {
|
76 | var columnProp = props[colWidth];
|
77 |
|
78 | if (!i && columnProp === undefined) {
|
79 | columnProp = true;
|
80 | }
|
81 |
|
82 | delete attributes[colWidth];
|
83 |
|
84 | if (!columnProp) {
|
85 | return;
|
86 | }
|
87 |
|
88 | var isXs = !i;
|
89 | var colClass = void 0;
|
90 |
|
91 | if ((0, _lodash2.default)(columnProp)) {
|
92 | var _classNames;
|
93 |
|
94 | var colSizeInterfix = isXs ? '-' : '-' + colWidth + '-';
|
95 | colClass = getColumnSizeClass(isXs, colWidth, columnProp.size);
|
96 |
|
97 | colClasses.push((0, _utils.mapToCssModules)((0, _classnames2.default)((_classNames = {}, _defineProperty(_classNames, colClass, columnProp.size || columnProp.size === ''), _defineProperty(_classNames, 'push' + colSizeInterfix + columnProp.push, columnProp.push), _defineProperty(_classNames, 'pull' + colSizeInterfix + columnProp.pull, columnProp.pull), _defineProperty(_classNames, 'offset' + colSizeInterfix + columnProp.offset, columnProp.offset), _classNames))), cssModule);
|
98 | } else {
|
99 | colClass = getColumnSizeClass(isXs, colWidth, columnProp);
|
100 | colClasses.push(colClass);
|
101 | }
|
102 | });
|
103 |
|
104 | var classes = (0, _utils.mapToCssModules)((0, _classnames2.default)(className, colClasses), cssModule);
|
105 |
|
106 | return _react2.default.createElement(Tag, _extends({}, attributes, { className: classes }));
|
107 | };
|
108 |
|
109 | Col.propTypes = propTypes;
|
110 | Col.defaultProps = defaultProps;
|
111 |
|
112 | exports.default = Col; |
\ | No newline at end of file |