UNPKG

5.01 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Button = exports.ButtonType = exports.ButtonVariant = void 0;
4const tslib_1 = require("tslib");
5const React = tslib_1.__importStar(require("react"));
6const button_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Button/button"));
7const react_styles_1 = require("@patternfly/react-styles");
8const Spinner_1 = require("../Spinner");
9const helpers_1 = require("../../helpers");
10var ButtonVariant;
11(function (ButtonVariant) {
12 ButtonVariant["primary"] = "primary";
13 ButtonVariant["secondary"] = "secondary";
14 ButtonVariant["tertiary"] = "tertiary";
15 ButtonVariant["danger"] = "danger";
16 ButtonVariant["warning"] = "warning";
17 ButtonVariant["link"] = "link";
18 ButtonVariant["plain"] = "plain";
19 ButtonVariant["control"] = "control";
20})(ButtonVariant = exports.ButtonVariant || (exports.ButtonVariant = {}));
21var ButtonType;
22(function (ButtonType) {
23 ButtonType["button"] = "button";
24 ButtonType["submit"] = "submit";
25 ButtonType["reset"] = "reset";
26})(ButtonType = exports.ButtonType || (exports.ButtonType = {}));
27const ButtonBase = (_a) => {
28 var { children = null, className = '', component = 'button', isActive = false, isBlock = false, isDisabled = false, isAriaDisabled = false, isLoading = null, isDanger = false, spinnerAriaValueText, spinnerAriaLabelledBy, spinnerAriaLabel, isSmall = false, isLarge = false, inoperableEvents = ['onClick', 'onKeyPress'], isInline = false, type = ButtonType.button, variant = ButtonVariant.primary, iconPosition = 'left', 'aria-label': ariaLabel = null, icon = null, ouiaId, ouiaSafe = true, tabIndex = null, innerRef } = _a, props = tslib_1.__rest(_a, ["children", "className", "component", "isActive", "isBlock", "isDisabled", "isAriaDisabled", "isLoading", "isDanger", "spinnerAriaValueText", "spinnerAriaLabelledBy", "spinnerAriaLabel", "isSmall", "isLarge", "inoperableEvents", "isInline", "type", "variant", "iconPosition", 'aria-label', "icon", "ouiaId", "ouiaSafe", "tabIndex", "innerRef"]);
29 const ouiaProps = helpers_1.useOUIAProps(exports.Button.displayName, ouiaId, ouiaSafe, variant);
30 const Component = component;
31 const isButtonElement = Component === 'button';
32 const isInlineSpan = isInline && Component === 'span';
33 const preventedEvents = inoperableEvents.reduce((handlers, eventToPrevent) => (Object.assign(Object.assign({}, handlers), { [eventToPrevent]: (event) => {
34 event.preventDefault();
35 } })), {});
36 const getDefaultTabIdx = () => {
37 if (isDisabled) {
38 return isButtonElement ? null : -1;
39 }
40 else if (isAriaDisabled) {
41 return null;
42 }
43 else if (isInlineSpan) {
44 return 0;
45 }
46 };
47 return (React.createElement(Component, Object.assign({}, props, (isAriaDisabled ? preventedEvents : null), { "aria-disabled": isDisabled || isAriaDisabled, "aria-label": ariaLabel, className: react_styles_1.css(button_1.default.button, button_1.default.modifiers[variant], isBlock && button_1.default.modifiers.block, isDisabled && button_1.default.modifiers.disabled, isAriaDisabled && button_1.default.modifiers.ariaDisabled, isActive && button_1.default.modifiers.active, isInline && variant === ButtonVariant.link && button_1.default.modifiers.inline, isDanger && (variant === ButtonVariant.secondary || variant === ButtonVariant.link) && button_1.default.modifiers.danger, isLoading !== null && children !== null && button_1.default.modifiers.progress, isLoading && button_1.default.modifiers.inProgress, isSmall && button_1.default.modifiers.small, isLarge && button_1.default.modifiers.displayLg, className), disabled: isButtonElement ? isDisabled : null, tabIndex: tabIndex !== null ? tabIndex : getDefaultTabIdx(), type: isButtonElement || isInlineSpan ? type : null, role: isInlineSpan ? 'button' : null, ref: innerRef }, ouiaProps),
48 isLoading && (React.createElement("span", { className: react_styles_1.css(button_1.default.buttonProgress) },
49 React.createElement(Spinner_1.Spinner, { size: Spinner_1.spinnerSize.md, "aria-valuetext": spinnerAriaValueText, "aria-label": spinnerAriaLabel, "aria-labelledby": spinnerAriaLabelledBy }))),
50 variant === ButtonVariant.plain && children === null && icon ? icon : null,
51 variant !== ButtonVariant.plain && icon && iconPosition === 'left' && (React.createElement("span", { className: react_styles_1.css(button_1.default.buttonIcon, button_1.default.modifiers.start) }, icon)),
52 children,
53 variant !== ButtonVariant.plain && icon && iconPosition === 'right' && (React.createElement("span", { className: react_styles_1.css(button_1.default.buttonIcon, button_1.default.modifiers.end) }, icon))));
54};
55exports.Button = React.forwardRef((props, ref) => (React.createElement(ButtonBase, Object.assign({ innerRef: ref }, props))));
56exports.Button.displayName = 'Button';
57//# sourceMappingURL=Button.js.map
\No newline at end of file