Skip to content

Commit

Permalink
Implement notifications/dialogs RFCs (#3584)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Potoms <[email protected]>
Co-authored-by: Pedro Ferreira <[email protected]>
  • Loading branch information
Janpot and apedroferreira authored Jun 19, 2024
1 parent 7c05759 commit a8fa780
Show file tree
Hide file tree
Showing 75 changed files with 4,161 additions and 17 deletions.
28 changes: 28 additions & 0 deletions docs/data/toolpad/core/components/use-dialogs/AlertDialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from 'react';
import { DialogsProvider, useDialogs } from '@toolpad/core/useDialogs';
import Button from '@mui/material/Button';

function DemoContent() {
const dialogs = useDialogs();
return (
<div>
<Button
onClick={async () => {
// preview-start
await dialogs.alert('Hello World');
// preview-end
}}
>
Alert
</Button>
</div>
);
}

export default function AlertDialog() {
return (
<DialogsProvider>
<DemoContent />
</DialogsProvider>
);
}
28 changes: 28 additions & 0 deletions docs/data/toolpad/core/components/use-dialogs/AlertDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from 'react';
import { DialogsProvider, useDialogs } from '@toolpad/core/useDialogs';
import Button from '@mui/material/Button';

function DemoContent() {
const dialogs = useDialogs();
return (
<div>
<Button
onClick={async () => {
// preview-start
await dialogs.alert('Hello World');
// preview-end
}}
>
Alert
</Button>
</div>
);
}

export default function AlertDialog() {
return (
<DialogsProvider>
<DemoContent />
</DialogsProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
await dialogs.alert('Hello World');
36 changes: 36 additions & 0 deletions docs/data/toolpad/core/components/use-dialogs/ConfirmDialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as React from 'react';
import { DialogsProvider, useDialogs } from '@toolpad/core/useDialogs';
import Button from '@mui/material/Button';

function DemoContent() {
const dialogs = useDialogs();
return (
<div>
<Button
onClick={async () => {
// preview-start
const confirmed = await dialogs.confirm('Are you sure?', {
okText: 'Yes',
cancelText: 'No',
});
if (confirmed) {
await dialogs.alert("Then let's do it!");
} else {
await dialogs.alert('Ok, forget about it!');
}
// preview-end
}}
>
Confirm
</Button>
</div>
);
}

export default function ConfirmDialog() {
return (
<DialogsProvider>
<DemoContent />
</DialogsProvider>
);
}
36 changes: 36 additions & 0 deletions docs/data/toolpad/core/components/use-dialogs/ConfirmDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as React from 'react';
import { DialogsProvider, useDialogs } from '@toolpad/core/useDialogs';
import Button from '@mui/material/Button';

function DemoContent() {
const dialogs = useDialogs();
return (
<div>
<Button
onClick={async () => {
// preview-start
const confirmed = await dialogs.confirm('Are you sure?', {
okText: 'Yes',
cancelText: 'No',
});
if (confirmed) {
await dialogs.alert("Then let's do it!");
} else {
await dialogs.alert('Ok, forget about it!');
}
// preview-end
}}
>
Confirm
</Button>
</div>
);
}

export default function ConfirmDialog() {
return (
<DialogsProvider>
<DemoContent />
</DialogsProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const confirmed = await dialogs.confirm('Are you sure?', {
okText: 'Yes',
cancelText: 'No',
});
if (confirmed) {
await dialogs.alert("Then let's do it!");
} else {
await dialogs.alert('Ok, forget about it!');
}
61 changes: 61 additions & 0 deletions docs/data/toolpad/core/components/use-dialogs/CustomDialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { DialogsProvider, useDialogs } from '@toolpad/core/useDialogs';
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import DialogActions from '@mui/material/DialogActions';

function MyCustomDialog({ open, onClose }) {
return (
<Dialog fullWidth open={open} onClose={() => onClose()}>
<DialogTitle>Custom dialog</DialogTitle>
<DialogContent>I am a custom dialog</DialogContent>
<DialogActions>
<Button onClick={() => onClose()}>Close me</Button>
</DialogActions>
</Dialog>
);
}

MyCustomDialog.propTypes = {
/**
* A function to call when the dialog should be closed. If the dialog has a return
* value, it should be passed as an argument to this function. You should use the promise
* that is returned to show a loading state while the dialog is performing async actions
* on close.
* @param result The result to return from the dialog.
* @returns A promise that resolves when the dialog can be fully closed.
*/
onClose: PropTypes.func.isRequired,
/**
* Whether the dialog is open.
*/
open: PropTypes.bool.isRequired,
};

function DemoContent() {
const dialogs = useDialogs();
return (
<div>
<Button
onClick={async () => {
// preview-start
await dialogs.open(MyCustomDialog);
// preview-end
}}
>
Open custom
</Button>
</div>
);
}

export default function CustomDialog() {
return (
<DialogsProvider>
<DemoContent />
</DialogsProvider>
);
}
44 changes: 44 additions & 0 deletions docs/data/toolpad/core/components/use-dialogs/CustomDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as React from 'react';
import { DialogsProvider, useDialogs, DialogProps } from '@toolpad/core/useDialogs';
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import DialogActions from '@mui/material/DialogActions';

function MyCustomDialog({ open, onClose }: DialogProps) {
return (
<Dialog fullWidth open={open} onClose={() => onClose()}>
<DialogTitle>Custom dialog</DialogTitle>
<DialogContent>I am a custom dialog</DialogContent>
<DialogActions>
<Button onClick={() => onClose()}>Close me</Button>
</DialogActions>
</Dialog>
);
}

function DemoContent() {
const dialogs = useDialogs();
return (
<div>
<Button
onClick={async () => {
// preview-start
await dialogs.open(MyCustomDialog);
// preview-end
}}
>
Open custom
</Button>
</div>
);
}

export default function CustomDialog() {
return (
<DialogsProvider>
<DemoContent />
</DialogsProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
await dialogs.open(MyCustomDialog);
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { DialogsProvider, useDialogs } from '@toolpad/core/useDialogs';
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import DialogActions from '@mui/material/DialogActions';
import TextField from '@mui/material/TextField';
import Stack from '@mui/material/Stack';

function MyCustomDialog({ payload, open, onClose }) {
return (
<Dialog fullWidth open={open} onClose={() => onClose()}>
<DialogTitle>Dialog with payload</DialogTitle>
<DialogContent>The payload is &quot;{payload}&quot;</DialogContent>
<DialogActions>
<Button onClick={() => onClose()}>Close me</Button>
</DialogActions>
</Dialog>
);
}

MyCustomDialog.propTypes = {
/**
* A function to call when the dialog should be closed. If the dialog has a return
* value, it should be passed as an argument to this function. You should use the promise
* that is returned to show a loading state while the dialog is performing async actions
* on close.
* @param result The result to return from the dialog.
* @returns A promise that resolves when the dialog can be fully closed.
*/
onClose: PropTypes.func.isRequired,
/**
* Whether the dialog is open.
*/
open: PropTypes.bool.isRequired,
/**
* The payload that was passed when the dialog was opened.
*/
payload: PropTypes.string.isRequired,
};

function DemoContent() {
const dialogs = useDialogs();
const [payload, setPayload] = React.useState('Some payload');
return (
<Stack spacing={2}>
<TextField
label="Payload"
value={payload}
onChange={(event) => setPayload(event.currentTarget.value)}
/>
<Button
onClick={async () => {
// preview-start
await dialogs.open(MyCustomDialog, payload);
// preview-end
}}
>
Open
</Button>
</Stack>
);
}

export default function CustomDialogWithPayload() {
return (
<DialogsProvider>
<DemoContent />
</DialogsProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as React from 'react';
import { DialogsProvider, useDialogs, DialogProps } from '@toolpad/core/useDialogs';
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import DialogActions from '@mui/material/DialogActions';
import TextField from '@mui/material/TextField';
import Stack from '@mui/material/Stack';

function MyCustomDialog({ payload, open, onClose }: DialogProps<string>) {
return (
<Dialog fullWidth open={open} onClose={() => onClose()}>
<DialogTitle>Dialog with payload</DialogTitle>
<DialogContent>The payload is &quot;{payload}&quot;</DialogContent>
<DialogActions>
<Button onClick={() => onClose()}>Close me</Button>
</DialogActions>
</Dialog>
);
}

function DemoContent() {
const dialogs = useDialogs();
const [payload, setPayload] = React.useState('Some payload');
return (
<Stack spacing={2}>
<TextField
label="Payload"
value={payload}
onChange={(event) => setPayload(event.currentTarget.value)}
/>
<Button
onClick={async () => {
// preview-start
await dialogs.open(MyCustomDialog, payload);
// preview-end
}}
>
Open
</Button>
</Stack>
);
}

export default function CustomDialogWithPayload() {
return (
<DialogsProvider>
<DemoContent />
</DialogsProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
await dialogs.open(MyCustomDialog, payload);
Loading

0 comments on commit a8fa780

Please sign in to comment.