/** ******************************************************************************************************************* 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. * ******************************************************************************************************************** */ import React, { FunctionComponent, useMemo } from 'react'; import { makeStyles } from '@material-ui/core/styles'; import CircularProgress, { CircularProgressProps } from '@material-ui/core/CircularProgress'; import Typography from '@material-ui/core/Typography'; import LinearProgress, { LinearProgressProps } 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', }, })); export interface ProgressBarProps { /** Percentage value of the progress */ value?: number; /** * Use "in-progress" to display a progress bar. * "success" and "error" are result states and replace the progress element with a status indicator, * resultText and, if set, with a button containing resultButtonText. * */ status?: 'in-progress' | 'success' | 'error'; /** * Text for the button that gets displayed when status is set to "error" or "success". * If resultButtonText is empty, no button will be displayed. */ resultButtonText?: string; /** Short information summarizing the operation */ label?: string; /** More detailed information about the operation, rendered between the progress bar and label. */ description?: string; /** Information that is displayed below the progress bar. */ additionalInfo?: string; /** Content that is displayed whenever status is set to "error" or "success". */ resultText?: string; /** Indicate whether to display value on the right end */ displayValue?: boolean; /** Fired when the user triggers the result state button. */ resultButtonClick?: () => void; /** Indicate what type of progress bar to render */ variant?: 'linear' | 'circular'; } interface ProgressBarComponentProps { value: number; displayValue: boolean; } const statusMapping: { [key in 'error' | 'success']: 'negative' | 'positive' } = { error: 'negative', success: 'positive', }; const LinearProgressComponent: React.FunctionComponent< ProgressBarComponentProps & Omit > = ({ value, displayValue, ...props }) => { const classes = useStyles(); return ( {displayValue && value && ( {value}% )} ); }; const CircularProgressWithLabel: React.FunctionComponent< ProgressBarComponentProps & Omit > = ({ value, displayValue, ...props }) => { const classes = useStyles(); return ( {displayValue && ( {`${Math.round( value )}%`} )} ); }; /** * A progress bar is a horizontal progress-bar for indicating progress and activity. */ const ProgressBar: FunctionComponent< ProgressBarProps & Omit > = ({ value, displayValue = true, status = 'in-progress', variant = 'linear', label, description, additionalInfo, resultText, resultButtonText, resultButtonClick, ...props }) => { const classes = useStyles(); const progressBody = ( ProgressBarComponent: React.FunctionComponent ): React.ReactNode => { if (status === 'error' || status === 'success') { return (
{resultText}
{resultButtonText && (
)}
); } return ( ); }; const ProgressBarComponent = useMemo( () => (variant === 'linear' ? LinearProgressComponent : CircularProgressWithLabel), [variant] ); const testId = props['data-testid'] || 'progress-bar'; return (
{label && {label}} {description && ( {description} )} {progressBody(ProgressBarComponent)} {additionalInfo && ( {additionalInfo} )}
); }; export default ProgressBar;