Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toast component refactor #4957

Merged
merged 7 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
);

minhtuev marked this conversation as resolved.
Show resolved Hide resolved
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";
Loading