-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathTriggeredDialog.tsx
64 lines (61 loc) · 1.75 KB
/
TriggeredDialog.tsx
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
57
58
59
60
61
62
63
64
import React, { ReactNode, useState } from "react";
import { CustomModal } from "./CustomModal";
export const TriggeredDialogContext = React.createContext<{
changeContent: (Content: ReactNode, showBack: boolean, title: ReactNode) => void;
setModalVisible: (visible: boolean) => void;
}>(undefined as any);
/**
* a modal triggered by the `trigger` ReactNode.
* @remarks
* this modal allows one-level sub-modal
*/
export function TriggeredDialog(props: {
trigger: ReactNode;
initialTitle?: ReactNode;
children?: ReactNode;
}) {
const [visible, setVisible] = useState(false);
const [showBackLink, setShowBackLink] = useState(false);
const [modalTitle, setModalTitle] = useState(props.initialTitle);
const [modalContent, setModalContent] = useState(props.children);
const changeContent = (Content: ReactNode, showBack: boolean, title: ReactNode) => {
setModalContent(Content);
setShowBackLink(showBack);
setModalTitle(title);
};
return (
<>
{props.trigger && (
<div
onClick={() => {
setVisible(true);
}}
>
{props.trigger}
</div>
)}
<CustomModal
open={visible}
title={modalTitle}
destroyOnClose
onCancel={() => setVisible(false)}
showOkButton={false}
showCancelButton={false}
width="440px"
showBackLink={showBackLink}
onBack={() => {
changeContent(props.children, false, props.initialTitle);
}}
>
<TriggeredDialogContext.Provider
value={{
changeContent: changeContent,
setModalVisible: setVisible,
}}
>
{modalContent}
</TriggeredDialogContext.Provider>
</CustomModal>
</>
);
}