-
Notifications
You must be signed in to change notification settings - Fork 0
/
Preview.jsx
132 lines (118 loc) · 4.16 KB
/
Preview.jsx
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import React from 'react';
import { DialogProvider, useDialog } from './Dialog';
/**
* Estrutura de contexto
*/
const TestContext = React.createContext("Null Context");
const TestProvider = ({ children, value }) => <TestContext.Provider value={value}>{children}</TestContext.Provider>;
const useTest = () => React.useContext(TestContext);
/**
* Dialogo livre com exemplo de aninhamento de dialogo
*/
function Custom({ modal = 0 }) {
const { setDialog } = useDialog();
const handleClick = () => {
// ...
setDialog(<Custom modal={++modal} />, { maxWidth: "sm", fullWidth: true, onClose: () => setDialog(false) });
};
return (
<div style={{ padding: "30px" }}>
<h2>My Custom title {modal}</h2>
<div>
<p>My custom content</p>
<input type="text" />
</div>
<br />
<br />
<div>
<button onClick={handleClick}>Nested</button>
</div>
</div>
)
};
/**
* Esse componente carrega o contexto de teste junto com o dialogo, para mostrar a estrutura de carregamento de contexto e como o dialogo afeta ela.
*/
function ContextExample() {
const { setDialog } = useDialog();
const testContextValue = useTest();
return (
<div style={{ padding: "100px" }}>
<h3>Context Test</h3>
<br />
<button onClick={() => { setDialog(<div style={{ padding: "100px" }}>{testContextValue}</div>, { fullWidth: true, maxWidth: "xs", onClose: () => setDialog(false) }) }}>
Show Context Value
</button>
</div>
);
}
/**
* Esse componente carrega o Dialogo e apresenta um botão para abrir o componente `ContextExample` no dialogo.
*/
function ButtonThatLoadsDialogWithContextExample({ children }){
const { setDialog } = useDialog();
return (
<button onClick={() => { setDialog(<ContextExample />, { fullWidth: true, maxWidth: "sm", onClose: () => setDialog(false) }) }}>
{children}
</button>
)
};
function Preview() {
const { setAlert, setConfirm, setDialog } = useDialog();
React.useEffect(() => {
if(setAlert){
console.log(setAlert);
setAlert("Render Test!");
}
}, [setAlert]);
return (
<div style={{ padding: "100px" }}>
<h1>Dialog Utils</h1>
<button onClick={() => { setAlert("Hello World!", { fullWidth: true, maxWidth: "xs" }).finally(() => console.log("close triggered")) }}>
Alert
</button>
<br />
<hr />
<button onClick={() => { setConfirm("Hello World!", "Confirm Dialog", { fullWidth: true, maxWidth: "xs" }).then(result => console.log("Triggered " + result)) }}>
Confirm
</button>
<br />
<hr />
<button onClick={() => { setDialog(<Custom />, { fullWidth: true, maxWidth: "md", onClose: () => setDialog(false) }) }}>
Dialog
</button>
<br />
<hr />
{
/**
* Tenha em mente que se o conteúdo dentro do seu dialogo for carregar um contexto, o seu provedor de dialogo tem que estar inserido dentro do contexto.
*/
}
<TestProvider value="Filled Context!">
<ButtonThatLoadsDialogWithContextExample>
Context without provider
</ButtonThatLoadsDialogWithContextExample>
<br />
<hr />
<DialogProvider globalDialogProps={{
sx: {
"& .MuiDialog-container": {
alignItems: "baseline"
}
}
}}>
<ButtonThatLoadsDialogWithContextExample>
Context with provider
</ButtonThatLoadsDialogWithContextExample>
</DialogProvider>
</TestProvider>
</div>
)
}
export default function PreviewProvider() {
return (
<DialogProvider>
<Preview />
</DialogProvider>
);
}