UNPKG

wix-style-react

Version:
368 lines (365 loc) • 12.3 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"); 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 _context = require("../WixStyleReactProvider/context"); var _jsxFileName = "/home/builduser/work/a9c1ac8876d5057c/packages/wix-style-react/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._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: 98, columnNumber: 7 } }) : this.props.backgroundImage; }; this._renderBottomTitle = () => { var { size, title, selected, disabled } = this.props; return /*#__PURE__*/_react.default.createElement(_Box.default, { align: "center", __self: this, __source: { fileName: _jsxFileName, lineNumber: 112, 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, __self: this, __source: { fileName: _jsxFileName, lineNumber: 113, 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: 130, 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: 135, columnNumber: 11 } }) : image); }; this._renderTitleAndDescription = () => { var { size, title, disabled, ellipsis, maxLines, description, textPosition } = this.props; return /*#__PURE__*/_react.default.createElement("div", { className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.thumbnailTextContent, { textPosition, size }), __self: this, __source: { fileName: _jsxFileName, lineNumber: 155, 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: size === 'tiny' ? 'bold' : 'normal', children: title, skin: disabled ? 'disabled' : undefined, ellipsis: ellipsis, maxLines: maxLines, __self: this, __source: { fileName: _jsxFileName, lineNumber: 157, 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, __self: this, __source: { fileName: _jsxFileName, lineNumber: 169, columnNumber: 11 } })); }; this._renderSelectedIcon = () => /*#__PURE__*/_react.default.createElement("div", { className: _ThumbnailSt.classes.selectedIcon, "data-hook": _constants.dataHooks.thumbnailSelectedIcon, __self: this, __source: { fileName: _jsxFileName, lineNumber: 186, columnNumber: 5 } }, /*#__PURE__*/_react.default.createElement(_system.CheckboxChecked, { height: "7.8", width: "10", __self: this, __source: { fileName: _jsxFileName, lineNumber: 190, columnNumber: 7 } })); this._renderThumbnail = () => { var { selected, disabled, size, width, height, textPosition, children, backgroundImage, image, contentAlignment, newColorsBranding, noPadding } = 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: 219, 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: 233, columnNumber: 9 } }, image && this._renderThumbnailImage(), textPosition === 'inside' && this._renderTitleAndDescription()); }; return /*#__PURE__*/_react.default.createElement("div", { style: { height }, className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.thumbnail, { selected, disabled, size, hasBackground, hasChildren, textPosition, contentAlignment, newColorsBranding, noPadding }), "data-selected": selected, "data-disabled": disabled, tabIndex: disabled ? null : 0, "data-hook": _constants.dataHooks.thumbnailWrapper, __self: this, __source: { fileName: _jsxFileName, lineNumber: 241, columnNumber: 7 } }, !this.props.hideSelectedIcon && selected && this._renderSelectedIcon(), getThumbnailContent()); }; this._onKeyDown = event => [13 /* enter */, 32 /* space */].some(key => event.keyCode === key) && this.props.onClick(event); } render() { var { children, backgroundImage, title, width, disabled, className, onClick, dataHook, textPosition } = this.props; var hasChildren = !!children; var hasBackground = !!backgroundImage; var showBottomTitle = (hasChildren || hasBackground) && title; return /*#__PURE__*/_react.default.createElement(_context.WixStyleReactContext.Consumer, { __self: this, __source: { fileName: _jsxFileName, lineNumber: 287, columnNumber: 7 } }, _ref => { var { newColorsBranding } = _ref; return /*#__PURE__*/_react.default.createElement("div", { style: { width }, className: (0, _ThumbnailSt.st)(_ThumbnailSt.classes.root, { disabled }, className), onClick: disabled ? null : onClick, onKeyDown: disabled ? null : this._onKeyDown, "data-hook": dataHook, __self: this, __source: { fileName: _jsxFileName, lineNumber: 289, columnNumber: 11 } }, 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. */ 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']), /** 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 }; Thumbnail.defaultProps = { size: 'medium', selected: false, disabled: false, contentAlignment: 'center', textPosition: 'inside', ellipsis: false, noPadding: false }; var _default = exports.default = (0, _Focusable.withFocusable)(Thumbnail); //# sourceMappingURL=Thumbnail.js.map