UNPKG

wix-style-react

Version:
283 lines (242 loc) • 10.6 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(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; } } import React from 'react'; import PropTypes from 'prop-types'; import { st, classes } from './Thumbnail.st.css'; import CheckboxChecked from 'wix-ui-icons-common/system/CheckboxChecked'; import Text from '../Text'; import { withFocusable } from "wix-ui-core/dist/es/src/hocs/Focusable/FocusableHOC"; import { dataHooks } from './constants'; import Box from '../Box'; import { FontUpgradeContext } from '../FontUpgrade/context'; 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) { _inherits(Thumbnail, _React$PureComponent); var _super = _createSuper(Thumbnail); function Thumbnail() { var _this; _classCallCheck(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)); _defineProperty(_assertThisInitialized(_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.createElement(Box, { align: "center" }, /*#__PURE__*/React.createElement(Text, { className: st(classes.bottomTitle, { selected: selected, disabled: disabled }), dataHook: dataHooks.thumbnailBottomTitle, size: size, tagName: "div", weight: "thin", ellipsis: true, children: title })); }); _defineProperty(_assertThisInitialized(_this), "_renderBackgroundLayout", function () { var disabled = _this.props.disabled; return isString(_this.props.backgroundImage) ? /*#__PURE__*/React.createElement("div", { className: st(classes.backgroundImage, { disabled: disabled }), "data-hook": dataHooks.thumbnailBackgroundImage, style: { backgroundImage: "url(".concat(_this.props.backgroundImage, ")") } }) : _this.props.backgroundImage; }); _defineProperty(_assertThisInitialized(_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.createElement("div", null, image && /*#__PURE__*/React.createElement("div", { className: classes.image, "data-hook": dataHooks.thumbnailImage, children: isString(image) ? /*#__PURE__*/React.createElement("img", { src: image }) : image }), title && /*#__PURE__*/React.createElement(FontUpgradeContext.Consumer, null, function (_ref) { var active = _ref.active; return /*#__PURE__*/React.createElement(Text, { className: classes.title, dataHook: dataHooks.thumbnailTitle, size: size, tagName: "div", weight: active && size === 'tiny' ? 'bold' : 'normal', children: title, skin: disabled ? 'disabled' : undefined }); }), description && /*#__PURE__*/React.createElement(Text, { className: classes.description, dataHook: dataHooks.thumbnailDescription, size: size, weight: "thin", tagName: "div", secondary: true, children: description, skin: disabled ? 'disabled' : undefined })); }); _defineProperty(_assertThisInitialized(_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.createElement("div", { className: st(classes.customChild, { disabled: disabled }) }, children); } if (hasBackground) { return _this._renderBackgroundLayout(); } return _this._renderNoBackgroundLayout(); }); _defineProperty(_assertThisInitialized(_this), "_renderSelectedIcon", function () { return /*#__PURE__*/React.createElement("div", { className: classes.selectedIcon, "data-hook": dataHooks.thumbnailSelectedIcon }, /*#__PURE__*/React.createElement(CheckboxChecked, { height: "7.8", width: "10" })); }); _defineProperty(_assertThisInitialized(_this), "_onKeyDown", function (event) { return [13 /* enter */ , 32 /* space */ ].some(function (key) { return event.keyCode === key; }) && _this.props.onClick(event); }); return _this; } _createClass(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.createElement("div", { style: { width: width }, className: st(classes.root, { disabled: disabled }), onClick: disabled ? null : onClick, onKeyDown: disabled ? null : this._onKeyDown, "data-hook": dataHook }, /*#__PURE__*/React.createElement("div", { style: { height: height }, className: st(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": dataHooks.thumbnailWrapper }, !hideSelectedIcon && selected && this._renderSelectedIcon(), this._renderThumbnailContent()), showBottomTitle ? this._renderBottomTitle() : null); } }]); return Thumbnail; }(React.PureComponent); _defineProperty(Thumbnail, "displayName", 'Thumbnail'); _defineProperty(Thumbnail, "propTypes", { /** Applies a data-hook HTML attribute that can be used in the tests. */ dataHook: PropTypes.string, /** Specifies a CSS class name to be appended to the component’s root element. */ className: PropTypes.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.node, /** Sets the title of a thumbnail. */ title: PropTypes.node, /** Sets thumbnail description. */ description: PropTypes.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.node, /** Controls the size of a thumbnail. */ size: PropTypes.oneOf(['tiny', 'small', 'medium']), /** Specifies whether a thumbnail is selected. */ selected: PropTypes.bool, /** Specifies whether thumbnail interactions are disabled. */ disabled: PropTypes.bool, /** Hides a checkmark icon that indicates selected thumbnail. Selected option will be highlighted with border only. */ hideSelectedIcon: PropTypes.bool, /** Render image as a background of a thumbnail. As a result, title is rendered below the image thumbnail. */ backgroundImage: PropTypes.node, /** Defines a callback function which is called every time a thumbnail is clicked. */ onClick: PropTypes.func, /** Controls the width of a thumbnail. */ width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), /** Controls the height of a thumbnail. */ height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), /** Controls vertical alignment of content. */ contentAlignment: PropTypes.oneOf(['top', 'center']) }); _defineProperty(Thumbnail, "defaultProps", { size: 'medium', selected: false, disabled: false, contentAlignment: 'center' }); export default withFocusable(Thumbnail);