UNPKG

wix-style-react

Version:
164 lines (131 loc) • 7.31 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; 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"; var _excluded = ["disabled", "focusableOnBlur", "focusableOnFocus", "dataHook", "fill", "iconSize", "tooltipContent", "tooltipProps", "className"]; 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 { withFocusable } from "wix-ui-core/dist/es/src/hocs/Focusable/FocusableHOC"; import IconAdd from 'wix-ui-icons-common/Add'; import IconAddSmall from 'wix-ui-icons-common/AddSmall'; import { TooltipCommonProps } from '../common/PropTypes/TooltipCommon'; import { st, classes } from './FillButton.st.css'; import Tooltip from '../Tooltip'; import Proportion from '../Proportion'; import { dataHooks } from './constants'; import { parseColor, parseGradient, parseContrastColor } from './utils'; var iconSmall = dataHooks.iconSmall, iconMedium = dataHooks.iconMedium; /** FillButton */ var FillButton = /*#__PURE__*/function (_React$PureComponent) { _inherits(FillButton, _React$PureComponent); var _super = _createSuper(FillButton); function FillButton() { var _this; _classCallCheck(this, FillButton); 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), "_getBackground", function (fill) { var disabled = _this.props.disabled; if (parseColor(fill) && !disabled) { return { backgroundColor: fill }; } if (parseGradient(fill) && !disabled) { return { backgroundImage: fill }; } return undefined; }); _defineProperty(_assertThisInitialized(_this), "_renderIcon", function () { var _this$props = _this.props, iconSize = _this$props.iconSize, fill = _this$props.fill, disabled = _this$props.disabled; var AddIcon = iconSize === 'small' ? IconAddSmall : IconAdd; return /*#__PURE__*/React.createElement(AddIcon, { style: { color: !disabled && parseContrastColor(fill) }, "data-hook": iconSize === 'small' ? iconSmall : iconMedium }); }); return _this; } _createClass(FillButton, [{ key: "render", value: function render() { var _this$props2 = this.props, disabled = _this$props2.disabled, focusableOnBlur = _this$props2.focusableOnBlur, focusableOnFocus = _this$props2.focusableOnFocus, dataHook = _this$props2.dataHook, fill = _this$props2.fill, iconSize = _this$props2.iconSize, tooltipContent = _this$props2.tooltipContent, _this$props2$tooltipP = _this$props2.tooltipProps, tooltipProps = _this$props2$tooltipP === void 0 ? {} : _this$props2$tooltipP, className = _this$props2.className, rest = _objectWithoutProperties(_this$props2, _excluded); var background = this._getBackground(fill); // For backwards compatibility var content = tooltipProps.content || tooltipContent; return /*#__PURE__*/React.createElement(Tooltip, _extends({ appendTo: "window", disabled: disabled }, tooltipProps, { content: content, dataHook: dataHook, size: "small" }), /*#__PURE__*/React.createElement(Proportion, { className: classes.proportion }, /*#__PURE__*/React.createElement("button", _extends({}, rest, { className: st(classes.root, { disabled: disabled, fill: !!background }, className), style: _objectSpread({}, background), "data-hook": dataHooks.button, onFocus: focusableOnFocus, onBlur: focusableOnBlur, disabled: disabled }), this._renderIcon()))); } }]); return FillButton; }(React.PureComponent); _defineProperty(FillButton, "displayName", 'FillButton'); _defineProperty(FillButton, "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, /** Defines a callback handler. Handler is called whenever a button is clicked. */ onClick: PropTypes.func, /** Controls the size of a plus icon. */ iconSize: PropTypes.oneOf(['small', 'medium']), /** Specify whether button should be disabled or not */ disabled: PropTypes.bool, /** Defines button fill value in string format. Accepts HEX colors and gradients. */ fill: PropTypes.string, /** Specifies a message to display on button hover. */ tooltipContent: PropTypes.node, /** Allows to pass tooltip common props. Check [`<Tooltip/>`](https://www.wix-style-react.com/?activeTab=API&path=%2Fstory%2Fcomponents-api-components--tooltip) API for all available properties. */ tooltipProps: PropTypes.shape(TooltipCommonProps) }); _defineProperty(FillButton, "defaultProps", { iconSize: 'small' }); export default withFocusable(FillButton);