UNPKG

wix-style-react

Version:
298 lines (249 loc) • 12.2 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _ThumbnailSt = require("./Thumbnail.st.css"); var _CheckboxChecked = _interopRequireDefault(require("wix-ui-icons-common/system/CheckboxChecked")); var _Text = _interopRequireDefault(require("../Text")); var _FocusableHOC = require("wix-ui-core/dist/src/hocs/Focusable/FocusableHOC"); var _constants = require("./constants"); var _Box = _interopRequireDefault(require("../Box")); var _context = require("../FontUpgrade/context"); function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } var isString = function isString(a) { return typeof a === 'string'; }; /** * # Thumbnail * Component for showing thumbnails * * It takes full space of parent component, works well together with `<Proportion/>` * */ var Thumbnail = /*#__PURE__*/function (_React$PureComponent) { (0, _inherits2["default"])(Thumbnail, _React$PureComponent); var _super = _createSuper(Thumbnail); function Thumbnail() { var _this; (0, _classCallCheck2["default"])(this, Thumbnail); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_renderBottomTitle", function () { var _this$props = _this.props, size = _this$props.size, title = _this$props.title, selected = _this$props.selected, disabled = _this$props.disabled; return /*#__PURE__*/_react["default"].createElement(_Box["default"], { align: "center" }, /*#__PURE__*/_react["default"].createElement(_Text["default"], { className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.bottomTitle, { selected: selected, disabled: disabled }), dataHook: _constants.dataHooks.thumbnailBottomTitle, size: size, tagName: "div", weight: "thin", ellipsis: true, children: title })); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_renderBackgroundLayout", function () { var disabled = _this.props.disabled; return isString(_this.props.backgroundImage) ? /*#__PURE__*/_react["default"].createElement("div", { className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.backgroundImage, { disabled: disabled }), "data-hook": _constants.dataHooks.thumbnailBackgroundImage, style: { backgroundImage: "url(".concat(_this.props.backgroundImage, ")") } }) : _this.props.backgroundImage; }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_renderNoBackgroundLayout", function () { var _this$props2 = _this.props, title = _this$props2.title, description = _this$props2.description, image = _this$props2.image, size = _this$props2.size, disabled = _this$props2.disabled; return /*#__PURE__*/_react["default"].createElement("div", null, image && /*#__PURE__*/_react["default"].createElement("div", { className: _ThumbnailSt.classes.image, "data-hook": _constants.dataHooks.thumbnailImage, children: isString(image) ? /*#__PURE__*/_react["default"].createElement("img", { src: image }) : image }), title && /*#__PURE__*/_react["default"].createElement(_context.FontUpgradeContext.Consumer, null, function (_ref) { var active = _ref.active; return /*#__PURE__*/_react["default"].createElement(_Text["default"], { className: _ThumbnailSt.classes.title, dataHook: _constants.dataHooks.thumbnailTitle, size: size, tagName: "div", weight: active && size === 'tiny' ? 'bold' : 'normal', children: title, skin: disabled ? 'disabled' : undefined }); }), description && /*#__PURE__*/_react["default"].createElement(_Text["default"], { className: _ThumbnailSt.classes.description, dataHook: _constants.dataHooks.thumbnailDescription, size: size, weight: "thin", tagName: "div", secondary: true, children: description, skin: disabled ? 'disabled' : undefined })); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_renderThumbnailContent", function () { var _this$props3 = _this.props, backgroundImage = _this$props3.backgroundImage, children = _this$props3.children, disabled = _this$props3.disabled; var hasBackground = !!backgroundImage; var hasChildren = !!children; if (hasChildren) { return /*#__PURE__*/_react["default"].createElement("div", { className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.customChild, { disabled: disabled }) }, children); } if (hasBackground) { return _this._renderBackgroundLayout(); } return _this._renderNoBackgroundLayout(); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_renderSelectedIcon", function () { return /*#__PURE__*/_react["default"].createElement("div", { className: _ThumbnailSt.classes.selectedIcon, "data-hook": _constants.dataHooks.thumbnailSelectedIcon }, /*#__PURE__*/_react["default"].createElement(_CheckboxChecked["default"], { height: "7.8", width: "10" })); }); (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_onKeyDown", function (event) { return [13 /* enter */ , 32 /* space */ ].some(function (key) { return event.keyCode === key; }) && _this.props.onClick(event); }); return _this; } (0, _createClass2["default"])(Thumbnail, [{ key: "render", value: function render() { var _this$props4 = this.props, children = _this$props4.children, dataHook = _this$props4.dataHook, size = _this$props4.size, selected = _this$props4.selected, disabled = _this$props4.disabled, title = _this$props4.title, backgroundImage = _this$props4.backgroundImage, onClick = _this$props4.onClick, hideSelectedIcon = _this$props4.hideSelectedIcon, width = _this$props4.width, height = _this$props4.height, contentAlignment = _this$props4.contentAlignment, focusableOnFocus = _this$props4.focusableOnFocus, focusableOnBlur = _this$props4.focusableOnBlur, className = _this$props4.className; var hasChildren = !!children; var hasBackground = !!backgroundImage; var showBottomTitle = (hasChildren || hasBackground) && title; return /*#__PURE__*/_react["default"].createElement("div", { style: { width: width }, className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.root, { disabled: disabled }), onClick: disabled ? null : onClick, onKeyDown: disabled ? null : this._onKeyDown, "data-hook": dataHook }, /*#__PURE__*/_react["default"].createElement("div", { style: { height: height }, className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.thumbnail, { selected: selected, disabled: disabled, size: size, hasBackground: hasBackground, hasChildren: hasChildren, contentAlignment: contentAlignment }, className), "data-selected": selected, "data-disabled": disabled, onFocus: focusableOnFocus, onBlur: focusableOnBlur, tabIndex: disabled ? null : 0, "data-hook": _constants.dataHooks.thumbnailWrapper }, !hideSelectedIcon && selected && this._renderSelectedIcon(), this._renderThumbnailContent()), showBottomTitle ? this._renderBottomTitle() : null); } }]); return Thumbnail; }(_react["default"].PureComponent); (0, _defineProperty2["default"])(Thumbnail, "displayName", 'Thumbnail'); (0, _defineProperty2["default"])(Thumbnail, "propTypes", { /** Applies a data-hook HTML attribute that can be used in the tests. */ dataHook: _propTypes["default"].string, /** Specifies a CSS class name to be appended to the component’s root element. */ className: _propTypes["default"].string, /** Renders specified children items inside of the thumbnail. Accepts any kind of content. When passed, title will be rendered below a thumbnail. */ children: _propTypes["default"].node, /** Sets the title of a thumbnail. */ title: _propTypes["default"].node, /** Sets thumbnail description. */ description: _propTypes["default"].node, /** Specifies an image to be displayed inside of a thumbnail. * If given as string, it will be used within `<img/>`. * Otherwise can be given as React.Node */ image: _propTypes["default"].node, /** Controls the size of a thumbnail. */ size: _propTypes["default"].oneOf(['tiny', 'small', 'medium']), /** Specifies whether a thumbnail is selected. */ selected: _propTypes["default"].bool, /** Specifies whether thumbnail interactions are disabled. */ disabled: _propTypes["default"].bool, /** Hides a checkmark icon that indicates selected thumbnail. Selected option will be highlighted with border only. */ hideSelectedIcon: _propTypes["default"].bool, /** Render image as a background of a thumbnail. As a result, title is rendered below the image thumbnail. */ backgroundImage: _propTypes["default"].node, /** Defines a callback function which is called every time a thumbnail is clicked. */ onClick: _propTypes["default"].func, /** Controls the width of a thumbnail. */ width: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]), /** Controls the height of a thumbnail. */ height: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]), /** Controls vertical alignment of content. */ contentAlignment: _propTypes["default"].oneOf(['top', 'center']) }); (0, _defineProperty2["default"])(Thumbnail, "defaultProps", { size: 'medium', selected: false, disabled: false, contentAlignment: 'center' }); var _default = (0, _FocusableHOC.withFocusable)(Thumbnail); exports["default"] = _default;