ConfirmContext.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import React, { useState } from 'react';
  2. interface ConfirmContextType {
  3. content: React.ReactNode;
  4. confirm?: () => void;
  5. setContent: React.Dispatch<React.SetStateAction<React.ReactNode>>;
  6. setConfirm: React.Dispatch<React.SetStateAction<(() => void) | undefined>>;
  7. cancel: () => void;
  8. dangerButton: boolean;
  9. setDangerButton: React.Dispatch<React.SetStateAction<boolean>>;
  10. }
  11. export const ConfirmContext = React.createContext<ConfirmContextType | null>(
  12. null
  13. );
  14. export const ConfirmContextProvider: React.FC<
  15. React.PropsWithChildren<unknown>
  16. > = ({ children }) => {
  17. const [content, setContent] = useState<React.ReactNode>(null);
  18. const [confirm, setConfirm] = useState<(() => void) | undefined>(undefined);
  19. const [dangerButton, setDangerButton] = useState(false);
  20. const cancel = () => {
  21. setContent(null);
  22. setConfirm(undefined);
  23. };
  24. return (
  25. <ConfirmContext.Provider
  26. value={{
  27. content,
  28. setContent,
  29. confirm,
  30. setConfirm,
  31. cancel,
  32. dangerButton,
  33. setDangerButton,
  34. }}
  35. >
  36. {children}
  37. </ConfirmContext.Provider>
  38. );
  39. };