UNPKG

react-cm-ui

Version:
618 lines (553 loc) 22.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.propTypes = void 0; var _lodash = require("lodash"); var _classnames = _interopRequireDefault(require("classnames")); var _propTypes = _interopRequireDefault(require("prop-types")); var _react = _interopRequireDefault(require("react")); var _constants = require("../../global/constants"); var _utils = _interopRequireDefault(require("../../utils/utils")); var _withStyles = _interopRequireDefault(require("../../styles/withStyles")); var _models = require("./models"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } 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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var propTypes = { as: _propTypes["default"].oneOf(Object.values(_models.AsType)), /** * Primary content. */ children: _propTypes["default"].node.isRequired, /** * Additional classes. */ className: _propTypes["default"].string, /** * Override or extend the styles applied to ButtonDropdown. */ classes: _propTypes["default"].shape({ colorActive: _propTypes["default"].string, colorDefault: _propTypes["default"].string, colorError: _propTypes["default"].string, colorLink: _propTypes["default"].string, colorPrimary: _propTypes["default"].string, colorSecondary: _propTypes["default"].string, colorSuccess: _propTypes["default"].string, colorWarning: _propTypes["default"].string, compact: _propTypes["default"].string, contained: _propTypes["default"].string, disabled: _propTypes["default"].string, fixedWidth: _propTypes["default"].string, fullWidth: _propTypes["default"].string, icon: _propTypes["default"].string, innerContainer: _propTypes["default"].string, inverse: _propTypes["default"].string, outlined: _propTypes["default"].string, pill: _propTypes["default"].string, relax: _propTypes["default"].string, root: _propTypes["default"].string, text: _propTypes["default"].string, transparent: _propTypes["default"].string }), /** * Color of the button. */ color: _propTypes["default"].oneOf(Object.values(_models.ColorType)), /** * A button can reduce its padding. */ compact: _propTypes["default"].bool, /** * A button can be disabled. */ disabled: _propTypes["default"].bool, /** * The Button will be resized to its parent container's width. */ fullWidth: _propTypes["default"].bool, /** * The URL that the hyperlink points to. */ href: _propTypes["default"].string, /** * If `true`, the button will be a square, housing the icon child. */ icon: _propTypes["default"].bool, /** * Assign the button an id attribute value. */ id: _propTypes["default"].string, /** * Allows for style overrides of the Button's inner container. */ innerStyle: _propTypes["default"].shape({}), /** * A button can be formatted to appear on dark backgrounds better. */ inverse: _propTypes["default"].bool, /** * The onClick event handler. */ onClick: _propTypes["default"].func, /** * A button can be outlined. */ outline: _propTypes["default"].bool, /** * Set a button with a pill like form. */ pill: _propTypes["default"].bool, /** * A button can relax its padding. */ relax: _propTypes["default"].bool, /** * A button can relax its padding. */ style: _propTypes["default"].shape({}), /** * Where to display the linked URL. */ target: _propTypes["default"].oneOf(['_blank']), /** * If `true`, only the button's text is shown. */ text: _propTypes["default"].bool, /** * The title attribute. */ title: _propTypes["default"].string, /** * Set transparent styles. */ transparent: _propTypes["default"].bool, /** * The default behavior of the button. * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type */ type: _propTypes["default"].oneOf(Object.values(_models.ButtonType)), /** * The variant to use. */ variant: _propTypes["default"].oneOf(Object.values(_models.VariantType)), /** * Set a fixed width. */ width: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].string]) }; exports.propTypes = propTypes; var defaultProps = { as: _models.AsType.Button, className: undefined, classes: undefined, color: _models.ColorType.Default, compact: false, disabled: false, fullWidth: false, href: undefined, icon: false, id: undefined, innerStyle: {}, inverse: false, onClick: undefined, outline: false, pill: false, relax: false, style: {}, target: undefined, text: false, title: undefined, transparent: false, type: undefined, variant: _models.VariantType.Contained, width: undefined }; var styles = function styles(theme) { var palette = theme.palette, shape = theme.shape, typography = theme.typography; var buttonSize = 32; var buttonPillSize = 46; var outlinedWidth = 'inset 0 0 0 1px'; return { contained: {}, root: { backgroundColor: 'transparent', border: 0, borderRadius: shape.borderRadius, color: palette.text.contrastText, cursor: 'pointer', display: 'inline-block', fontSize: typography.fontSize, fontWeight: typography.fontWeightMedium, letterSpacing: '1px', lineHeight: 1, margin: '0 11px 0 0', minHeight: buttonSize, outline: 'none', overflow: 'hidden', padding: '0 22px 1px', textAlign: 'center', textDecoration: 'none', textTransform: 'capitalize', transition: 'background-color 125ms linear, color 125ms linear, opacity 250ms ease-out', verticalAlign: 'top', whiteSpace: 'normal', width: function width(props) { return props.width || ''; }, '&:focus': { boxShadow: "0 0 0 1px ".concat(theme.palette.active.main) }, '&:last-child': { marginRight: 0 }, '& .ui.icon:only-child': { margin: 0 }, '&$icon': { height: buttonSize, padding: 0, width: buttonSize }, '& .icon .icon-use-path': { fill: theme.palette.background.primary } }, innerContainer: { alignItems: 'center', display: 'flex', justifyContent: 'center', minHeight: buttonSize, '& .ui.icon': { outline: 'none', pointerEvents: 'none' }, '& span + .ui.icon': { margin: '0 0 0 11px' }, '&$icon': { alignItems: 'center', display: 'inline-flex', height: buttonSize, justifyContent: 'center', width: buttonSize, '& .ui.icon': { "float": 'none', fontSize: 16 } } }, colorActive: { '&:not($text)': { backgroundColor: theme.palette.active.main, '&$outlined': { boxShadow: "".concat(outlinedWidth, " ").concat(theme.palette.active.main), '& .icon .icon-use-path': { fill: theme.palette.active.main } } }, '&$text': { color: theme.palette.active.main, '& .icon .icon-use-path': { fill: theme.palette.active.main } } }, colorDefault: { '&:not($text)': { backgroundColor: theme.palette.grey[600], '&$outlined': { boxShadow: "".concat(outlinedWidth, " ").concat(theme.palette.grey[600]), '& .icon .icon-use-path': { fill: theme.palette.grey[600] }, '&$inverse': { boxShadow: "".concat(outlinedWidth, " ").concat(theme.palette.common.white), '& .icon .icon-use-path': { fill: theme.palette.common.white } } } }, '&$text': { color: theme.palette.text.primary, '&:not($inverse) .icon .icon-use-path': { fill: theme.palette.text.primary }, '&$inverse .icon .icon-use-path': { fill: theme.palette.text.primary } } }, colorLink: { '&:not($text)': { backgroundColor: theme.palette.text.link, '&$outlined': { boxShadow: "".concat(outlinedWidth, " ").concat(theme.palette.text.link), '& .icon .icon-use-path': { fill: theme.palette.text.link } } }, '&$text': { color: theme.palette.text.link, '& .icon .icon-use-path': { fill: theme.palette.text.link } } }, colorPrimary: { '&:not($text)': { backgroundColor: theme.palette.primary.main, '&$outlined': { boxShadow: "".concat(outlinedWidth, " ").concat(theme.palette.primary.main), '& .icon .icon-use-path': { fill: theme.palette.primary.main } } }, '&$text': { color: theme.palette.primary.main, '& .icon .icon-use-path': { fill: theme.palette.primary.main } } }, colorError: { '&:not($text)': { backgroundColor: theme.palette.error.main, '&$outlined': { boxShadow: "".concat(outlinedWidth, " ").concat(theme.palette.error.main), '& .icon .icon-use-path': { fill: theme.palette.error.main } } }, '&$text': { color: theme.palette.error.main, '& .icon .icon-use-path': { fill: theme.palette.error.main } } }, colorSecondary: { '&:not($text)': { backgroundColor: theme.palette.secondary.main, '&$outlined': { boxShadow: "".concat(outlinedWidth, " ").concat(theme.palette.secondary.main), '& .icon .icon-use-path': { fill: theme.palette.secondary.main }, '&$inverse': { boxShadow: "".concat(outlinedWidth, " ").concat(theme.palette.common.white), '& .icon .icon-use-path': { fill: theme.palette.common.white } } } }, '&$text': { color: theme.palette.secondary.main, '&:not($inverse) .icon .icon-use-path': { fill: theme.palette.secondary.main }, '&$inverse .icon .icon-use-path': { fill: theme.palette.text.inverseText } } }, colorSuccess: { '&:not($text)': { backgroundColor: theme.palette.success.main, '&$outlined': { boxShadow: "".concat(outlinedWidth, " ").concat(theme.palette.success.main), '& .icon .icon-use-path': { fill: theme.palette.success.main } } }, '&$text': { color: theme.palette.success.main, '& .icon .icon-use-path': { fill: theme.palette.success.main } } }, colorWarning: { '&:not($text)': { backgroundColor: theme.palette.warning.main, '&$outlined': { boxShadow: "".concat(outlinedWidth, " ").concat(theme.palette.warning.main), '& .icon .icon-use-path': { fill: theme.palette.warning.main } } }, '&$text': { color: theme.palette.warning.main, '& .icon .icon-use-path': { fill: theme.palette.warning.main } } }, compact: { paddingLeft: 11, paddingRight: 11 }, fullWidth: { width: '100%' }, icon: {}, fixedWidth: { paddingLeft: 0, paddingRight: 0 }, outlined: { '&:not($text)': { backgroundColor: 'transparent', color: theme.palette.text.primary } }, inverse: { '&:not($text)': { color: theme.palette.text.contrastText } }, pill: { borderRadius: buttonPillSize / 2, fontWeight: theme.typography.fontWeightBold, minHeight: buttonPillSize }, relax: { paddingLeft: 33, paddingRight: 33 }, text: { '& .icon .icon-use-path': { fill: theme.palette.text.primary } }, transparent: { '&:not($text)': { backgroundColor: theme.palette.hexToRGBA(theme.palette.background.primary, 0.5), boxShadow: "inset 0 0 0 1px ".concat(theme.palette.border.contrastPrimary) } }, disabled: { '&:not($text)': { backgroundColor: theme.palette.action.disabled, color: theme.palette.text.constrastText }, '&$outlined': { backgroundColor: 'transparent', boxShadow: "".concat(outlinedWidth, " ").concat(theme.palette.action.disabled), '& .icon .icon-use-path': { fill: theme.palette.action.disabled } }, '&$text': { color: theme.palette.text.disable } } }; }; var Button = /*#__PURE__*/function (_React$PureComponent) { _inherits(Button, _React$PureComponent); var _super = _createSuper(Button); function Button(props) { var _this; _classCallCheck(this, Button); _this = _super.call(this, props); _this.onClick = _this.onClick.bind(_assertThisInitialized(_this)); _this.onMouseDown = _this.onMouseDown.bind(_assertThisInitialized(_this)); return _this; } _createClass(Button, [{ key: "onClick", value: function onClick(event) { var _this$props = this.props, disabled = _this$props.disabled, onClick = _this$props.onClick; if (!disabled && (0, _lodash.isFunction)(onClick)) { onClick(event); } } }, { key: "onMouseDown", value: function onMouseDown(event) { event.preventDefault(); } }, { key: "render", value: function render() { var _ClassNames; var _this$props2 = this.props, as = _this$props2.as, children = _this$props2.children, classes = _this$props2.classes, className = _this$props2.className, color = _this$props2.color, compact = _this$props2.compact, disabled = _this$props2.disabled, fullWidth = _this$props2.fullWidth, href = _this$props2.href, icon = _this$props2.icon, id = _this$props2.id, innerStyle = _this$props2.innerStyle, inverse = _this$props2.inverse, outline = _this$props2.outline, pill = _this$props2.pill, relax = _this$props2.relax, style = _this$props2.style, text = _this$props2.text, target = _this$props2.target, title = _this$props2.title, transparent = _this$props2.transparent, type = _this$props2.type, variant = _this$props2.variant, width = _this$props2.width, otherProps = _objectWithoutProperties(_this$props2, ["as", "children", "classes", "className", "color", "compact", "disabled", "fullWidth", "href", "icon", "id", "innerStyle", "inverse", "outline", "pill", "relax", "style", "text", "target", "title", "transparent", "type", "variant", "width"]); var ElementType = _utils["default"].getElementType(as, this.props); var rootClasses = (0, _classnames["default"])(_constants.UI_CLASS_NAME, _constants.BEM_BUTTON, classes.root, className, (_ClassNames = {}, _defineProperty(_ClassNames, classes.colorActive, !disabled && color === _models.ColorType.Active), _defineProperty(_ClassNames, classes.colorDefault, !disabled && color === _models.ColorType.Default), _defineProperty(_ClassNames, classes.colorError, !disabled && color === _models.ColorType.Error), _defineProperty(_ClassNames, classes.colorLink, !disabled && color === _models.ColorType.Link), _defineProperty(_ClassNames, classes.colorPrimary, !disabled && color === _models.ColorType.Primary), _defineProperty(_ClassNames, classes.colorSecondary, !disabled && color === _models.ColorType.Secondary), _defineProperty(_ClassNames, classes.colorSuccess, !disabled && color === _models.ColorType.Success), _defineProperty(_ClassNames, classes.colorWarning, !disabled && color === _models.ColorType.Warning), _defineProperty(_ClassNames, classes.compact, compact), _defineProperty(_ClassNames, classes.disabled, disabled), _defineProperty(_ClassNames, classes.fixedWidth, !!width), _defineProperty(_ClassNames, classes.fullWidth, fullWidth), _defineProperty(_ClassNames, classes.icon, icon), _defineProperty(_ClassNames, classes.inverse, inverse), _defineProperty(_ClassNames, classes.contained, variant === _models.VariantType.Contained), _defineProperty(_ClassNames, classes.outlined, variant === _models.VariantType.Outlined), _defineProperty(_ClassNames, classes.pill, pill), _defineProperty(_ClassNames, classes.relax, relax), _defineProperty(_ClassNames, classes.text, variant === _models.VariantType.Text), _defineProperty(_ClassNames, classes.transparent, transparent), _ClassNames)); var innerContainerClasses = (0, _classnames["default"])('button-inner-container', classes.innerContainer); return /*#__PURE__*/_react["default"].createElement(ElementType // eslint-disable-next-line react/jsx-props-no-spreading , _extends({}, otherProps, { className: rootClasses, disabled: disabled, id: id, href: href, onClick: this.onClick, onMouseDown: this.onMouseDown, style: style, target: target, title: title, type: as === _models.AsType.Button ? type !== null && type !== void 0 ? type : _models.AsType.Button : null }), /*#__PURE__*/_react["default"].createElement("span", { className: innerContainerClasses, style: innerStyle }, children)); } }]); return Button; }(_react["default"].PureComponent); Button.propTypes = propTypes; Button.defaultProps = defaultProps; var _default = (0, _withStyles["default"])(styles)(Button); exports["default"] = _default;