您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

87 行
1.4KB

  1. import {
  2. Button,
  3. Dialog,
  4. DialogActions,
  5. DialogContent,
  6. DialogContentText,
  7. DialogTitle,
  8. } from "@mui/material";
  9. import { ReactNode, useState } from "react";
  10. type Props = {
  11. message?: string;
  12. onClose?: VoidFunction;
  13. onAgree?: VoidFunction;
  14. onDisagree?: VoidFunction;
  15. };
  16. export type UseDialogReturn = {
  17. show: boolean;
  18. open: VoidFunction;
  19. close: VoidFunction;
  20. setShow: (show: boolean) => void;
  21. element: ReactNode;
  22. };
  23. export function useDialog({
  24. message,
  25. onClose,
  26. onAgree,
  27. onDisagree,
  28. }: Props = {}): UseDialogReturn {
  29. const [show, setShow] = useState(false);
  30. const open = () => {
  31. setShow(true);
  32. };
  33. const close = () => {
  34. if (onClose) {
  35. onClose();
  36. }
  37. setShow(false);
  38. };
  39. const agree = () => {
  40. if (onAgree) {
  41. onAgree();
  42. }
  43. setShow(false);
  44. };
  45. const disagree = () => {
  46. if (onDisagree) {
  47. onDisagree();
  48. }
  49. setShow(false);
  50. };
  51. const element = (
  52. <Dialog open={show} onClose={close}>
  53. <DialogTitle>確認</DialogTitle>
  54. {message && (
  55. <DialogContent>
  56. <DialogContentText>{message}</DialogContentText>
  57. </DialogContent>
  58. )}
  59. <DialogActions>
  60. <Button onClick={disagree}>CANCEL</Button>
  61. <Button onClick={agree}>OK</Button>
  62. </DialogActions>
  63. </Dialog>
  64. );
  65. return {
  66. // param
  67. show,
  68. // Element
  69. element,
  70. // function
  71. open,
  72. close,
  73. setShow,
  74. };
  75. }