feat(toast): use id for open status toggling (#178)

This commit is contained in:
Mamadou DICKO 2023-05-27 12:35:39 +02:00 committed by GitHub
parent e388990384
commit 655f07ddd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 5 deletions

View File

@ -18,12 +18,12 @@ export const Toast = ({
<ToastContext.Provider value={{ publish }}> <ToastContext.Provider value={{ publish }}>
{children} {children}
<AnimatePresence mode="popLayout"> <AnimatePresence mode="popLayout">
{toasts.map((toast, index) => { {toasts.map((toast) => {
if (!toast.open) return; if (!toast.open) return;
return ( return (
<ToastPrimitive.Root <ToastPrimitive.Root
open={toast.open} open={toast.open}
onOpenChange={(value) => toggleToast(value, index)} onOpenChange={(value) => toggleToast(value, toast.id)}
asChild asChild
forceMount forceMount
key={toast.id} key={toast.id}

View File

@ -6,10 +6,10 @@ import { generateToastUniqueId } from "../helpers/generateToastUniqueId";
export const useToastBuilder = () => { export const useToastBuilder = () => {
const [toasts, setToasts] = useState<ToastContent[]>([]); const [toasts, setToasts] = useState<ToastContent[]>([]);
const toggleToast = (value: boolean, index: number) => { const toggleToast = (value: boolean, toastId: string) => {
setToasts((toasts) => setToasts((toasts) =>
toasts.map((toast, i) => { toasts.map((toast) => {
if (i === index) { if (toast.id === toastId) {
toast.open = value; toast.open = value;
} }
return toast; return toast;