UNPKG

@wix/design-system

Version:

@wix/design-system

395 lines (391 loc) 13.1 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _ThumbnailSt = require("./Thumbnail.st.css.js"); var _system = require("@wix/wix-ui-icons-common/system"); var _Text = _interopRequireDefault(require("../Text")); var _Focusable = require("../common/Focusable"); var _constants = require("./constants"); var _Box = _interopRequireDefault(require("../Box")); var _TooltipCommon = require("../common/PropTypes/TooltipCommon"); var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/Thumbnail/Thumbnail.js"; var isString = a => typeof a === 'string'; /** * # Thumbnail * Component for showing thumbnails * * It takes full space of parent component, works well together with `<Proportion/>` * */ class Thumbnail extends _react.default.PureComponent { constructor() { super(...arguments); this.thumbnailRef = /*#__PURE__*/_react.default.createRef(); this._renderBackgroundLayout = () => { var { disabled } = this.props; return isString(this.props.backgroundImage) ? /*#__PURE__*/_react.default.createElement("div", { className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.backgroundImage, { disabled }), "data-hook": _constants.dataHooks.thumbnailBackgroundImage, style: { backgroundImage: "url(".concat(this.props.backgroundImage, ")") }, __self: this, __source: { fileName: _jsxFileName, lineNumber: 118, columnNumber: 7 } }) : this.props.backgroundImage; }; this._renderBottomTitle = () => { var { size, title, selected, disabled, tooltipProps = {} } = this.props; return /*#__PURE__*/_react.default.createElement(_Box.default, { align: "center", __self: this, __source: { fileName: _jsxFileName, lineNumber: 132, columnNumber: 7 } }, /*#__PURE__*/_react.default.createElement(_Text.default, { className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.bottomTitle, { selected, disabled }), dataHook: _constants.dataHooks.thumbnailBottomTitle, size: size, tagName: "div", weight: "thin", ellipsis: true, children: title, tooltipProps: tooltipProps, __self: this, __source: { fileName: _jsxFileName, lineNumber: 133, columnNumber: 9 } })); }; this._renderThumbnailImage = () => { var { textPosition, image } = this.props; return /*#__PURE__*/_react.default.createElement("div", { className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.imageContainer, { textPosition }), "data-hook": _constants.dataHooks.thumbnailImage, __self: this, __source: { fileName: _jsxFileName, lineNumber: 151, columnNumber: 7 } }, isString(image) ? /*#__PURE__*/_react.default.createElement("img", { src: image, className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.image, { textPosition }), __self: this, __source: { fileName: _jsxFileName, lineNumber: 156, columnNumber: 11 } }) : image); }; this._renderTitleAndDescription = () => { var { size, title, disabled, ellipsis, maxLines, description, textPosition, tooltipProps = {} } = this.props; return /*#__PURE__*/_react.default.createElement("div", { className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.thumbnailTextContent, { textPosition, size }), __self: this, __source: { fileName: _jsxFileName, lineNumber: 177, columnNumber: 7 } }, title && /*#__PURE__*/_react.default.createElement(_Text.default, { className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.title, { size, textPosition }), dataHook: _constants.dataHooks.thumbnailTitle, size: size, weight: "normal", children: title, skin: disabled ? 'disabled' : undefined, ellipsis: ellipsis, maxLines: maxLines, tooltipProps: tooltipProps, __self: this, __source: { fileName: _jsxFileName, lineNumber: 179, columnNumber: 11 } }), description && /*#__PURE__*/_react.default.createElement(_Text.default, { className: _ThumbnailSt.classes.description, dataHook: _constants.dataHooks.thumbnailDescription, size: size, weight: "thin", secondary: true, children: description, skin: disabled ? 'disabled' : undefined, ellipsis: ellipsis, maxLines: maxLines, tooltipProps: tooltipProps, __self: this, __source: { fileName: _jsxFileName, lineNumber: 192, columnNumber: 11 } })); }; this._renderSelectedIcon = () => /*#__PURE__*/_react.default.createElement("div", { className: _ThumbnailSt.classes.selectedIcon, "data-hook": _constants.dataHooks.thumbnailSelectedIcon, __self: this, __source: { fileName: _jsxFileName, lineNumber: 210, columnNumber: 5 } }, /*#__PURE__*/_react.default.createElement(_system.CheckboxChecked, { height: "7.8", width: "10", __self: this, __source: { fileName: _jsxFileName, lineNumber: 214, columnNumber: 7 } })); this._renderThumbnail = () => { var { selected, disabled, size, width, height, textPosition, children, backgroundImage, image, contentAlignment, newColorsBranding, noPadding, focusableOnFocus, focusableOnBlur, border } = this.props; var hasChildren = !!this.props.children; var hasBackground = !!backgroundImage; var getThumbnailContent = () => { /** * Ignores the rest of the properties and just renders children */ if (hasChildren) { return /*#__PURE__*/_react.default.createElement("div", { className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.customChild, { disabled }), __self: this, __source: { fileName: _jsxFileName, lineNumber: 246, columnNumber: 11 } }, children); } /** * @deprecated flow */ if (hasBackground) { return this._renderBackgroundLayout(); } return /*#__PURE__*/_react.default.createElement("div", { className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.contentContainer, { textPosition }), __self: this, __source: { fileName: _jsxFileName, lineNumber: 260, columnNumber: 9 } }, image && this._renderThumbnailImage(), textPosition === 'inside' && this._renderTitleAndDescription()); }; return /*#__PURE__*/_react.default.createElement("div", { ref: this.thumbnailRef, style: { height }, className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.thumbnail, { selected, disabled, size, hasBackground, hasChildren, textPosition, contentAlignment, noPadding, border }), "data-selected": selected, "data-disabled": disabled, tabIndex: disabled ? null : 0, "data-hook": _constants.dataHooks.thumbnailWrapper, onFocus: focusableOnFocus, onBlur: focusableOnBlur, __self: this, __source: { fileName: _jsxFileName, lineNumber: 268, columnNumber: 7 } }, !this.props.hideSelectedIcon && selected && this._renderSelectedIcon(), getThumbnailContent()); }; this._onKeyDown = event => this.props.onClick && [13 /* enter */, 32 /* space */].some(key => event.keyCode === key) && this.props.onClick(event); this.focus = () => { if (this.thumbnailRef.current) { this.thumbnailRef.current.focus(); } }; } render() { var { children, backgroundImage, title, width, disabled, className, onClick, onMouseOver, dataHook, textPosition, border, selected } = this.props; var hasChildren = !!children; var hasBackground = !!backgroundImage; var showBottomTitle = (hasChildren || hasBackground) && title; return /*#__PURE__*/_react.default.createElement("div", { style: { width }, className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.root, { disabled, border, selected, textPosition }, className), onClick: disabled ? null : onClick, onMouseOver: disabled ? null : onMouseOver, onKeyDown: disabled ? null : this._onKeyDown, "data-hook": dataHook, __self: this, __source: { fileName: _jsxFileName, lineNumber: 327, columnNumber: 7 } }, this._renderThumbnail(), textPosition === 'outside' && this._renderTitleAndDescription(), showBottomTitle ? this._renderBottomTitle() : null); } } Thumbnail.displayName = 'Thumbnail'; 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. * @internal */ className: _propTypes.default.string, /** Adds a border and corner radius * @default true */ border: _propTypes.default.bool, /** 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, /** Defines a callback function which is called every time mouse is over a thumbnail. */ onMouseOver: _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']), /** Enables ellipsis text truncation. */ ellipsis: _propTypes.default.bool, /** Controls maximum lines that is allowed before ellipsis text truncation is applied */ maxLines: _propTypes.default.number, /** Controls text position whether it is outside or inside of a thumbnail */ textPosition: _propTypes.default.oneOf(['inside', 'outside']), /** Turns off padding for thumbnail with textPosition='inside' */ noPadding: _propTypes.default.bool, /** Allows to pass tooltip common props. * @linkTypeTo components-overlays--tooltip * @setTypeName TooltipCommonProps */ tooltipProps: _propTypes.default.shape(_TooltipCommon.TooltipCommonProps) }; Thumbnail.defaultProps = { size: 'medium', selected: false, disabled: false, border: true, contentAlignment: 'center', textPosition: 'inside', ellipsis: false, noPadding: false }; var _default = exports.default = (0, _Focusable.withFocusable)(Thumbnail); //# sourceMappingURL=Thumbnail.js.map