@orca-fe/antd-plus
Version:
Transformer Container
271 lines (268 loc) • 12.3 kB
JavaScript
var _excluded = ["className", "style", "open", "title", "center", "left", "top", "width", "height", "getContainer", "children", "forceRender", "onOk", "onClose", "afterClose", "footer", "footerAlign", "scrollable", "destroyOnClose", "size", "zIndex", "bodyStyle", "bodyClassname", "onPositionChange"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import { CloseOutlined } from '@ant-design/icons';
import { Button, Space } from 'antd';
import { useDebounceFn, useEventListener, useMemoizedFn } from 'ahooks';
import cn from 'classnames';
import { isInBy } from '@orca-fe/tools';
import { useEffectWithTarget, useHotkeyListener, usePan } from '@orca-fe/hooks';
import useStyles from "./Dialog.style";
// const px = pc('orca-dialog');
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
var ef = () => {};
var Dialog = props => {
var _props$className = props.className,
className = _props$className === void 0 ? '' : _props$className,
style = props.style,
_props$open = props.open,
open = _props$open === void 0 ? false : _props$open,
_props$title = props.title,
title = _props$title === void 0 ? '标题' : _props$title,
_props$center = props.center,
center = _props$center === void 0 ? true : _props$center,
_props$left = props.left,
left = _props$left === void 0 ? 100 : _props$left,
_props$top = props.top,
top = _props$top === void 0 ? 100 : _props$top,
_props$width = props.width,
width = _props$width === void 0 ? 600 : _props$width,
_props$height = props.height,
height = _props$height === void 0 ? 400 : _props$height,
_props$getContainer = props.getContainer,
getContainer = _props$getContainer === void 0 ? () => document.body : _props$getContainer,
children = props.children,
_props$forceRender = props.forceRender,
forceRender = _props$forceRender === void 0 ? false : _props$forceRender,
_props$onOk = props.onOk,
onOk = _props$onOk === void 0 ? ef : _props$onOk,
_props$onClose = props.onClose,
onClose = _props$onClose === void 0 ? ef : _props$onClose,
_props$afterClose = props.afterClose,
afterClose = _props$afterClose === void 0 ? ef : _props$afterClose,
footer = props.footer,
_props$footerAlign = props.footerAlign,
footerAlign = _props$footerAlign === void 0 ? 'right' : _props$footerAlign,
_props$scrollable = props.scrollable,
scrollable = _props$scrollable === void 0 ? true : _props$scrollable,
destroyOnClose = props.destroyOnClose,
_props$size = props.size,
size = _props$size === void 0 ? 'large' : _props$size,
zIndex = props.zIndex,
bodyStyle = props.bodyStyle,
_props$bodyClassname = props.bodyClassname,
bodyClassname = _props$bodyClassname === void 0 ? '' : _props$bodyClassname,
_props$onPositionChan = props.onPositionChange,
onPositionChange = _props$onPositionChan === void 0 ? ef : _props$onPositionChan,
otherProps = _objectWithoutProperties(props, _excluded);
var styles = useStyles();
var _useState = useState({
value: forceRender
}),
_useState2 = _slicedToArray(_useState, 2),
openRef = _useState2[0],
setOpenRef = _useState2[1];
var _useState3 = useState(false),
_useState4 = _slicedToArray(_useState3, 2),
dragging = _useState4[0],
setDragging = _useState4[1];
var _useState5 = useState(false),
_useState6 = _slicedToArray(_useState5, 2),
show = _useState6[0],
setShow = _useState6[1];
var rootRef = useRef(null);
var headerRef = useRef(null);
var _useState7 = useState(() => ({
centerLeft: Math.round(0.5 * (window.innerWidth - width)),
centerTop: Math.round(0.5 * (window.innerHeight - (typeof height === 'string' ? 500 : height)))
})),
_useState8 = _slicedToArray(_useState7, 1),
_useState8$ = _useState8[0],
centerLeft = _useState8$.centerLeft,
centerTop = _useState8$.centerTop;
var _useState9 = useState({
startTop: center ? centerTop : top,
top: center ? centerTop : top,
startLeft: center ? centerLeft : left,
left: center ? centerLeft : left
}),
_useState10 = _slicedToArray(_useState9, 1),
_this = _useState10[0];
var updateBounds = useMemoizedFn((limit = true) => {
var dom = rootRef.current;
if (dom) {
if (limit) {
_this.top = Math.max(0, Math.min(_this.top, window.innerHeight - 50));
_this.left = Math.max(100 - dom.offsetWidth, Math.min(_this.left, window.innerWidth - 100));
}
Object.assign(dom.style, {
left: `${_this.left}px`,
top: `${_this.top}px`,
width: `${width}px`,
height: typeof height === 'string' ? height : `${height}px`
});
}
});
usePan(({
ev,
offset,
start,
finish
}) => {
if (start) {
_this.startTop = _this.top;
_this.startLeft = _this.left;
setDragging(true);
}
_this.top = _this.startTop + offset[1];
_this.left = _this.startLeft + offset[0];
updateBounds(finish);
if (finish) {
setDragging(false);
onPositionChange({
left: _this.left,
top: _this.top
});
}
}, headerRef);
useEffectWithTarget(() => {
updateBounds();
}, [], rootRef);
useHotkeyListener('Escape', () => {
if (open) {
onClose();
}
}, {
target: () => rootRef.current
});
var triggerAfterClose = useDebounceFn(() => {
if (destroyOnClose) {
setOpenRef({
value: false
});
}
afterClose();
}, {
wait: 100
});
useEventListener('transitionend', e => {
if (e.target === e.currentTarget) {
if (!show) {
triggerAfterClose.run();
} else {
triggerAfterClose.cancel();
}
}
}, {
target: rootRef
});
if (open) {
openRef.value = open;
}
useEffect(() => {
if (open) {
setTimeout(() => {
setShow(open);
var dom = rootRef.current;
updateBounds();
if (dom) {
dom.focus();
}
}, 0);
// reset position
} else {
setShow(open);
}
}, [open]);
var triggerOk = () => {
onOk();
};
var triggerClose = () => {
onClose();
};
var defaultFooter = /*#__PURE__*/_jsxs(Space, {
children: [/*#__PURE__*/_jsx(Button, {
size: size === 'small' ? 'small' : 'middle',
onClick: triggerClose,
children: "\u53D6\u6D88"
}), /*#__PURE__*/_jsx(Button, {
size: size === 'small' ? 'small' : 'middle',
type: "primary",
onClick: triggerOk,
children: "\u786E\u5B9A"
})]
});
var realFooter = footer !== undefined ? footer : defaultFooter;
// eslint-disable-next-line react/jsx-no-useless-fragment
if (!openRef.value) return /*#__PURE__*/_jsx(_Fragment, {
children: null
});
return /*#__PURE__*/ReactDOM.createPortal( /*#__PURE__*/_jsx("span", {
className: cn(styles.wrapper, {
[styles.hidden]: !show
}),
style: {
zIndex
},
children: /*#__PURE__*/_jsxs("div", _objectSpread(_objectSpread({
tabIndex: -1,
ref: rootRef,
className: `${cn(styles.root, styles[size], {
[styles.dragging]: dragging
})} ${className}`,
style: _objectSpread(_objectSpread({}, style), {}, {
width,
height
})
}, otherProps), {}, {
children: [/*#__PURE__*/_jsxs("div", {
ref: headerRef,
className: styles.header,
onMouseDown: e => {
if (isInBy(e.target, node => node.tagName === 'BUTTON')) {
e.preventDefault();
}
},
children: [/*#__PURE__*/_jsx("div", {
className: styles.title,
children: title
}), /*#__PURE__*/_jsx("div", {
className: styles.buttons,
children: /*#__PURE__*/_jsx("div", {
className: cn(styles.button, styles.buttonDanger),
onClick: triggerClose,
children: /*#__PURE__*/_jsx(CloseOutlined, {})
})
})]
}), /*#__PURE__*/_jsx("div", {
className: `${cn(styles.body, {
[styles.scrollable]: scrollable
})} ${bodyClassname}`,
style: bodyStyle,
children: children
}), !!realFooter && /*#__PURE__*/_jsx("div", {
className: styles.footer,
style: {
textAlign: footerAlign
},
children: realFooter
})]
}))
}), getContainer());
};
export default Dialog;