UNPKG

4.72 kBJavaScriptView Raw
1var _excluded = ["className", "cssModule", "widths", "tag"];
2function _extends() { _extends = Object.assign ? Object.assign.bind() : 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; }; return _extends.apply(this, arguments); }
3function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
4function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
5function _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; }
6import React from 'react';
7import PropTypes from 'prop-types';
8import classNames from 'classnames';
9import { mapToCssModules, tagPropType, isObject } from './utils';
10var colWidths = ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'];
11var stringOrNumberProp = PropTypes.oneOfType([PropTypes.number, PropTypes.string]);
12var columnProps = PropTypes.oneOfType([PropTypes.bool, PropTypes.number, PropTypes.string, PropTypes.shape({
13 size: PropTypes.oneOfType([PropTypes.bool, PropTypes.number, PropTypes.string]),
14 order: stringOrNumberProp,
15 offset: stringOrNumberProp
16})]);
17var propTypes = {
18 tag: tagPropType,
19 xs: columnProps,
20 sm: columnProps,
21 md: columnProps,
22 lg: columnProps,
23 xl: columnProps,
24 xxl: columnProps,
25 className: PropTypes.string,
26 cssModule: PropTypes.object,
27 widths: PropTypes.array
28};
29var getColumnSizeClass = function getColumnSizeClass(isXs, colWidth, colSize) {
30 if (colSize === true || colSize === '') {
31 return isXs ? 'col' : "col-".concat(colWidth);
32 }
33 if (colSize === 'auto') {
34 return isXs ? 'col-auto' : "col-".concat(colWidth, "-auto");
35 }
36 return isXs ? "col-".concat(colSize) : "col-".concat(colWidth, "-").concat(colSize);
37};
38export var getColumnClasses = function getColumnClasses(attributes, cssModule) {
39 var widths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : colWidths;
40 var modifiedAttributes = attributes;
41 var colClasses = [];
42 widths.forEach(function (colWidth, i) {
43 var columnProp = modifiedAttributes[colWidth];
44 delete modifiedAttributes[colWidth];
45 if (!columnProp && columnProp !== '') {
46 return;
47 }
48 var isXs = !i;
49 if (isObject(columnProp)) {
50 var _classNames;
51 var colSizeInterfix = isXs ? '-' : "-".concat(colWidth, "-");
52 var colClass = getColumnSizeClass(isXs, colWidth, columnProp.size);
53 colClasses.push(mapToCssModules(classNames((_classNames = {}, _defineProperty(_classNames, colClass, columnProp.size || columnProp.size === ''), _defineProperty(_classNames, "order".concat(colSizeInterfix).concat(columnProp.order), columnProp.order || columnProp.order === 0), _defineProperty(_classNames, "offset".concat(colSizeInterfix).concat(columnProp.offset), columnProp.offset || columnProp.offset === 0), _classNames)), cssModule));
54 } else {
55 var _colClass = getColumnSizeClass(isXs, colWidth, columnProp);
56 colClasses.push(_colClass);
57 }
58 });
59 return {
60 colClasses: colClasses,
61 modifiedAttributes: modifiedAttributes
62 };
63};
64function Col(props) {
65 var className = props.className,
66 cssModule = props.cssModule,
67 _props$widths = props.widths,
68 widths = _props$widths === void 0 ? colWidths : _props$widths,
69 _props$tag = props.tag,
70 Tag = _props$tag === void 0 ? 'div' : _props$tag,
71 attributes = _objectWithoutProperties(props, _excluded);
72 var _getColumnClasses = getColumnClasses(attributes, cssModule, widths),
73 modifiedAttributes = _getColumnClasses.modifiedAttributes,
74 colClasses = _getColumnClasses.colClasses;
75 if (!colClasses.length) {
76 colClasses.push('col');
77 }
78 var classes = mapToCssModules(classNames(className, colClasses), cssModule);
79 return /*#__PURE__*/React.createElement(Tag, _extends({}, modifiedAttributes, {
80 className: classes
81 }));
82}
83Col.propTypes = propTypes;
84export default Col;
\No newline at end of file