Skip to content

Commit ee8cc1d

Browse files
authored
feat: build Information dialog component to show confirmation or alert (#1176)
* Build Information dialog component to show confirmation or alert * Modified the PR based on review * Add preview api as dependency for storybook for orchestrator plugin * Move preview api to dev dependency * Fixed lint issue
1 parent edff3b4 commit ee8cc1d

File tree

3 files changed

+160
-1
lines changed

3 files changed

+160
-1
lines changed

plugins/orchestrator/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@
107107
"ts-loader": "^8.4.0",
108108
"url-loader": "^3.0.0",
109109
"webpack": "^5.70.0",
110-
"webpack-cli": "^4.10.0"
110+
"webpack-cli": "^4.10.0",
111+
"@storybook/preview-api": "^7.5.3"
111112
},
112113
"peerDependencies": {
113114
"react": "^16.13.1 || ^17.0.0",
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import React from 'react';
2+
3+
import { Button } from '@material-ui/core';
4+
import { useArgs } from '@storybook/preview-api';
5+
import { Meta, StoryObj } from '@storybook/react';
6+
7+
import { InfoDialog } from './InfoDialog';
8+
9+
const meta = {
10+
title: 'Orchestrator/InfoDialog',
11+
component: InfoDialog,
12+
} satisfies Meta<typeof InfoDialog>;
13+
14+
export default meta;
15+
type Story = StoryObj<typeof meta>;
16+
17+
const handleSubmit = () => {};
18+
19+
const ConfirmationDialogContent = () => (
20+
<div>
21+
Are you sure you want to submit? By clicking the submit button, you cannot
22+
change the action
23+
</div>
24+
);
25+
const AlertDialogContent = () => (
26+
<div>
27+
This app sends anonymous location data, even when it is not running.
28+
</div>
29+
);
30+
31+
export const ConfirmDialogStory: Story = {
32+
name: 'Confirm Dialog',
33+
args: {
34+
title: 'Confirm',
35+
open: true,
36+
children: <ConfirmationDialogContent />,
37+
},
38+
render: function Render(args) {
39+
const [{ open }, updateArgs] = useArgs();
40+
41+
const handleClose = () => {
42+
updateArgs({ open: !open });
43+
};
44+
45+
const DialogActions = () => (
46+
<>
47+
<Button onClick={handleClose} color="primary">
48+
Disagree
49+
</Button>
50+
<Button onClick={handleSubmit} color="primary">
51+
Agree
52+
</Button>
53+
</>
54+
);
55+
56+
return (
57+
<InfoDialog
58+
{...args}
59+
onClose={handleClose}
60+
dialogActions={<DialogActions />}
61+
/>
62+
);
63+
},
64+
};
65+
66+
export const AlertDialogStory: Story = {
67+
name: 'Alert Dialog',
68+
args: {
69+
title: 'Alert',
70+
open: true,
71+
children: <AlertDialogContent />,
72+
},
73+
render: function Render(args) {
74+
const [{ open }, updateArgs] = useArgs();
75+
76+
const handleClose = () => {
77+
updateArgs({ open: !open });
78+
};
79+
80+
const DialogActions = () => (
81+
<>
82+
<Button onClick={handleClose} color="primary">
83+
OK
84+
</Button>
85+
</>
86+
);
87+
88+
return (
89+
<InfoDialog
90+
{...args}
91+
onClose={handleClose}
92+
dialogActions={<DialogActions />}
93+
/>
94+
);
95+
},
96+
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React, { forwardRef, ForwardRefRenderFunction } from 'react';
2+
3+
import {
4+
Box,
5+
Dialog,
6+
DialogActions,
7+
DialogContent,
8+
DialogTitle,
9+
IconButton,
10+
makeStyles,
11+
Typography,
12+
} from '@material-ui/core';
13+
import CloseIcon from '@material-ui/icons/Close';
14+
15+
export type InfoDialogProps = {
16+
title: string;
17+
open: boolean;
18+
onClose?: () => void;
19+
dialogActions?: React.ReactNode;
20+
children: React.ReactNode;
21+
};
22+
23+
export type ParentComponentRef = HTMLElement;
24+
25+
const useStyles = makeStyles(_theme => ({
26+
closeBtn: {
27+
position: 'absolute',
28+
right: 8,
29+
top: 8,
30+
},
31+
}));
32+
33+
export const RefForwardingInfoDialog: ForwardRefRenderFunction<
34+
ParentComponentRef,
35+
InfoDialogProps
36+
> = (props, forwardedRef): JSX.Element | null => {
37+
const { title, open = false, onClose, children, dialogActions } = props;
38+
const classes = useStyles();
39+
40+
return (
41+
<Dialog onClose={_ => onClose} open={open} ref={forwardedRef}>
42+
<DialogTitle>
43+
<Box>
44+
<Typography variant="h5">{title}</Typography>
45+
<IconButton
46+
className={classes.closeBtn}
47+
aria-label="close"
48+
onClick={onClose}
49+
>
50+
<CloseIcon />
51+
</IconButton>
52+
</Box>
53+
</DialogTitle>
54+
<DialogContent>
55+
<Box>{children}</Box>
56+
</DialogContent>
57+
<DialogActions>{dialogActions}</DialogActions>
58+
</Dialog>
59+
);
60+
};
61+
62+
export const InfoDialog = forwardRef(RefForwardingInfoDialog);

0 commit comments

Comments
 (0)