UNPKG

aws-northstar

Version:
122 lines (118 loc) 6.36 kB
/** ******************************************************************************************************************* 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 { makeStyles } from '@material-ui/core/styles'; import CircularProgress from '@material-ui/core/CircularProgress'; import Typography from '@material-ui/core/Typography'; import LinearProgress from '@material-ui/core/LinearProgress'; import Box from '../../layouts/Box'; import Grid from '../../layouts/Grid'; import StatusIndicator from '../StatusIndicator'; import Button from '../Button'; import Heading from '../Heading'; import Text from '../Text'; const useStyles = makeStyles((theme) => ({ colorPrimary: { marginTop: '8px', }, circularColorPrimary: { color: theme.palette.info.dark, position: 'absolute', left: 0, }, circularColorBottom: { color: theme.palette.grey[theme.palette.type === 'light' ? 200 : 700], }, barColorPrimary: { backgroundColor: theme.palette.info.dark, }, label: { color: theme.palette.text.primary, }, description: { padding: theme.spacing(0.3, 0), color: theme.palette.text.secondary, }, body: { display: 'inline-flex', }, icon: { paddingTop: '4px', }, resultButton: { marginLeft: '10px', }, })); const statusMapping = { error: 'negative', success: 'positive', }; const LinearProgressComponent = (_a) => { var { value, displayValue } = _a, props = __rest(_a, ["value", "displayValue"]); const classes = useStyles(); return (React.createElement(Grid, { container: true, spacing: 3 }, React.createElement(Grid, { item: true, xs: value && displayValue ? 11 : 12 }, React.createElement(LinearProgress, Object.assign({ variant: "determinate", value: value, classes: { colorPrimary: classes.colorPrimary, barColorPrimary: classes.barColorPrimary, } }, props))), displayValue && value && (React.createElement(Grid, { item: true, xs: 1 }, React.createElement(Text, null, value, "%"))))); }; const CircularProgressWithLabel = (_a) => { var { value, displayValue } = _a, props = __rest(_a, ["value", "displayValue"]); const classes = useStyles(); return (React.createElement(Box, { position: "relative", display: "inline-flex" }, React.createElement(CircularProgress, Object.assign({ value: 100, variant: "determinate", classes: { colorPrimary: classes.circularColorBottom } }, props)), React.createElement(CircularProgress, Object.assign({ variant: "determinate", value: value, classes: { colorPrimary: classes.circularColorPrimary } }, props)), displayValue && (React.createElement(Box, { display: "flex", top: 0, bottom: 0, right: 0, left: 0, position: "absolute", alignItems: "center", justifyContent: "center" }, React.createElement(Typography, { variant: "caption", component: "div", color: "textSecondary" }, `${Math.round(value)}%`))))); }; /** * A progress bar is a horizontal progress-bar for indicating progress and activity. */ const ProgressBar = (_a) => { var { value, displayValue = true, status = 'in-progress', variant = 'linear', label, description, additionalInfo, resultText, resultButtonText, resultButtonClick } = _a, props = __rest(_a, ["value", "displayValue", "status", "variant", "label", "description", "additionalInfo", "resultText", "resultButtonText", "resultButtonClick"]); const classes = useStyles(); const progressBody = (ProgressBarComponent) => { if (status === 'error' || status === 'success') { return (React.createElement("div", { className: classes.body }, React.createElement("div", { className: classes.icon }, React.createElement(StatusIndicator, { statusType: statusMapping[status] }, resultText)), resultButtonText && (React.createElement("div", { className: classes.resultButton }, React.createElement(Button, { onClick: resultButtonClick }, resultButtonText))))); } return (React.createElement(ProgressBarComponent, Object.assign({ value: value || (value === 0 ? 0 : 100), displayValue: displayValue }, props, { "data-testid": `${testId}-indicator` }))); }; const ProgressBarComponent = useMemo(() => (variant === 'linear' ? LinearProgressComponent : CircularProgressWithLabel), [variant]); const testId = props['data-testid'] || 'progress-bar'; return (React.createElement("div", { "data-testid": testId }, label && React.createElement(Heading, { variant: "h3" }, label), description && (React.createElement(Typography, { className: classes.description, variant: "body1" }, description)), progressBody(ProgressBarComponent), additionalInfo && (React.createElement(Typography, { className: classes.description, variant: "body1" }, additionalInfo)))); }; export default ProgressBar;