-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: updated design ConfirmationDialog
- Loading branch information
Showing
2 changed files
with
91 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,92 @@ | ||
import { | ||
AlertDialog, | ||
AlertDialogBody, | ||
AlertDialogContent, | ||
AlertDialogFooter, | ||
AlertDialogHeader, | ||
AlertDialogOverlay, | ||
Button | ||
Button, | ||
Input, | ||
Modal, | ||
ModalBody, | ||
ModalCloseButton, | ||
ModalContent, | ||
ModalFooter, | ||
ModalHeader, | ||
ModalOverlay, | ||
Text | ||
} from '@chakra-ui/react'; | ||
import { FC, useRef } from 'react'; | ||
import { FC, ReactNode, useEffect, useState } from 'react'; | ||
|
||
export const ConfirmationDialog: FC<{ | ||
isOpen: boolean; | ||
onClose: () => void; | ||
onConfirm: () => void; | ||
title: string; | ||
description?: string; | ||
cancelText?: string; | ||
confirmText?: string; | ||
title: ((confirmValue: string) => string) | string; | ||
confirmValue?: string; | ||
description?: ((confirmValue: string) => ReactNode) | ReactNode; | ||
cancelButtonText?: string; | ||
confirmButtonText?: string; | ||
isLoading?: boolean; | ||
}> = ({ | ||
isOpen, | ||
onClose, | ||
onConfirm, | ||
title, | ||
description, | ||
cancelText = 'Cancel', | ||
confirmText = 'Confirm' | ||
confirmValue, | ||
cancelButtonText = 'Cancel', | ||
confirmButtonText = 'Confirm', | ||
isLoading = false | ||
}) => { | ||
const cancelRef = useRef(null); | ||
const [inputValue, setInputValue] = useState(''); | ||
|
||
return ( | ||
<AlertDialog isOpen={isOpen} leastDestructiveRef={cancelRef} onClose={onClose}> | ||
<AlertDialogOverlay> | ||
<AlertDialogContent> | ||
<AlertDialogHeader fontSize="lg" fontWeight="bold"> | ||
{title} | ||
</AlertDialogHeader> | ||
useEffect(() => { | ||
if (!isOpen) { | ||
setInputValue(''); | ||
} | ||
}, [isOpen]); | ||
|
||
const titleString = typeof title === 'function' ? confirmValue && title(confirmValue) : title; | ||
const descriptionString = | ||
typeof description === 'function' ? confirmValue && description(confirmValue) : description; | ||
|
||
{description && <AlertDialogBody paddingY={0}>{description}</AlertDialogBody>} | ||
return ( | ||
<Modal isOpen={isOpen} onClose={onClose} scrollBehavior="inside" size="md"> | ||
<ModalOverlay /> | ||
<ModalContent> | ||
<ModalHeader textAlign="center">{titleString}</ModalHeader> | ||
<ModalCloseButton /> | ||
{descriptionString && ( | ||
<ModalBody pb="0"> | ||
<Text | ||
textStyle="text.body2" | ||
mb="6" | ||
color="text.secondary" | ||
textAlign="center" | ||
> | ||
{descriptionString} | ||
</Text> | ||
{confirmValue && ( | ||
<Input | ||
onChange={e => setInputValue(e.target.value)} | ||
placeholder="Name" | ||
value={inputValue} | ||
/> | ||
)} | ||
</ModalBody> | ||
)} | ||
|
||
<AlertDialogFooter> | ||
<Button ref={cancelRef} onClick={onClose} variant="outline"> | ||
{cancelText} | ||
</Button> | ||
<Button ml={3} onClick={onConfirm}> | ||
{confirmText} | ||
</Button> | ||
</AlertDialogFooter> | ||
</AlertDialogContent> | ||
</AlertDialogOverlay> | ||
</AlertDialog> | ||
<ModalFooter gap="3"> | ||
<Button flex={1} onClick={onClose} variant="secondary"> | ||
{cancelButtonText} | ||
</Button> | ||
<Button | ||
flex={1} | ||
isDisabled={!!confirmValue && inputValue !== confirmValue} | ||
isLoading={isLoading} | ||
onClick={onConfirm} | ||
type="submit" | ||
variant="primary" | ||
> | ||
{confirmButtonText} | ||
</Button> | ||
</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters