@age/quantum
Version:
Catho react components
264 lines (209 loc) • 12.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _styledComponents = _interopRequireWildcard(require("styled-components"));
var _theme = require("../shared/theme");
var _Tab = _interopRequireDefault(require("./Tab"));
var _shared = require("../Grid/sub-components/shared");
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _templateObject() {
var data = _taggedTemplateLiteral(["\n overflow-x: auto;\n "]);
_templateObject = function _templateObject() {
return data;
};
return data;
}
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
var getSkinByThemeColor = function getSkinByThemeColor(skin, themeSkins) {
return themeSkins[skin];
};
var Navbar = _styledComponents.default.nav.attrs({
role: 'tablist'
}).withConfig({
displayName: "TabbedView__Navbar",
componentId: "y0uac7-0"
})(["display:flex;flex-grow:1;flex-shrink:1;padding:0;max-width:100%;clear:left;float:left;", ""], function (_ref) {
var skin = _ref.skin,
_ref$theme = _ref.theme,
skins = _ref$theme.components.tabbedView.skins,
breakpoints = _ref$theme.breakpoints,
large = _ref$theme.spacing.large,
fluid = _ref.fluid;
var _getSkinByThemeColor = getSkinByThemeColor(skin, skins),
background = _getSkinByThemeColor.background;
var mediumQuery = (0, _shared.query)(breakpoints).medium;
return (0, _styledComponents.css)(["background-color:", ";margin:0 0 ", "px 0;", " ", ""], background, large, fluid ? 'width: 100%; overflow-x: scroll;' : null, mediumQuery(_templateObject()));
});
Navbar.displayName = 'Navbar';
var NavItem = _styledComponents.default.button.attrs({
role: 'tab'
}).withConfig({
displayName: "TabbedView__NavItem",
componentId: "y0uac7-1"
})(["border:none;box-sizing:border-box;cursor:pointer;flex-shrink:0;height:56px;line-height:1.5;min-width:90px;outline:none;overflow:hidden;position:relative;text-align:center;transition:background-color 0.2s ease-in-out,color 0.2s ease-in-out;", ""], function (_ref2) {
var skin = _ref2.skin,
children = _ref2.children,
_ref2$theme = _ref2.theme,
skins = _ref2$theme.components.tabbedView.skins,
baseFontSize = _ref2$theme.baseFontSize,
medium = _ref2$theme.spacing.medium,
fluid = _ref2.fluid;
var _getSkinByThemeColor2 = getSkinByThemeColor(skin, skins),
background = _getSkinByThemeColor2.background,
text = _getSkinByThemeColor2.text,
activeText = _getSkinByThemeColor2.activeText,
hoverBackground = _getSkinByThemeColor2.hoverBackground,
border = _getSkinByThemeColor2.border;
return (0, _styledComponents.css)(["border-bottom:1px solid ", ";background-color:", ";font-size:", "px;color:", ";padding:", "px;", " &[aria-selected='true']{&::after{content:' ';background-color:", ";position:absolute;left:0px;bottom:0px;height:3px;border-radius:4px 4px 0 0;width:100%;}border-bottom:1px solid ", ";color:", ";cursor:default;}&:hover{color:", ";border-bottom:1px solid ", ";background-color:", ";}&:focus{color:", ";border:1px solid ", ";background-color:", ";padding:", "px;}"], border, background, baseFontSize, text, medium, fluid ? "\n text-overflow: ellipsis;\n white-space: nowrap;\n overflow: hidden;\n width: ".concat((100 / children.length).toFixed(2), "%;\n ") : null, activeText, activeText, activeText, activeText, activeText, hoverBackground, activeText, activeText, hoverBackground, medium - 1);
});
NavItem.displayName = 'NavItem';
var TitleContainer = _styledComponents.default.span.withConfig({
displayName: "TabbedView__TitleContainer",
componentId: "y0uac7-2"
})(["vertical-align:top;"]);
var BadgeContainer = _styledComponents.default.div.withConfig({
displayName: "TabbedView__BadgeContainer",
componentId: "y0uac7-3"
})(["margin-left:0px;margin-right:-8px;display:inline-block;vertical-align:top;"]);
var IconContainer = _styledComponents.default.div.withConfig({
displayName: "TabbedView__IconContainer",
componentId: "y0uac7-4"
})(["display:inline-block;height:24px;margin-left:0px;margin-right:8px;width:24px;"]);
var TabPanel = _styledComponents.default.div.withConfig({
displayName: "TabbedView__TabPanel",
componentId: "y0uac7-5"
})(["clear:left;"]);
var RenderIf = function RenderIf(_ref3) {
var conditional = _ref3.conditional,
children = _ref3.children;
return conditional && children;
};
var TabbedView =
/*#__PURE__*/
function (_React$Component) {
_inherits(TabbedView, _React$Component);
function TabbedView(props) {
var _this;
_classCallCheck(this, TabbedView);
_this = _possibleConstructorReturn(this, _getPrototypeOf(TabbedView).call(this, props));
_this.onTabClick = function (tab) {
_this.setState({
activeTab: tab
});
};
_this.sanitize = function (str) {
return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(' ', '-').toLowerCase();
};
var children = props.children,
activeTab = props.activeTab;
if (activeTab) {
_this.state = {
activeTab: activeTab
};
} else {
var _React$Children$toArr = _react.default.Children.toArray(children),
_React$Children$toArr2 = _slicedToArray(_React$Children$toArr, 1),
title = _React$Children$toArr2[0].props.title;
_this.state = {
activeTab: title
};
}
return _this;
}
_createClass(TabbedView, [{
key: "render",
value: function render() {
var _this2 = this;
var _this$props = this.props,
children = _this$props.children,
skin = _this$props.skin,
theme = _this$props.theme,
fluid = _this$props.fluid;
var activeTab = this.state.activeTab;
return _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(Navbar, {
theme: theme,
skin: skin,
fluid: fluid
}, _react.default.Children.map(children, function (_ref4) {
var _ref4$props = _ref4.props,
title = _ref4$props.title,
badge = _ref4$props.badge,
icon = _ref4$props.icon;
return _react.default.createElement(NavItem, {
fluid: fluid,
key: title,
onClick: function onClick() {
return _this2.onTabClick(title);
},
skin: skin,
theme: theme,
id: "".concat(_this2.sanitize(title), "-tab"),
"aria-controls": "".concat(_this2.sanitize(title), "-panel"),
"aria-selected": title === activeTab
}, icon && _react.default.createElement(IconContainer, null, icon), title && _react.default.createElement(TitleContainer, null, title), badge && _react.default.createElement(BadgeContainer, null, badge));
})), _react.default.Children.map(children, function (_ref5) {
var _ref5$props = _ref5.props,
title = _ref5$props.title,
tabContent = _ref5$props.children;
return _react.default.createElement(RenderIf, {
conditional: title === activeTab
}, _react.default.createElement(TabPanel, {
role: "tabpanel",
key: "".concat(_this2.sanitize(title), "-panel-key"),
id: "".concat(_this2.sanitize(title), "-panel"),
"aria-labelledby": "".concat(_this2.sanitize(title), "-tab")
}, tabContent));
}));
}
}]);
return TabbedView;
}(_react.default.Component);
TabbedView.Tab = _Tab.default;
TabbedView.propTypes = {
/** Used to expand all tabs along all the parent width */
fluid: _propTypes.default.bool,
children: _propTypes.default.oneOfType([_propTypes.default.arrayOf(_propTypes.default.node), _propTypes.default.node]).isRequired,
activeTab: _propTypes.default.string,
skin: _propTypes.default.oneOf(['neutral', 'primary']),
theme: _propTypes.default.shape({
components: _propTypes.default.shape({
tabbedView: _propTypes.default.object
}),
baseFontSize: _propTypes.default.number,
breakpoints: _propTypes.default.object,
spacing: _propTypes.default.object
})
};
TabbedView.defaultProps = {
fluid: false,
activeTab: undefined,
skin: 'neutral',
theme: {
components: _theme.components,
baseFontSize: _theme.baseFontSize,
breakpoints: _theme.breakpoints,
spacing: _theme.spacing
}
};
TabbedView.displayName = 'TabbedView';
var _default = TabbedView;
exports.default = _default;