UNPKG

@awsui/components-react

Version:

AWS UI is a collection of [React](https://reactjs.org/) components that help create intuitive, responsive, and accessible user experiences for web applications. It is developed by Amazon Web Services (AWS). This work is available under the terms of the [A

41 lines (40 loc) 3.35 kB
import { __assign, __rest } from "tslib"; import clsx from 'clsx'; import React, { useCallback, useRef } from 'react'; import { fireCancelableEvent } from '../internal/events'; import useFocusVisible from '../internal/hooks/focus-visible'; import useForwardFocus from '../internal/hooks/forward-focus'; import styles from './styles.css.js'; import { LeftIcon, RightIcon } from './icon-helper'; export var InternalButton = React.forwardRef(function (_a, ref) { var _b; var children = _a.children, iconName = _a.iconName, iconClass = _a.iconClass, onClick = _a.onClick, _c = _a.iconAlign, iconAlign = _c === void 0 ? 'left' : _c, iconUrl = _a.iconUrl, iconAlt = _a.iconAlt, _d = _a.variant, variant = _d === void 0 ? 'normal' : _d, _e = _a.loading, loading = _e === void 0 ? false : _e, _f = _a.disabled, disabled = _f === void 0 ? false : _f, _g = _a.wrapText, wrapText = _g === void 0 ? true : _g, href = _a.href, target = _a.target, formAction = _a.formAction, ariaLabel = _a.ariaLabel, nativeAttributes = _a.nativeAttributes, props = __rest(_a, ["children", "iconName", "iconClass", "onClick", "iconAlign", "iconUrl", "iconAlt", "variant", "loading", "disabled", "wrapText", "href", "target", "formAction", "ariaLabel", "nativeAttributes"]); var focusVisible = useFocusVisible(); var isAnchor = Boolean(href); var isDisabled = loading || disabled; var shouldHaveContent = children && variant !== 'icon' && variant !== 'flashbar-icon'; var buttonRef = useRef(null); useForwardFocus(ref, buttonRef); var handleClick = useCallback(function (event) { if (isAnchor && isDisabled) { return event.preventDefault(); } var altKey = event.altKey, button = event.button, ctrlKey = event.ctrlKey, metaKey = event.metaKey, shiftKey = event.shiftKey; fireCancelableEvent(onClick, { altKey: altKey, button: button, ctrlKey: ctrlKey, metaKey: metaKey, shiftKey: shiftKey }, event); }, [isAnchor, isDisabled, onClick]); var buttonClass = clsx(props.className, styles.button, styles["variant-" + variant], (_b = {}, _b[styles.disabled] = isDisabled, _b[styles['button-no-wrap']] = !wrapText, _b[styles['button-no-text']] = !shouldHaveContent, _b)); var buttonProps = __assign(__assign(__assign(__assign({}, props), focusVisible), nativeAttributes), { ref: buttonRef, 'aria-label': ariaLabel, className: buttonClass, onClick: handleClick }); var iconProps = { loading: loading, iconName: iconName, iconAlign: iconAlign, iconUrl: iconUrl, iconAlt: iconAlt, variant: variant, iconClass: iconClass }; var buttonContent = (React.createElement(React.Fragment, null, React.createElement(LeftIcon, __assign({}, iconProps)), shouldHaveContent && React.createElement("span", { className: styles.content }, children), React.createElement(RightIcon, __assign({}, iconProps)))); if (isAnchor) { return (React.createElement("a", __assign({}, buttonProps, { href: href, target: target, rel: target === '_blank' ? 'noopener' : undefined, tabIndex: isDisabled ? -1 : undefined, "aria-disabled": isDisabled ? true : undefined }), buttonContent)); } return (React.createElement("button", __assign({}, buttonProps, { type: formAction === 'none' ? 'button' : 'submit', disabled: isDisabled }), buttonContent)); });