UNPKG

react-cm-ui

Version:
374 lines (308 loc) 15.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _lodash = require("lodash"); var _classnames = _interopRequireDefault(require("classnames")); var _propTypes = _interopRequireDefault(require("prop-types")); var _react = _interopRequireDefault(require("react")); var _constants = require("../../global/constants"); var _gridConstants = require("./gridConstants"); var _makeStyles = _interopRequireDefault(require("../../styles/makeStyles")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _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; } function _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; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } 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; } var propTypes = { /** * The content of the GridColumn */ children: _propTypes["default"].node, /** * Override or extend the styles applied to the component. */ classes: _propTypes["default"].shape({ root: _propTypes["default"].string }), /** * Assign additional class names to GridColumn. */ className: _propTypes["default"].string, /** * Used for DOM testing. https://testing-library.com/docs/queries/bytestid/ */ dataTestId: _propTypes["default"].string, /** * The `id` of the GridColumn. */ id: _propTypes["default"].string, /** * Defines the number of columns the component is going to use. * It's applied for the `lg` breakpoint and wider screens if not overridden. */ lg: _propTypes["default"].oneOfType([_propTypes["default"].oneOf(['auto', true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), _propTypes["default"].bool]), /** * Defines the number of columns the component is going to use. * It's applied for the `md` breakpoint and wider screens if not overridden. */ md: _propTypes["default"].oneOfType([_propTypes["default"].oneOf(['auto', true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), _propTypes["default"].bool]), /** * Defines the number of columns the component is going to use. * It's applied for the `sm` breakpoint and wider screens if not overridden. */ sm: _propTypes["default"].oneOfType([_propTypes["default"].oneOf(['auto', true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), _propTypes["default"].bool]), /** * Defines the number of columns the component is going to use. * It's applied for the `xl` breakpoint and wider screens if not overridden. */ xl: _propTypes["default"].oneOfType([_propTypes["default"].oneOf(['auto', true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), _propTypes["default"].bool]), // **** // NOTE: All props below are deprecated and should not be used. // **** /** * Deprecated prop. Please use `classes` to override styles. */ align: _propTypes["default"].oneOf(['stretch']), /** * Deprecated prop. Please use `classes` to override styles. */ desktop: _propTypes["default"].oneOf(['auto', true, false, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), /** * Deprecated prop. Please use `classes` to override styles. */ desktopLarge: _propTypes["default"].oneOf(['auto', true, false, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), /** * Deprecated prop. Please use `classes` to override styles. */ fieldType: _propTypes["default"].oneOf(['checkbox', 'legend', 'radio']), /** * Deprecated prop. Please use `classes` to override styles. */ floated: _propTypes["default"].oneOf(['left', 'right']), /** * Deprecated prop. Please use `classes` to override styles. */ laptop: _propTypes["default"].oneOf(['auto', true, false, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), /** * Deprecated prop. Please use `classes` to override styles. */ mobile: _propTypes["default"].oneOf(['auto', true, false, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), /** * Deprecated prop. Please use `classes` to override styles. */ mobileLarge: _propTypes["default"].oneOf(['auto', true, false, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), /** * Deprecated prop. Please use `classes` to override styles. */ mobileMedium: _propTypes["default"].oneOf(['auto', true, false, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), /** * Deprecated prop. Please use `classes` to override styles. */ style: _propTypes["default"].shape({}), /** * Deprecated prop. Please use `classes` to override styles. */ tablet: _propTypes["default"].oneOf(['auto', true, false, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), /** * Deprecated prop. Please use `classes` to override styles. */ textAlign: _propTypes["default"].oneOf(['center', 'left', 'right']), /** * Deprecated prop. Please use `classes` to override styles. */ verticalAlign: _propTypes["default"].oneOf(['bottom', 'middle', 'top']), /** * Deprecated prop. Please use `classes` to override styles. */ width: _propTypes["default"].oneOf(['auto', true, false, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) }; var defaultProps = { classes: null, children: null, className: null, dataTestId: "".concat(_constants.UI_CLASS_NAME, "-").concat(_constants.BEM_GRID, "_column"), id: null, lg: false, md: false, sm: false, xl: false, // **** // NOTE: All props below are deprecated and should not be used. // **** align: undefined, desktop: false, desktopLarge: false, fieldType: undefined, floated: undefined, laptop: false, mobile: false, mobileLarge: false, mobileMedium: false, style: null, tablet: false, textAlign: undefined, verticalAlign: undefined, width: false }; var generateGrid = function generateGrid(globalStyles, theme, breakpoint) { var styles = {}; _gridConstants.GRID_SIZES.forEach(function (size) { var key = "".concat(_constants.BEM_GRID_COLUMN, "-").concat(breakpoint, "-").concat(size); var width = "".concat(Math.round(size / 12 * 10e7) / 10e5, "%"); if (breakpoint === 'mobile' || breakpoint === 'mobile-medium' || breakpoint === 'mobile-large' || breakpoint === 'tablet' || breakpoint === 'laptop' || breakpoint === 'desktop' || breakpoint === 'desktop-large') { styles[key] = { flexBasis: width, maxWidth: width, width: width }; return; } if (size === true) { // For the auto layouting styles[key] = { flexBasis: 0, flexGrow: 1, maxWidth: '100%' }; return; } if (size === 'auto') { styles[key] = { flexBasis: 'auto', flexGrow: 0, maxWidth: 'none' }; return; } styles[key] = { flexBasis: width, flexGrow: 0, maxWidth: width }; }); switch (breakpoint) { /* eslint-disable no-param-reassign */ case 'mobile': globalStyles[theme.breakpoints.down(374)] = styles; break; case 'mobile-medium': globalStyles[theme.breakpoints.up(375)] = styles; break; case 'mobile-large': globalStyles[theme.breakpoints.up(425)] = styles; break; case 'laptop': globalStyles[theme.breakpoints.up(1024)] = styles; break; case 'desktop': globalStyles[theme.breakpoints.up(1440)] = styles; break; case 'desktop-large': globalStyles[theme.breakpoints.up(1720)] = styles; break; case 'sm': Object.assign(globalStyles, styles); break; default: globalStyles[theme.breakpoints.up(breakpoint)] = styles; /* eslint-enable no-param-reassign */ } }; var useStyles = (0, _makeStyles["default"])(function (theme) { return _objectSpread({ root: { margin: 0, /** * Deprecated classses */ '&$deprecatedAlignStretch': { alignSelf: 'stretch' }, '&deprecatedFloatedLeft': { marginRight: 'auto' }, '&$deprecatedFloatedRight': { marginLeft: 'auto' }, '&$deprecatedTextAlignCenter': { textAlign: 'center' }, '&$deprecatedTextAlignLeft': { textAlign: 'left' }, '&$deprecatedTextAlignRight': { textAlign: 'right' }, '&$deprecatedVerticalAlignBottom': { alignItems: 'bottom', display: 'flex' }, '&$deprecatedVerticalAlignCenter': { alignItems: 'center', display: 'flex' }, '&$deprecatedVerticalAlignTop': { alignItems: 'top', display: 'flex' } /** * End of deprecated classes */ }, /** * Deprecated classses */ deprecatedAlignStretch: {}, deprecatedFloatedLeft: {}, deprecatedFloatedRight: {}, deprecatedTextAlignCenter: {}, deprecatedTextAlignLeft: {}, deprecatedTextAlignRight: {}, deprecatedVerticalAlignBottom: {}, deprecatedVerticalAlignCenter: {}, deprecatedVerticalAlignTop: {} }, (0, _lodash.reduce)([].concat(_toConsumableArray(theme.breakpoints.keys), ['mobile', 'mobile-medium', 'mobile-large', 'laptop', 'desktop', 'desktop-large']), function (accumulator, key) { generateGrid(accumulator, theme, key); return accumulator; }, {})); }); var GridColumn = /*#__PURE__*/_react["default"].forwardRef( /* eslint-disable react-hooks/rules-of-hooks */ // eslint-disable-next-line prefer-arrow-callback function GridColumn(props, ref) { var _ClassNames; var children = props.children, className = props.className, dataTestId = props.dataTestId, id = props.id, lg = props.lg, md = props.md, sm = props.sm, style = props.style, xl = props.xl, otherProps = _objectWithoutProperties(props, ["children", "className", "dataTestId", "id", "lg", "md", "sm", "style", "xl"]); var classes = useStyles(props); var rootClasses = (0, _classnames["default"])(_constants.UI_CLASS_NAME, _constants.BEM_GRID_COLUMN, classes.root, className, (_ClassNames = {}, _defineProperty(_ClassNames, classes["".concat(_constants.BEM_GRID_COLUMN, "-sm-").concat(String(sm || otherProps.width))], sm !== false || otherProps.width !== false), _defineProperty(_ClassNames, classes["".concat(_constants.BEM_GRID_COLUMN, "-md-").concat(String(md || otherProps.tablet))], md !== false || otherProps.tablet !== false), _defineProperty(_ClassNames, classes["".concat(_constants.BEM_GRID_COLUMN, "-lg-").concat(String(lg))], lg !== false), _defineProperty(_ClassNames, classes["".concat(_constants.BEM_GRID_COLUMN, "-xl-").concat(String(xl))], xl !== false), _defineProperty(_ClassNames, classes["".concat(_constants.BEM_GRID_COLUMN, "-mobile-").concat(String(otherProps.mobile))], otherProps.mobile !== false), _defineProperty(_ClassNames, classes["".concat(_constants.BEM_GRID_COLUMN, "-mobile-medium-").concat(String(otherProps.mobileMedium))], otherProps.mobileMedium !== false), _defineProperty(_ClassNames, classes["".concat(_constants.BEM_GRID_COLUMN, "-mobile-large-").concat(String(otherProps.mobileLarge))], otherProps.mobileLarge !== false), _defineProperty(_ClassNames, classes["".concat(_constants.BEM_GRID_COLUMN, "-laptop-").concat(String(otherProps.laptop))], otherProps.laptop !== false), _defineProperty(_ClassNames, classes["".concat(_constants.BEM_GRID_COLUMN, "-desktop-").concat(String(otherProps.desktop))], otherProps.desktop !== false), _defineProperty(_ClassNames, classes["".concat(_constants.BEM_GRID_COLUMN, "-desktop-large-").concat(String(otherProps.desktopLarge))], otherProps.desktopLarge !== false), _defineProperty(_ClassNames, classes.deprecatedAlignStretch, otherProps.align === 'stretch'), _defineProperty(_ClassNames, classes.deprecatedFloatedLeft, otherProps.floated === 'left'), _defineProperty(_ClassNames, classes.deprecatedFloatedRight, otherProps.floated === 'right'), _defineProperty(_ClassNames, classes.deprecatedTextAlignCenter, otherProps.textAlign === 'center'), _defineProperty(_ClassNames, classes.deprecatedTextAlignLeft, otherProps.textAlign === 'left'), _defineProperty(_ClassNames, classes.deprecatedTextAlignRight, otherProps.textAlign === 'right'), _defineProperty(_ClassNames, classes.deprecatedVerticalAlignBottom, otherProps.verticalAlign === 'bottom'), _defineProperty(_ClassNames, classes.deprecatedVerticalAlignCenter, otherProps.verticalAlign === 'center'), _defineProperty(_ClassNames, classes.deprecatedVerticalAlignTop, otherProps.verticalAlign === 'top'), _ClassNames)); return /*#__PURE__*/_react["default"].createElement("div", { className: rootClasses, "data-testid": dataTestId, id: id, ref: ref, style: style }, children); } /* eslint-enable react-hooks/rules-of-hooks */ ); GridColumn.propTypes = propTypes; GridColumn.defaultProps = defaultProps; var _default = GridColumn; exports["default"] = _default;