index.jsx 2.42 KB
Newer Older
wuhao's avatar
wuhao committed
1 2
import { SendOutlined } from "@ant-design/icons";
import LoadingButton from "@mui/lab/LoadingButton";
wuhao's avatar
wuhao committed
3 4 5 6 7 8
import Button from "@mui/material/Button";
import Dialog from "@mui/material/Dialog";
import DialogActions from "@mui/material/DialogActions";
import DialogContent from "@mui/material/DialogContent";
import DialogTitle from "@mui/material/DialogTitle";
import Paper from "@mui/material/Paper";
wuhao's avatar
wuhao committed
9 10
import Slide from "@mui/material/Slide";
import React from "react";
wuhao's avatar
wuhao committed
11 12
import Draggable from "react-draggable";

wuhao's avatar
wuhao committed
13 14 15 16
const Transition = React.forwardRef(function Transition(props, ref) {
  return <Slide direction="up" ref={ref} {...props} />;
});

wuhao's avatar
wuhao committed
17 18 19 20 21 22 23 24 25 26 27
function PaperComponent(props) {
  return (
    <Draggable
      handle="#draggable-dialog-title"
      cancel={'[class*="MuiDialogContent-root"]'}
    >
      <Paper {...props} />
    </Draggable>
  );
}

wuhao's avatar
wuhao committed
28 29 30 31 32 33 34 35
export default function DraggableDialog({
  children,
  dialogprops,
  handleClose,
  loading,
  formdom,
  maxWidth,
}) {
wuhao's avatar
wuhao committed
36 37 38 39 40 41 42 43
  const formRef = React.useRef();

  return (
    <div>
      <Dialog
        onClose={handleClose}
        PaperComponent={PaperComponent}
        aria-labelledby="draggable-dialog-title"
wuhao's avatar
wuhao committed
44 45 46 47 48
        TransitionComponent={Transition}
        maxWidth={maxWidth ?? "xs"}
        fullWidth
        {...dialogprops}
        //keepMounted
wuhao's avatar
wuhao committed
49 50
      >
        <DialogTitle style={{ cursor: "move" }} id="draggable-dialog-title">
wuhao's avatar
wuhao committed
51
          {dialogprops?.title}
wuhao's avatar
wuhao committed
52 53
        </DialogTitle>
        <DialogContent>
wuhao's avatar
wuhao committed
54 55 56 57
          {children &&
            React.cloneElement(children, { submitter: false, formRef })}
          {formdom &&
            React.cloneElement(formdom, { submitter: false, formRef })}
wuhao's avatar
wuhao committed
58
        </DialogContent>
wuhao's avatar
wuhao committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        {dialogprops?.footer === false ? null : (
          <DialogActions>
            <Button
              type="reset"
              key="rest"
              onClick={() => {
                formRef?.current?.resetFields();
              }}
            >
              重置
            </Button>
            <LoadingButton
              type="submit"
              key="submit"
              variant="contained"
              loading={loading}
              loadingPosition="start"
              startIcon={<SendOutlined />}
wuhao's avatar
wuhao committed
77
              disabled={dialogprops?.disabled}
wuhao's avatar
wuhao committed
78
              onClick={() => {
wuhao's avatar
wuhao committed
79
                formRef?.current?.submit();
wuhao's avatar
wuhao committed
80 81 82 83 84 85
              }}
            >
              提交
            </LoadingButton>
          </DialogActions>
        )}
wuhao's avatar
wuhao committed
86 87 88 89
      </Dialog>
    </div>
  );
}