hooks to control the opening and closing of the dialog.
import React from "react";
import { useDialog } from "~/hooks"
import { Dialog, DialogContent, DialogActions } from "@mui/material"
const Component = () => {
const dialog = useDialog<{ content: string }>()
const executeSomething = async () => {
/** pending until onOk or onCancel is executed */
const result = await dialog.showDialog()
if (result.success) {
/** fired onOk */
} else {
/** fired onCancel */
}
}
return (
<>
<button onClick={executeSomething}>open</button>
<Dialog open={dialog.open}>
<DialogContent>
{/** something */}
</DialogContent>
<DialogActions>
<Button onClick={dialog.onCancel}>cancel</Button>
<Button onClick={dialog.onOk}>ok</Button>
</DialogActions>
</Dialog>
</>
)
}
import React from "react";
import { useDialog } from "~/hooks"
import { Dialog, DialogContent, DialogActions } from "@mui/material"
const Component = () => {
const dialog = useDialog<{ content: string }>()
const executeSomething = async () => {
const result = await dialog.showDialog({ content: "hello dialog" })
if (result.success) {
//
} else {
//
}
}
return (
<>
<button onClick={executeSomething}>open</button>
<Dialog open={dialog.open}>
<DialogContent>
{dialog.open && (
<p>{dialog.props.content}</p>
)}
</DialogContent>
<DialogActions>
<Button onClick={dialog.onCancel}>cancel</Button>
<Button onClick={dialog.onOk}>ok</Button>
</DialogActions>
</Dialog>
</>
)
}
import React from "react";
import { useDialog } from "~/hooks"
import { Dialog, DialogContent, DialogActions } from "@mui/material"
const Component = () => {
const dialog = useDialog<{ content: string }, { message: string }>()
const executeSomething = async () => {
const result = await dialog.showDialog({ content: "hello dialog" })
if (result.success) {
// value is dialog output
const value = result.value
} else {
//
}
}
return (
<>
<button onClick={executeSomething}>open</button>
<UserInputDialog
open={dialog.open}
onCancel={dialog.onCancel}
onSubmit={dialog.onOk}
/>
</>
)
}
type UserInputDialogProps = {
open?: boolean
onCancel?: () => void
onSubmit?: (output: { message: string }) => void
}
const UserInputDialog = (props) => {
/** something */
}
``