UNPKG

@arche-mc2/arche-controls

Version:

We know that there are a ton of react UI library projects to choose from. Our hope with this one is to provide the next generation of react components that you can use to bootstrap your next project, or as a reference for building a UIKit. Read on to get

238 lines 11.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var React = require("react"); var classnames = require("classnames"); var Tooltip_1 = require("../../Display/Tooltip"); var theming_1 = require("../../../Common/theming"); var utils_1 = require("../../../Common/utils"); var typestyle_1 = require("typestyle"); var LoadingIndicator_1 = require("../../Display/LoadingIndicator"); var types_1 = require("../../../Common/utils/types"); var icons_1 = require("../../../Common/theming/icons"); var SvgIcon_1 = require("../../Display/SvgIcon"); var styles_1 = require("./styles"); var withTheme_1 = require("../../../Common/theming/withTheme"); exports.ActionIconMap = new types_1.Dictionary([]); for (var i = 0; i < icons_1.ArcheIconNames.length; i++) { var iconName = icons_1.ArcheIconNames[i]; exports.ActionIconMap.set(iconName, iconName); } ; var BaseButton = (function (_super) { tslib_1.__extends(BaseButton, _super); function BaseButton() { return _super !== null && _super.apply(this, arguments) || this; } BaseButton.prototype.render = function () { var _a = this.props, children = _a.children, className = _a.className, onClick = _a.onClick, dataFor = _a.dataFor, width = _a.width, iconPosition = _a.iconPosition, isProcessing = _a.isProcessing, type = _a.type, disabled = _a.disabled, dropDown = _a.dropDown, name = _a.name; var actionType = this.props.actionType; var iconName = null; if (actionType && exports.ActionIconMap.containsKey(actionType)) { iconName = exports.ActionIconMap.get(actionType); } else if (this.props.iconName) { iconName = this.props.iconName; } var icon = null; if (iconName) { icon = React.createElement(SvgIcon_1.default, { iconName: iconName, width: this.props.iconSize, height: this.props.iconSize, className: this.props.rotate ? 'up-rotating' : '', color: "currentcolor" }); } var tooltipProps = {}; if (dataFor) { tooltipProps = { "data-tip": "tooltip", "data-for": dataFor }; } var MainButton = (React.createElement("button", tslib_1.__assign({ type: type, name: name, disabled: disabled, onClick: onClick, className: classnames('up-btn', dropDown !== 'none' && children ? 'up-btn-drop-down' : '', styles_1.getStyles(this.props), className) }, tooltipProps), icon != null && isProcessing !== true && icon, width !== 'icon' && children, isProcessing === true && (React.createElement("div", { className: "up-loading-indicator-wrapper" }, React.createElement(LoadingIndicator_1.default, { displayMode: 'inline', isLoading: true, width: width === 'icon' ? 24 : 48, height: width === 'icon' ? 24 : 48 }))))); return MainButton; }; return BaseButton; }(React.Component)); exports.BaseButton = BaseButton; ; var UpButton = (function (_super) { tslib_1.__extends(UpButton, _super); function UpButton(props) { var _this = _super.call(this, props) || this; _this.handleClick = function (e) { if (_this.props.disabled !== true) { var returnValue_1 = _this.props.onClick(e); if (Promise.resolve(returnValue_1) === returnValue_1) { _this.setState({ isProcessing: true }, function () { returnValue_1.then(function (result) { _this.setState({ isProcessing: false }); return result; }).catch(function (errors) { _this.setState({ isProcessing: false }); return errors; }); }); } if (_this.props.dropDown !== 'none') { _this.setState({ isToggled: !_this.state.isToggled }); } } e.preventDefault(); e.stopPropagation(); }; _this.collapse = function () { if (_this.props.dropDown !== 'none') { _this.setState({ isToggled: false }); } }; _this.handleActionClick = function (action) { action.onClick(null); _this.collapse(); }; _this.isControlled = function (propName) { return _this.props[propName] !== undefined; }; _this.getValue = function (propName) { return _this.isControlled(propName) ? _this.props[propName] : _this.state[propName]; }; _this.disabled = function () { return _this.props.disabled || _this.state.isProcessing; }; _this.setReference = function (e) { _this.dropDownContainer = e; }; _this.getDropDownStyles = function () { var styles = { display: _this.getValue('isToggled') ? "block" : "none", position: 'unset', zIndex: 1000, listStyle: "none", backgroundColor: _this.props.theme.colorMap.white, color: _this.props.theme.colorMap.black1, margin: 0, paddingTop: 10, paddingBottom: 10, paddingLeft: 0, height: _this.getValue('isToggled') ? 'auto' : '0px', transition: _this.getValue('isToggled') ? "height 2s ease-in" : "height 2s ease-out", transform: _this.getValue('isToggled') ? "scaleY(1)" : "scaleY(0)", transformOrigin: "top", border: "1px solid " + _this.props.theme.colorMap[_this.props.intent], borderTop: 0, borderRadius: '0px 0px 4px 4px' }; var viewportWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0); if (_this.dropDownContainer && (_this.dropDownContainer.getBoundingClientRect().right + _this.dropDownContainer.offsetWidth + 20) >= viewportWidth) { styles.right = '10px'; } else if (_this.dropDownContainer && _this.dropDownContainer.offsetLeft <= 10) { styles.left = '10px'; } return styles; }; _this.handleClick = _this.handleClick.bind(_this); _this.state = { isToggled: _this.props.isToggled, isProcessing: _this.props.isProcessing, prevProps: props, }; return _this; } UpButton.prototype.componentWillReceiveProps = function (nextProps, prevState) { if (nextProps.disabled) { this.setState({ isToggled: false }); } }; ; UpButton.prototype.isSeparator = function (element) { return element.size !== undefined; }; UpButton.prototype.render = function () { var _this = this; var _a = this.props, name = _a.name, children = _a.children, tooltip = _a.tooltip, onClick = _a.onClick, iconName = _a.iconName, iconPosition = _a.iconPosition, disabled = _a.disabled, isProcessing = _a.isProcessing, others = tslib_1.__rest(_a, ["name", "children", "tooltip", "onClick", "iconName", "iconPosition", "disabled", "isProcessing"]); var buttonElement = typestyle_1.style({ cursor: "pointer", padding: "8px", color: this.props.theme.colorMap.grey1, $nest: { '&:hover': { color: this.props.theme.colorMap[this.props.intent] } } }); var separatorElement = typestyle_1.style({ height: "1px", margin: "9px 0", overflow: "hidden", backgroundColor: this.props.theme.colorMap.white, }); var icon = iconName; if (icon == null && this.props.dropDown != 'none') { if (this.props.dropDown == 'up') { icon = 'arrow-up'; } else if (this.props.dropDown == 'down') { icon = 'arrow-down'; } } var position = iconPosition; if (position === 'none' && this.props.dropDown != 'none') { position = 'right'; } else if (position === 'none' && icon == null) { position = 'left'; } var _tooltip = null; if (tooltip) { if (utils_1.isString(tooltip)) { _tooltip = { content: tooltip }; } else { _tooltip = tooltip; } } var handleClickProps = this.props.type === 'submit' ? {} : { onClick: this.handleClick }; var RenderButton = function (renderButtonProps) { return React.createElement("div", { style: { display: 'flex' } }, React.createElement(BaseButton, tslib_1.__assign({ name: name, iconName: icon, iconPosition: position, isToggled: _this.getValue('isToggled') }, handleClickProps, { isProcessing: _this.getValue('isProcessing'), disabled: _this.disabled() }, others, renderButtonProps), children != null && (React.createElement("span", { className: 'up-btn-label' }, children))), !children && _this.props.dropDown != 'none' && _this.getValue('isToggled') && React.createElement("span", { className: "up-btn-missing-border" })); }; return (React.createElement("div", { ref: this.setReference, className: classnames('up-btn-wrapper', styles_1.getWrapperStyles(this.props), this.props.className) }, tooltip === null ? React.createElement(RenderButton, null) : React.createElement(Tooltip_1.default, tslib_1.__assign({}, _tooltip), React.createElement(RenderButton, null)), !this.props.disabled && this.props.dropDown != 'none' && this.getValue('isToggled') && React.createElement("ul", { tabIndex: 0, className: typestyle_1.style(this.getDropDownStyles()) }, this.props.extraActions.map(function (v, i) { var isSeparator = _this.isSeparator(v); if (!isSeparator) { return React.createElement("li", { tabIndex: i + 1, key: i, className: buttonElement, onMouseDown: _this.handleActionClick.bind(_this, v) }, v.libelle); } else { return React.createElement("li", { tabIndex: i + 1, role: "separator", key: i, className: separatorElement }); } })))); }; UpButton.defaultProps = { backgroundColor: '', fontSize: 'medium', disabled: false, shadow: false, iconName: null, iconPosition: 'none', iconSize: 20, intent: 'default', width: 'auto', height: 'normal', tooltip: null, dropDown: 'none', onClick: function (e) { }, theme: theming_1.default, type: 'button', name: 'button' }; return UpButton; }(React.Component)); exports.UpButton = UpButton; exports.default = withTheme_1.default(UpButton); //# sourceMappingURL=UpButton.js.map