All files / src/components/dialogs ConfirmDialog.tsx

100% Statements 10/10
100% Branches 10/10
100% Functions 4/4
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 571x                   1x                           1x 8x     1x 4x 4x   4x                                                
import {
  Button,
  Dialog,
  DialogActions,
  DialogContent,
  DialogContentText,
  DialogTitle,
  Grow,
  GrowProps,
} from '@mui/material';
import React from 'react';
 
export type ConfirmDialogProps = {
  open: boolean;
  title: string;
  message: string;
  affirmativeText?: string;
  negativeText?: string;
  affirmativeAction: { (): void };
  negativeAction: { (): void };
  bottomContent?: React.ReactNode;
  danger?: boolean;
};
 
const Transition = React.forwardRef(function _(props: GrowProps & { children: React.ReactElement }, ref) {
  return <Grow ref={ref} timeout={500} {...props} />;
});
 
export const ConfirmDialog = React.memo(function _(props: ConfirmDialogProps) {
  const handleAffirmative = React.useCallback(() => props.affirmativeAction && props.affirmativeAction(), [props]);
  const handleNegative = React.useCallback(() => props.negativeAction && props.negativeAction(), [props]);
 
  return (
    <Dialog open={props.open} TransitionComponent={Transition} onClose={handleNegative} keepMounted>
      <DialogTitle data-testid="ConfirmDialog__Title">{props.title}</DialogTitle>
      <DialogContent data-testid="ConfirmDialog__Message">
        <DialogContentText sx={{ whiteSpace: 'pre-wrap' }}>{props.message}</DialogContentText>
      </DialogContent>
      <DialogActions>
        <Button color="inherit" variant="outlined" onClick={handleNegative} data-testid="ConfirmDialog__Cancel">
          {props.negativeText ?? 'Cancel'}
        </Button>
        <Button
          color={props.danger ? 'error' : 'info'}
          variant="contained"
          onClick={handleAffirmative}
          autoFocus
          data-testid="ConfirmDialog__OK"
        >
          {props.affirmativeText ?? 'OK'}
        </Button>
      </DialogActions>
      {props.bottomContent}
    </Dialog>
  );
});