aws-northstar
Version:
NorthStar Design System
88 lines (84 loc) • 4.19 kB
JavaScript
/** *******************************************************************************************************************
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. *
******************************************************************************************************************** */
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import React, { useMemo } from 'react';
import MaterialButton from '@material-ui/core/Button';
import { makeStyles } from '@material-ui/core/styles';
import IconButton from '@material-ui/core/IconButton';
import CircularProgress from '@material-ui/core/CircularProgress';
import ButtonIcon from './components/ButtonIcon';
import Box from '../../layouts/Box';
const useStyles = makeStyles({
iconButton: {
padding: '4px 12px',
},
loadingIcon: {
paddingRight: '2px',
},
});
const OffsetCircularProgress = () => (React.createElement(Box, { pr: 0.6 },
React.createElement(CircularProgress, { size: 14 })));
const muiButtonProps = ({ loading, disabled, onClick, variant, href, icon, iconAlign, label, type = 'button', size = 'medium', }) => {
const getVariantProps = () => {
switch (variant) {
case 'link':
return { href };
case 'primary':
return { variant: 'contained', color: 'primary' };
case 'normal':
return { variant: 'text' };
}
return {};
};
return Object.assign(Object.assign(Object.assign({ disabled,
onClick,
type,
size, 'aria-label': label }, (icon && { [iconAlign === 'right' ? 'endIcon' : 'startIcon']: React.createElement(ButtonIcon, { type: icon }) })), (loading && { startIcon: React.createElement(OffsetCircularProgress, null) })), getVariantProps());
};
/**
* A button allows users to trigger actions on the interface.
*/
const Button = (_a) => {
var { variant = 'normal', href = '', iconAlign = 'left', onClick, icon, loading, label, disabled, children, type = 'button', size = 'medium' } = _a, props = __rest(_a, ["variant", "href", "iconAlign", "onClick", "icon", "loading", "label", "disabled", "children", "type", "size"]);
const styles = useStyles({});
const isDisabled = useMemo(() => disabled || loading, [disabled, loading]);
switch (variant) {
case 'icon':
return (React.createElement(IconButton, Object.assign({}, props, { "aria-label": label, disabled: isDisabled, onClick: onClick, className: styles.iconButton }), loading ? (React.createElement(CircularProgress, { size: 14, className: styles.loadingIcon })) : (React.createElement(ButtonIcon, { type: icon }))));
default:
return (React.createElement(MaterialButton, Object.assign({}, props, muiButtonProps({
disabled: isDisabled,
onClick,
variant,
href,
loading,
icon,
iconAlign,
label,
type,
size,
})), children));
}
};
export default Button;
export { ButtonIcon };