UNPKG

@kiwicom/orbit-components

Version:

Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.

218 lines (172 loc) 9.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.StyledTile = void 0; var React = _interopRequireWildcard(require("react")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _defaultTheme = _interopRequireDefault(require("../defaultTheme")); var _TileHeader = _interopRequireWildcard(require("./TileHeader")); var _TileExpandable = _interopRequireDefault(require("./TileExpandable")); var _keyMaps = _interopRequireDefault(require("../common/keyMaps")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } 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 _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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } 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 _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; } 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; } var StyledTile = (0, _styledComponents.default)(function (_ref) { var theme = _ref.theme, icon = _ref.icon, title = _ref.title, external = _ref.external, props = _objectWithoutProperties(_ref, ["theme", "icon", "title", "external"]); var Component = props.href ? "a" : "div"; return React.createElement(Component, props, props.children); }).withConfig({ displayName: "Tile__StyledTile", componentId: "sc-15i01z6-0" })(["display:block;width:100%;box-sizing:border-box;font-family:", ";text-decoration:none;background:", ";border-radius:", ";box-shadow:", ";transition:box-shadow ", " ease-in-out;&:hover,&:focus{outline:0;box-shadow:", ";}&:hover ", "{color:", ";}"], function (_ref2) { var theme = _ref2.theme; return theme.orbit.fontFamily; }, function (_ref3) { var theme = _ref3.theme; return theme.orbit.paletteWhite; }, function (_ref4) { var theme = _ref4.theme; return theme.orbit.borderRadiusNormal; }, function (_ref5) { var theme = _ref5.theme; return theme.orbit.boxShadowAction; }, function (_ref6) { var theme = _ref6.theme; return theme.orbit.durationFast; }, function (_ref7) { var theme = _ref7.theme; return theme.orbit.boxShadowActionActive; }, _TileHeader.StyledIconRight, function (_ref8) { var theme = _ref8.theme; return theme.orbit.paletteInkLightHover; }); exports.StyledTile = StyledTile; StyledTile.defaultProps = { theme: _defaultTheme.default }; var Tile = /*#__PURE__*/ function (_React$PureComponent) { _inherits(Tile, _React$PureComponent); function Tile() { var _getPrototypeOf2; var _this; _classCallCheck(this, Tile); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Tile)).call.apply(_getPrototypeOf2, [this].concat(args))); _defineProperty(_assertThisInitialized(_this), "state", { expanded: false, initialExpanded: false }); _defineProperty(_assertThisInitialized(_this), "setExpanded", function (_ref9) { var expanded = _ref9.expanded, initialExpanded = _ref9.initialExpanded; _this.setState({ expanded: expanded, initialExpanded: initialExpanded }); }); _defineProperty(_assertThisInitialized(_this), "isExpandable", function () { var _this$props = _this.props, href = _this$props.href, children = _this$props.children; return !!(!href && children); // Tile is expandable if - not href && children are passed }); _defineProperty(_assertThisInitialized(_this), "toggleExpandable", function () { if (_this.isExpandable()) { _this.setExpanded({ expanded: !_this.state.expanded, initialExpanded: false }); } }); _defineProperty(_assertThisInitialized(_this), "handleClick", function (ev) { var onClick = _this.props.onClick; _this.toggleExpandable(); if (onClick) { onClick(ev); } }); _defineProperty(_assertThisInitialized(_this), "handleKeyDown", function (ev) { var onClick = _this.props.onClick; if (ev.keyCode === _keyMaps.default.ENTER) { _this.toggleExpandable(); if (onClick) { onClick(ev); } } else if (ev.keyCode === _keyMaps.default.SPACE) { ev.preventDefault(); _this.toggleExpandable(); if (onClick) { onClick(ev); } } }); return _this; } _createClass(Tile, [{ key: "componentDidMount", value: function componentDidMount() { var expanded = this.props.expanded; this.setExpanded({ expanded: expanded, initialExpanded: expanded }); } }, { key: "render", value: function render() { var _this$props2 = this.props, href = _this$props2.href, external = _this$props2.external, icon = _this$props2.icon, title = _this$props2.title, description = _this$props2.description, children = _this$props2.children, dataTest = _this$props2.dataTest; var isExpandable = this.isExpandable(); var isExpanded = this.state.expanded; return React.createElement(StyledTile, { target: !isExpandable && external ? "_blank" : undefined, rel: !isExpandable && external ? "noopener noreferrer" : undefined, href: !isExpandable ? href : undefined, "data-test": dataTest, onKeyDown: this.handleKeyDown, tabIndex: href ? undefined : "0", role: href ? undefined : "button" }, React.createElement(_TileHeader.default, { icon: icon, title: title, description: description, external: external, onClick: this.handleClick, isExpandable: isExpandable, isExpanded: isExpanded }), isExpandable && children && React.createElement(_TileExpandable.default, { expanded: isExpanded, initialExpanded: this.state.initialExpanded }, children)); } }]); return Tile; }(React.PureComponent); var _default = Tile; exports.default = _default;