UNPKG

react-cm-ui

Version:
525 lines (472 loc) 19.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _lodash = require("lodash"); var _classnames = _interopRequireDefault(require("classnames")); var _propTypes = _interopRequireDefault(require("prop-types")); var _react = _interopRequireDefault(require("react")); var _utils = _interopRequireDefault(require("../../utils/utils")); var _withStyles = _interopRequireDefault(require("../../styles/withStyles")); 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(['a', 'button']), /** * Primary content. */ children: _propTypes["default"].node.isRequired, classes: _propTypes["default"].shape({ root: _propTypes["default"].string, inverse: _propTypes["default"].string, colorActive: _propTypes["default"].string, colorAlert: _propTypes["default"].string, colorAlternate: _propTypes["default"].string, colorDisable: _propTypes["default"].string, colorInverse: _propTypes["default"].string, colorLight: _propTypes["default"].string, colorOutline: _propTypes["default"].string, colorPrimary: _propTypes["default"].string, colorSecondary: _propTypes["default"].string, colorSuccess: _propTypes["default"].string, colorTransparent: _propTypes["default"].string, colorWarning: _propTypes["default"].string, compact: _propTypes["default"].string, fluid: _propTypes["default"].string, icon: _propTypes["default"].string, fixedWidth: _propTypes["default"].string, outlined: _propTypes["default"].string, relax: _propTypes["default"].string, innerContainer: _propTypes["default"].string }), /** * Additional classes. */ className: _propTypes["default"].string, /** * Color of the button. */ color: _propTypes["default"].oneOf(_utils["default"].colorEnums()), /** * A button can reduce its padding. */ compact: _propTypes["default"].bool, /** * Deprecated prop. Please use `disabled` instead. */ disable: _propTypes["default"].bool, /** * A button can be disabled. */ disabled: _propTypes["default"].bool, /** * The Button will be resized to its parent container's width. */ fluid: _propTypes["default"].bool, href: _propTypes["default"].string, icon: _propTypes["default"].bool, /** * Assign the button an id attribute value. */ id: _propTypes["default"].string, innerStyle: _propTypes["default"].shape({}), /** * A button can be formatted to appear on dark backgrounds better. */ inverse: _propTypes["default"].bool, onClick: _propTypes["default"].func, /** * A button can be outlined. */ outlined: _propTypes["default"].bool, /** * A button can relax its padding. */ relax: _propTypes["default"].bool, /** * A button can relax its padding. */ style: _propTypes["default"].shape({}), target: _propTypes["default"].oneOf(['_blank']), title: _propTypes["default"].string, /** * Set a fixed width. */ width: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].string]) }; var defaultProps = { as: 'button', className: undefined, classes: undefined, color: 'primary', compact: false, disable: false, disabled: false, fluid: false, href: undefined, icon: false, id: undefined, innerStyle: {}, inverse: false, onClick: undefined, outlined: false, relax: false, style: {}, target: undefined, title: undefined, width: undefined }; var styles = function styles(theme) { var palette = theme.palette, shape = theme.shape, typography = theme.typography; var buttonSize = 32; return { root: { 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.primary) }, '&:last-child': { marginRight: 0 }, '& .ui.icon:only-child': { margin: 0 }, '&$icon': { height: buttonSize, padding: 0, width: buttonSize }, '&$inverse': { '&$colorOutline, &$colorTransparent': { color: palette.text.contrastText }, '&$colorAlert': { backgroundColor: palette.error.main }, '&$colorAlternate': { backgroundColor: palette.grey[500] }, '&$colorDisable': { backgroundColor: palette["static"].main, cursor: 'default' }, '&$colorLight': { backgroundColor: palette.grey[100], color: palette.text.primary }, '&$colorOutline': { backgroundColor: 'rgba(255, 255, 255, .30)', boxShadow: "inset 0 0 0 1px ".concat(palette.background.secondary), '& > .ui.icon .icon-use-path': { fill: palette.text.primary } }, '&$colorPrimary': { backgroundColor: palette.cyan[600] }, '&$colorSuccess': { backgroundColor: palette.green[500] }, '&$colorSecondary': { backgroundColor: palette.blue[500] }, '&$colorTransparent': { backgroundColor: 'transparent' }, '&$colorWarning': { backgroundColor: palette.warning.main } }, '&$outlined': { backgroundColor: 'transparent', boxShadow: "inset 0 0 0 1px ".concat(palette.grey[500]), color: palette.text.primary, '& .ui.icon .icon-use-path': { fill: palette.text.primary }, '&$colorAlert': { backgroundColor: 'transparent', boxShadow: "inset 0 0 0 1px ".concat(palette.error.main), color: palette.error.main, '& .ui.icon .icon-use-path': { fill: palette.error.main } }, '&$colorAlternate': { backgroundColor: 'transparent', boxShadow: "inset 0 0 0 1px ".concat(palette.grey[500]), color: palette.text.primary, '& .ui.icon .icon-use-path': { fill: palette.text.primary } }, '&$colorDisable': { backgroundColor: 'transparent', boxShadow: "inset 0 0 0 1px ".concat(palette["static"].main), color: palette.text.disable, cursor: 'default', '& .ui.icon .icon-use-path': { fill: palette.text.disable } }, '&$colorLight': { backgroundColor: 'transparent', boxShadow: "inset 0 0 0 1px ".concat(palette.grey[100]), color: palette.grey[100], '& .ui.icon .icon-use-path': { fill: palette.grey[100] } }, '&$colorOutline': { backgroundColor: 'transparent', boxShadow: "inset 0 0 0 1px ".concat(palette.grey[500]), color: palette.text.primary, '& .ui.icon .icon-use-path': { fill: palette.text.primary } }, '&$colorPrimary': { backgroundColor: 'transparent', boxShadow: "inset 0 0 0 1px ".concat(palette.cyan[600]), color: palette.cyan[600], '& .ui.icon .icon-use-path': { fill: palette.cyan[600] } }, '&$colorSecondary': { backgroundColor: 'transparent', boxShadow: "inset 0 0 0 1px ".concat(palette.blue[500]), color: palette.blue[500], '& .ui.icon .icon-use-path': { fill: palette.blue[500] } }, '&$colorSuccess': { backgroundColor: 'transparent', boxShadow: "inset 0 0 0 1px ".concat(palette.green[500]), color: palette.success.main, '& .ui.icon .icon-use-path': { fill: palette.success.main } }, '&$colorWarning': { backgroundColor: 'transparent', boxShadow: "inset 0 0 0 1px ".concat(palette.warning.main), color: palette.warning.main, '& .ui.icon .icon-use-path': { fill: palette.warning.main } } } }, innerContainer: { alignItems: 'center', display: 'flex', justifyContent: 'center', minHeight: buttonSize, '& .ui.icon': { outline: 'none', pointerEvents: 'none' }, '& > .ui.icon .icon-use-path': { fill: palette.text.contrastText }, '& 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 } } }, inverse: {}, colorActive: { backgroundColor: palette.active.primary }, colorAlert: { backgroundColor: palette.error.main }, colorAlternate: { backgroundColor: palette.grey[500] }, colorDisable: { backgroundColor: palette["static"].main, cursor: 'default' }, colorInverse: {}, colorLight: { backgroundColor: palette.grey[100], color: palette.text.primary, '& .ui.icon .icon-use-path': { fill: palette.text.primary } }, colorOutline: { backgroundColor: 'transparent', boxShadow: "inset 0 0 0 1px ".concat(palette.grey[500]), color: palette.text.primary, '& .ui.icon .icon-use-path': { fill: palette.text.primary } }, colorPrimary: { backgroundColor: palette.cyan[600] }, colorSecondary: { backgroundColor: palette.blue[500] }, colorSuccess: { backgroundColor: palette.green[500] }, colorTransparent: { backgroundColor: 'transparent', color: palette.text.primary, '& .ui.icon .icon-use-path': { fill: palette.text.primary } }, colorWarning: { backgroundColor: palette.warning.main }, compact: { paddingLeft: 11, paddingRight: 11 }, fluid: { width: '100%' }, icon: {}, fixedWidth: { paddingLeft: 0, paddingRight: 0 }, outlined: {}, relax: { paddingLeft: 33, paddingRight: 33 } }; }; var ButtonDeprecated = /*#__PURE__*/function (_React$PureComponent) { _inherits(ButtonDeprecated, _React$PureComponent); var _super = _createSuper(ButtonDeprecated); function ButtonDeprecated(props) { var _this; _classCallCheck(this, ButtonDeprecated); _this = _super.call(this, props); _this.onClick = _this.onClick.bind(_assertThisInitialized(_this)); _this.onMouseDown = _this.onMouseDown.bind(_assertThisInitialized(_this)); return _this; } _createClass(ButtonDeprecated, [{ key: "onClick", value: function onClick(event) { var onClick = this.props.onClick; if ((0, _lodash.isFunction)(onClick)) { onClick(event); } } }, { key: "onMouseDown", value: function onMouseDown(event) { event.preventDefault(); } }, { key: "render", value: function render() { var _ClassNames; var _this$props = this.props, as = _this$props.as, children = _this$props.children, className = _this$props.className, classes = _this$props.classes, color = _this$props.color, compact = _this$props.compact, disableProp = _this$props.disable, disabledProp = _this$props.disabled, fluid = _this$props.fluid, href = _this$props.href, icon = _this$props.icon, id = _this$props.id, innerStyle = _this$props.innerStyle, inverse = _this$props.inverse, outlined = _this$props.outlined, relax = _this$props.relax, style = _this$props.style, target = _this$props.target, title = _this$props.title, width = _this$props.width, otherProps = _objectWithoutProperties(_this$props, ["as", "children", "className", "classes", "color", "compact", "disable", "disabled", "fluid", "href", "icon", "id", "innerStyle", "inverse", "outlined", "relax", "style", "target", "title", "width"]); var disabled = disabledProp || disableProp; var ElementType = _utils["default"].getElementType(as, this.props); var rootClasses = (0, _classnames["default"])('ui', 'button', classes.root, className, (_ClassNames = {}, _defineProperty(_ClassNames, classes.inverse, inverse), _defineProperty(_ClassNames, classes.colorActive, !disabled && color === 'active'), _defineProperty(_ClassNames, classes.colorAlert, !disabled && color === 'alert'), _defineProperty(_ClassNames, classes.colorAlternate, !disabled && color === 'alternate'), _defineProperty(_ClassNames, classes.colorDisable, disabled || color === 'disable'), _defineProperty(_ClassNames, classes.colorInverse, !disabled && color === 'inverse'), _defineProperty(_ClassNames, classes.colorLight, !disabled && color === 'light'), _defineProperty(_ClassNames, classes.colorOutline, !disabled && color === 'outline'), _defineProperty(_ClassNames, classes.colorPrimary, !disabled && (!color || color === 'primary')), _defineProperty(_ClassNames, classes.colorSecondary, !disabled && color === 'secondary'), _defineProperty(_ClassNames, classes.colorSuccess, !disabled && color === 'success'), _defineProperty(_ClassNames, classes.colorTransparent, !disabled && color === 'transparent'), _defineProperty(_ClassNames, classes.colorWarning, !disabled && color === 'warning'), _defineProperty(_ClassNames, classes.compact, compact), _defineProperty(_ClassNames, classes.fluid, fluid), _defineProperty(_ClassNames, classes.icon, icon), _defineProperty(_ClassNames, classes.fixedWidth, !!width), _defineProperty(_ClassNames, classes.outlined, outlined), _defineProperty(_ClassNames, classes.relax, relax), _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 }), /*#__PURE__*/_react["default"].createElement("span", { className: innerContainerClasses, style: innerStyle }, children)); } }]); return ButtonDeprecated; }(_react["default"].PureComponent); ButtonDeprecated.propTypes = propTypes; ButtonDeprecated.defaultProps = defaultProps; var _default = (0, _withStyles["default"])(styles)(ButtonDeprecated); exports["default"] = _default;