-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #653 from NBISweden/dev/confirmation-dialogue
Confirmation Dialog
- Loading branch information
Showing
5 changed files
with
255 additions
and
50 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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React, { | ||
createContext, | ||
useState, | ||
useRef, | ||
useCallback, | ||
useContext, | ||
} from "react"; | ||
import { ModalDialog } from "./ModalDialog"; | ||
|
||
const ConfirmDialog = createContext(null); | ||
|
||
export const ConfirmDialogProvider = ({ children }) => { | ||
const [state, setState] = useState<{}>({ isOpen: false }); | ||
const handlerFunction = useRef<(choice: boolean) => void>(); | ||
|
||
const confirm = useCallback( | ||
(forwarded) => { | ||
return new Promise((resolve) => { | ||
setState({ ...forwarded, isOpen: true }); | ||
handlerFunction.current = (choice: boolean) => { | ||
resolve(choice); | ||
setState({ isOpen: false }); | ||
}; | ||
}); | ||
}, | ||
[setState] | ||
); | ||
|
||
return ( | ||
<ConfirmDialog.Provider value={confirm}> | ||
{children} | ||
<ModalDialog | ||
{...state} | ||
onCancel={() => handlerFunction.current(false)} | ||
onConfirm={() => handlerFunction.current(true)} | ||
/> | ||
</ConfirmDialog.Provider> | ||
); | ||
}; | ||
|
||
export const useConfirm = () => { | ||
return useContext(ConfirmDialog); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React, { useEffect, useRef } from "react"; | ||
import { useEscaper } from "../utils"; | ||
|
||
/* | ||
The modal component can be filled with a simple string using the content prop. | ||
In that case, the component should be rendered without children. | ||
To fill it with more complex JSX, it can be rendered with children. | ||
If children are present, the content prop will be ignored. | ||
*/ | ||
|
||
export const ModalDialog = ({ | ||
isOpen, | ||
title, | ||
content, | ||
confirmButtonLabel, | ||
onCancel, | ||
onConfirm, | ||
children, | ||
}: { | ||
isOpen: boolean; | ||
title: string; | ||
content?: string; | ||
confirmButtonLabel: string; | ||
onCancel: () => void; | ||
onConfirm: () => void; | ||
children: JSX.Element; | ||
}) => { | ||
// Autofocus on cancel button when opening dialog | ||
const cancelButton = useRef(null); | ||
useEffect(() => { | ||
cancelButton.current.focus(); | ||
}); | ||
|
||
// Close the dialog when pressing escape | ||
const modal = useRef(null); | ||
useEscaper(modal, () => onCancel()); | ||
|
||
return ( | ||
<section | ||
className="modal-overlay" | ||
style={{ display: `${isOpen ? "block" : "none"}` }} | ||
ref={modal} | ||
> | ||
<div | ||
className="modal-wrapper" | ||
role="alertdialog" | ||
aria-modal={true} | ||
aria-labelledby="modalTitle" | ||
aria-describedby="modalContent" | ||
> | ||
<section className="modal-title-wrapper"> | ||
<h2 id="modalTitle">{title}</h2> | ||
</section> | ||
<section className="modal-content-wrapper"> | ||
{!children && <p id="modalContent">{content}</p>} | ||
{children} | ||
</section> | ||
<section className="modal-buttons-wrapper"> | ||
<button | ||
ref={cancelButton} | ||
onClick={onCancel} | ||
className="basic-button modal-cancel-button" | ||
> | ||
Cancel | ||
</button> | ||
<button | ||
onClick={onConfirm} | ||
className="basic-button modal-confirm-button" | ||
> | ||
{confirmButtonLabel} | ||
</button> | ||
</section> | ||
</div> | ||
</section> | ||
); | ||
}; |
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
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