Skip to content

Commit

Permalink
Toast component refactor (#4957)
Browse files Browse the repository at this point in the history
* Refactored Toast component to support a primary and secondary button and exposed visibility state
* Addressed feedback
  • Loading branch information
minhtuev authored Oct 22, 2024
1 parent dcf166b commit 26431e2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
32 changes: 23 additions & 9 deletions app/packages/components/src/components/Toast/Toast.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import React, { useState } from "react";
import { Snackbar, Button, SnackbarContent } from "@mui/material";
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import BoltIcon from '@mui/icons-material/Bolt'; // Icon for the lightning bolt
import React from "react";
import { atom, useRecoilState } from "recoil";
import { Box, Snackbar, SnackbarContent } from "@mui/material";

// Define types for the props
interface ToastProps {
action: React.ReactNode; // Accepts any valid React component, element, or JSX
message: React.ReactNode; // Accepts any valid React component, element, or JSX
message: React.ReactNode;
primary: (setOpen: React.Dispatch<React.SetStateAction<boolean>>) => React.ReactNode;
secondary: (setOpen: React.Dispatch<React.SetStateAction<boolean>>) => React.ReactNode;
duration?: number; // Optional duration, with a default value
}

const Toast: React.FC<ToastProps> = ({ action, message, duration = 5000 }) => {
const [open, setOpen] = useState(true);
const toastStateAtom = atom({
key: "toastOpenState",
default: true,
});

const Toast: React.FC<ToastProps> = ({message, primary, secondary, duration = 5000 }) => {

const [open, setOpen] = useRecoilState(toastStateAtom); // State management for toast visibility

const handleClose = (event, reason) => {
if (reason === "clickaway") {
Expand All @@ -21,6 +26,15 @@ const Toast: React.FC<ToastProps> = ({ action, message, duration = 5000 }) => {
setOpen(false);
};

const action = (
<div>
<Box display="flex" justifyContent="flex-end">
{primary(setOpen)} {/* Pass setOpen to primary button */}
{secondary(setOpen)} {/* Pass setOpen to secondary button */}
</Box>
</div>
);

return (
<Snackbar
anchorOrigin={{ vertical: "bottom", horizontal: "center" }}
Expand Down
2 changes: 1 addition & 1 deletion app/packages/components/src/components/Toast/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {default as Toast} from "./Toast";
export { default } from "./Toast";
2 changes: 1 addition & 1 deletion app/packages/components/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ export { default as TabOption } from "./TabOption";
export { default as TextField } from "./TextField";
export { default as ThemeProvider, useFont, useTheme } from "./ThemeProvider";
export { default as Tooltip } from "./Tooltip";
export { Toast } from "./Toast";
export { default as Toast } from "./Toast";

export * from "./types";

0 comments on commit 26431e2

Please sign in to comment.