UNPKG

wix-style-react

Version:
221 lines (188 loc) • 8.64 kB
import _extends from "@babel/runtime/helpers/extends"; 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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } 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 './Tag.st.css'; import CloseButton from '../CloseButton'; import Text from '../Text'; import { dataHooks } from './Tag.helpers'; import { FontUpgradeContext } from '../FontUpgrade/context'; import { generateDataAttr } from '../utils/generateDataAttr'; var tagToTextSize = { tiny: 'tiny', small: 'small', medium: 'small', large: 'medium' }; var noop = function noop() { return {}; }; /** * A Tag component */ var Tag = /*#__PURE__*/function (_React$PureComponent) { _inherits(Tag, _React$PureComponent); var _super = _createSuper(Tag); function Tag() { var _this; _classCallCheck(this, Tag); 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), "_handleRemoveClick", function (event) { var _this$props = _this.props, onRemove = _this$props.onRemove, id = _this$props.id; event.stopPropagation(); onRemove(id); }); _defineProperty(_assertThisInitialized(_this), "_handleOnClick", function (event) { var _this$props2 = _this.props, id = _this$props2.id, onClick = _this$props2.onClick; return onClick && onClick(id, event); }); return _this; } _createClass(Tag, [{ key: "_renderThumb", value: function _renderThumb() { var thumb = this.props.thumb; return thumb ? /*#__PURE__*/React.createElement("span", { "data-hook": dataHooks.thumb, className: classes.thumb }, thumb) : null; } }, { key: "_renderText", value: function _renderText(_ref) { var isMadefor = _ref.isMadefor; var _this$props3 = this.props, size = _this$props3.size, children = _this$props3.children, disabled = _this$props3.disabled, theme = _this$props3.theme; return /*#__PURE__*/React.createElement(Text, { skin: disabled ? 'disabled' : 'standard', light: theme === 'dark', secondary: theme !== 'dark', ellipsis: true, size: tagToTextSize[size], weight: isMadefor || size === 'tiny' ? 'thin' : 'normal', className: classes.text, dataHook: dataHooks.text }, children); } }, { key: "_renderRemoveButton", value: function _renderRemoveButton() { var _this$props4 = this.props, removable = _this$props4.removable, disabled = _this$props4.disabled, size = _this$props4.size, theme = _this$props4.theme; if (removable) { return /*#__PURE__*/React.createElement(CloseButton, { size: size === 'large' ? 'medium' : 'small', skin: theme === 'dark' ? 'light' : 'dark', disabled: disabled, dataHook: dataHooks.removeButton, className: classes.removeButton, onClick: this._handleRemoveClick }); } else { return null; } } }, { key: "render", value: function render() { var _this2 = this; var _this$props5 = this.props, id = _this$props5.id, onClick = _this$props5.onClick, maxWidth = _this$props5.maxWidth, dataHook = _this$props5.dataHook, size = _this$props5.size, disabled = _this$props5.disabled, theme = _this$props5.theme, thumb = _this$props5.thumb, removable = _this$props5.removable, className = _this$props5.className; var clickable = !!onClick; var hoverable = !disabled && !!onClick; return /*#__PURE__*/React.createElement(FontUpgradeContext.Consumer, null, function (_ref2) { var isMadefor = _ref2.active; return /*#__PURE__*/React.createElement("span", _extends({ className: st(classes.root, { withRemoveButton: removable, withThumb: !!thumb, disabled: disabled, clickable: clickable, hoverable: hoverable, size: size, theme: theme }, className) }, generateDataAttr(_objectSpread(_objectSpread({}, _this2.props), {}, { hoverable: hoverable, clickable: clickable }), ['size', 'disabled', 'theme', 'hoverable', 'clickable']), { "data-hook": dataHook, id: id, onClick: _this2._handleOnClick, style: { maxWidth: "".concat(maxWidth, "px") } }), _this2._renderThumb(), _this2._renderText({ isMadefor: isMadefor }), _this2._renderRemoveButton()); }); } }]); return Tag; }(React.PureComponent); _defineProperty(Tag, "displayName", 'Tag'); Tag.propTypes = { /** Applies a data-hook HTML attribute that can be used in the tests */ dataHook: PropTypes.string, /** Sets the text of the tag */ children: PropTypes.node.isRequired, /** Specifies whether the tag should be disabled */ disabled: PropTypes.bool, /** Assigns an unique identifier for the tag */ id: PropTypes.string.isRequired, /** Defines a callback function which is called whenever a tag component is clicked. * It passes the id property as the first parameter and a mouse event as second parameter. */ onClick: PropTypes.func, /** Defines a callback function which is called when removing the tag. It passes the id property as a parameter. */ onRemove: PropTypes.func, /** Specifies whether the tag is removable or not. If enabled, a small clickable X icon is displayed on the right side of a component. */ removable: PropTypes.bool, /** Controls the size of the component */ size: PropTypes.oneOf(['tiny', 'small', 'medium', 'large']), /** Sets the theme of the component */ theme: PropTypes.oneOf(['standard', 'error', 'warning', 'dark', 'neutral', 'success', 'light', 'lightOutlined']), /** Displays a thumbnail at the front of a tag label. Usually contain icons or `<Avatar/>`. */ thumb: PropTypes.element, /** Sets a maximum tag width in pixels */ maxWidth: PropTypes.number, /** Specifies a CSS class name to be appended to the component’s root element */ className: PropTypes.string }; Tag.defaultProps = { size: 'small', removable: true, theme: 'standard' }; export default Tag;