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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _activeElement = _interopRequireDefault(require("dom-helpers/activeElement"));
  5. var _contains = _interopRequireDefault(require("dom-helpers/query/contains"));
  6. var _inDOM = _interopRequireDefault(require("dom-helpers/util/inDOM"));
  7. var _listen = _interopRequireDefault(require("dom-helpers/events/listen"));
  8. var _propTypes = _interopRequireDefault(require("prop-types"));
  9. var _componentOrElement = _interopRequireDefault(require("prop-types-extra/lib/componentOrElement"));
  10. var _elementType = _interopRequireDefault(require("prop-types-extra/lib/elementType"));
  11. var _react = _interopRequireDefault(require("react"));
  12. var _reactDom = _interopRequireDefault(require("react-dom"));
  13. var _ModalManager = _interopRequireDefault(require("./ModalManager"));
  14. var _Portal = _interopRequireDefault(require("./Portal"));
  15. var _getContainer = _interopRequireDefault(require("./utils/getContainer"));
  16. var _ownerDocument = _interopRequireDefault(require("./utils/ownerDocument"));
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  18. function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
  19. 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; }
  20. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  21. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  22. var modalManager = new _ModalManager.default();
  23. function omitProps(props, propTypes) {
  24. var keys = Object.keys(props);
  25. var newProps = {};
  26. keys.map(function (prop) {
  27. if (!Object.prototype.hasOwnProperty.call(propTypes, prop)) {
  28. newProps[prop] = props[prop];
  29. }
  30. });
  31. return newProps;
  32. }
  33. /**
  34. * Love them or hate them, `<Modal />` provides a solid foundation for creating dialogs, lightboxes, or whatever else.
  35. * The Modal component renders its `children` node in front of a backdrop component.
  36. *
  37. * The Modal offers a few helpful features over using just a `<Portal/>` component and some styles:
  38. *
  39. * - Manages dialog stacking when one-at-a-time just isn't enough.
  40. * - Creates a backdrop, for disabling interaction below the modal.
  41. * - It properly manages focus; moving to the modal content, and keeping it there until the modal is closed.
  42. * - It disables scrolling of the page content while open.
  43. * - Adds the appropriate ARIA roles are automatically.
  44. * - Easily pluggable animations via a `<Transition/>` component.
  45. *
  46. * Note that, in the same way the backdrop element prevents users from clicking or interacting
  47. * with the page content underneath the Modal, Screen readers also need to be signaled to not to
  48. * interact with page content while the Modal is open. To do this, we use a common technique of applying
  49. * the `aria-hidden='true'` attribute to the non-Modal elements in the Modal `container`. This means that for
  50. * a Modal to be truly modal, it should have a `container` that is _outside_ your app's
  51. * React hierarchy (such as the default: document.body).
  52. */
  53. var Modal =
  54. /*#__PURE__*/
  55. function (_React$Component) {
  56. _inheritsLoose(Modal, _React$Component);
  57. function Modal() {
  58. var _this;
  59. for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) {
  60. _args[_key] = arguments[_key];
  61. }
  62. _this = _React$Component.call.apply(_React$Component, [this].concat(_args)) || this;
  63. _this.state = {
  64. exited: !_this.props.show
  65. };
  66. _this.onPortalRendered = function () {
  67. if (_this.props.onShow) {
  68. _this.props.onShow();
  69. } // autofocus after onShow, to not trigger a focus event for previous
  70. // modals before this one is shown.
  71. _this.autoFocus();
  72. };
  73. _this.onShow = function () {
  74. var doc = (0, _ownerDocument.default)(_assertThisInitialized(_assertThisInitialized(_this)));
  75. var container = (0, _getContainer.default)(_this.props.container, doc.body);
  76. _this.props.manager.add(_assertThisInitialized(_assertThisInitialized(_this)), container, _this.props.containerClassName);
  77. _this.removeKeydownListener = (0, _listen.default)(doc, 'keydown', _this.handleDocumentKeyDown);
  78. _this.removeFocusListener = (0, _listen.default)(doc, 'focus', // the timeout is necessary b/c this will run before the new modal is mounted
  79. // and so steals focus from it
  80. function () {
  81. return setTimeout(_this.enforceFocus);
  82. }, true);
  83. };
  84. _this.onHide = function () {
  85. _this.props.manager.remove(_assertThisInitialized(_assertThisInitialized(_this)));
  86. _this.removeKeydownListener();
  87. _this.removeFocusListener();
  88. if (_this.props.restoreFocus) {
  89. _this.restoreLastFocus();
  90. }
  91. };
  92. _this.setDialogRef = function (ref) {
  93. _this.dialog = ref;
  94. };
  95. _this.setBackdropRef = function (ref) {
  96. _this.backdrop = ref && _reactDom.default.findDOMNode(ref);
  97. };
  98. _this.handleHidden = function () {
  99. _this.setState({
  100. exited: true
  101. });
  102. _this.onHide();
  103. if (_this.props.onExited) {
  104. var _this$props;
  105. (_this$props = _this.props).onExited.apply(_this$props, arguments);
  106. }
  107. };
  108. _this.handleBackdropClick = function (e) {
  109. if (e.target !== e.currentTarget) {
  110. return;
  111. }
  112. if (_this.props.onBackdropClick) {
  113. _this.props.onBackdropClick(e);
  114. }
  115. if (_this.props.backdrop === true) {
  116. _this.props.onHide();
  117. }
  118. };
  119. _this.handleDocumentKeyDown = function (e) {
  120. if (_this.props.keyboard && e.keyCode === 27 && _this.isTopModal()) {
  121. if (_this.props.onEscapeKeyDown) {
  122. _this.props.onEscapeKeyDown(e);
  123. }
  124. _this.props.onHide();
  125. }
  126. };
  127. _this.enforceFocus = function () {
  128. if (!_this.props.enforceFocus || !_this._isMounted || !_this.isTopModal()) {
  129. return;
  130. }
  131. var currentActiveElement = (0, _activeElement.default)((0, _ownerDocument.default)(_assertThisInitialized(_assertThisInitialized(_this))));
  132. if (_this.dialog && !(0, _contains.default)(_this.dialog, currentActiveElement)) {
  133. _this.dialog.focus();
  134. }
  135. };
  136. _this.renderBackdrop = function () {
  137. var _this$props2 = _this.props,
  138. renderBackdrop = _this$props2.renderBackdrop,
  139. Transition = _this$props2.backdropTransition;
  140. var backdrop = renderBackdrop({
  141. ref: _this.setBackdropRef,
  142. onClick: _this.handleBackdropClick
  143. });
  144. if (Transition) {
  145. backdrop = _react.default.createElement(Transition, {
  146. appear: true,
  147. in: _this.props.show
  148. }, backdrop);
  149. }
  150. return backdrop;
  151. };
  152. return _this;
  153. }
  154. Modal.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps) {
  155. if (nextProps.show) {
  156. return {
  157. exited: false
  158. };
  159. } else if (!nextProps.transition) {
  160. // Otherwise let handleHidden take care of marking exited.
  161. return {
  162. exited: true
  163. };
  164. }
  165. return null;
  166. };
  167. var _proto = Modal.prototype;
  168. _proto.getSnapshotBeforeUpdate = function getSnapshotBeforeUpdate(prevProps) {
  169. if (_inDOM.default && !prevProps.show && this.props.show) {
  170. this.lastFocus = (0, _activeElement.default)();
  171. }
  172. return null;
  173. };
  174. _proto.componentDidMount = function componentDidMount() {
  175. this._isMounted = true;
  176. if (this.props.show) {
  177. this.onShow();
  178. }
  179. };
  180. _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
  181. var transition = this.props.transition;
  182. if (prevProps.show && !this.props.show && !transition) {
  183. // Otherwise handleHidden will call this.
  184. this.onHide();
  185. } else if (!prevProps.show && this.props.show) {
  186. this.onShow();
  187. }
  188. };
  189. _proto.componentWillUnmount = function componentWillUnmount() {
  190. var _this$props3 = this.props,
  191. show = _this$props3.show,
  192. transition = _this$props3.transition;
  193. this._isMounted = false;
  194. if (show || transition && !this.state.exited) {
  195. this.onHide();
  196. }
  197. };
  198. _proto.autoFocus = function autoFocus() {
  199. if (!this.props.autoFocus) return;
  200. var currentActiveElement = (0, _activeElement.default)((0, _ownerDocument.default)(this));
  201. if (this.dialog && !(0, _contains.default)(this.dialog, currentActiveElement)) {
  202. this.lastFocus = currentActiveElement;
  203. this.dialog.focus();
  204. }
  205. };
  206. _proto.restoreLastFocus = function restoreLastFocus() {
  207. // Support: <=IE11 doesn't support `focus()` on svg elements (RB: #917)
  208. if (this.lastFocus && this.lastFocus.focus) {
  209. this.lastFocus.focus();
  210. this.lastFocus = null;
  211. }
  212. };
  213. _proto.isTopModal = function isTopModal() {
  214. return this.props.manager.isTopModal(this);
  215. };
  216. _proto.render = function render() {
  217. var _this$props4 = this.props,
  218. show = _this$props4.show,
  219. container = _this$props4.container,
  220. children = _this$props4.children,
  221. renderDialog = _this$props4.renderDialog,
  222. _this$props4$role = _this$props4.role,
  223. role = _this$props4$role === void 0 ? 'dialog' : _this$props4$role,
  224. Transition = _this$props4.transition,
  225. backdrop = _this$props4.backdrop,
  226. className = _this$props4.className,
  227. style = _this$props4.style,
  228. onExit = _this$props4.onExit,
  229. onExiting = _this$props4.onExiting,
  230. onEnter = _this$props4.onEnter,
  231. onEntering = _this$props4.onEntering,
  232. onEntered = _this$props4.onEntered,
  233. props = _objectWithoutPropertiesLoose(_this$props4, ["show", "container", "children", "renderDialog", "role", "transition", "backdrop", "className", "style", "onExit", "onExiting", "onEnter", "onEntering", "onEntered"]);
  234. if (!(show || Transition && !this.state.exited)) {
  235. return null;
  236. }
  237. var dialogProps = _extends({
  238. role: role,
  239. ref: this.setDialogRef,
  240. // apparently only works on the dialog role element
  241. 'aria-modal': role === 'dialog' ? true : undefined
  242. }, omitProps(props, Modal.propTypes), {
  243. style: style,
  244. className: className,
  245. tabIndex: '-1'
  246. });
  247. var dialog = renderDialog ? renderDialog(dialogProps) : _react.default.createElement("div", dialogProps, _react.default.cloneElement(children, {
  248. role: 'document'
  249. }));
  250. if (Transition) {
  251. dialog = _react.default.createElement(Transition, {
  252. appear: true,
  253. unmountOnExit: true,
  254. in: show,
  255. onExit: onExit,
  256. onExiting: onExiting,
  257. onExited: this.handleHidden,
  258. onEnter: onEnter,
  259. onEntering: onEntering,
  260. onEntered: onEntered
  261. }, dialog);
  262. }
  263. return _react.default.createElement(_Portal.default, {
  264. container: container,
  265. onRendered: this.onPortalRendered
  266. }, _react.default.createElement(_react.default.Fragment, null, backdrop && this.renderBackdrop(), dialog));
  267. };
  268. return Modal;
  269. }(_react.default.Component);
  270. Modal.propTypes = {
  271. /**
  272. * Set the visibility of the Modal
  273. */
  274. show: _propTypes.default.bool,
  275. /**
  276. * A Node, Component instance, or function that returns either. The Modal is appended to it's container element.
  277. *
  278. * For the sake of assistive technologies, the container should usually be the document body, so that the rest of the
  279. * page content can be placed behind a virtual backdrop as well as a visual one.
  280. */
  281. container: _propTypes.default.oneOfType([_componentOrElement.default, _propTypes.default.func]),
  282. /**
  283. * A callback fired when the Modal is opening.
  284. */
  285. onShow: _propTypes.default.func,
  286. /**
  287. * A callback fired when either the backdrop is clicked, or the escape key is pressed.
  288. *
  289. * The `onHide` callback only signals intent from the Modal,
  290. * you must actually set the `show` prop to `false` for the Modal to close.
  291. */
  292. onHide: _propTypes.default.func,
  293. /**
  294. * Include a backdrop component.
  295. */
  296. backdrop: _propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.oneOf(['static'])]),
  297. /**
  298. * A function that returns the dialog component. Useful for custom
  299. * rendering. **Note:** the component should make sure to apply the provided ref.
  300. *
  301. * ```js
  302. * renderDialog={props => <MyDialog {...props} />}
  303. * ```
  304. */
  305. renderDialog: _propTypes.default.func,
  306. /**
  307. * A function that returns a backdrop component. Useful for custom
  308. * backdrop rendering.
  309. *
  310. * ```js
  311. * renderBackdrop={props => <MyBackdrop {...props} />}
  312. * ```
  313. */
  314. renderBackdrop: _propTypes.default.func,
  315. /**
  316. * A callback fired when the escape key, if specified in `keyboard`, is pressed.
  317. */
  318. onEscapeKeyDown: _propTypes.default.func,
  319. /**
  320. * A callback fired when the backdrop, if specified, is clicked.
  321. */
  322. onBackdropClick: _propTypes.default.func,
  323. /**
  324. * A css class or set of classes applied to the modal container when the modal is open,
  325. * and removed when it is closed.
  326. */
  327. containerClassName: _propTypes.default.string,
  328. /**
  329. * Close the modal when escape key is pressed
  330. */
  331. keyboard: _propTypes.default.bool,
  332. /**
  333. * A `react-transition-group@2.0.0` `<Transition/>` component used
  334. * to control animations for the dialog component.
  335. */
  336. transition: _elementType.default,
  337. /**
  338. * A `react-transition-group@2.0.0` `<Transition/>` component used
  339. * to control animations for the backdrop components.
  340. */
  341. backdropTransition: _elementType.default,
  342. /**
  343. * When `true` The modal will automatically shift focus to itself when it opens, and
  344. * replace it to the last focused element when it closes. This also
  345. * works correctly with any Modal children that have the `autoFocus` prop.
  346. *
  347. * Generally this should never be set to `false` as it makes the Modal less
  348. * accessible to assistive technologies, like screen readers.
  349. */
  350. autoFocus: _propTypes.default.bool,
  351. /**
  352. * When `true` The modal will prevent focus from leaving the Modal while open.
  353. *
  354. * Generally this should never be set to `false` as it makes the Modal less
  355. * accessible to assistive technologies, like screen readers.
  356. */
  357. enforceFocus: _propTypes.default.bool,
  358. /**
  359. * When `true` The modal will restore focus to previously focused element once
  360. * modal is hidden
  361. */
  362. restoreFocus: _propTypes.default.bool,
  363. /**
  364. * Callback fired before the Modal transitions in
  365. */
  366. onEnter: _propTypes.default.func,
  367. /**
  368. * Callback fired as the Modal begins to transition in
  369. */
  370. onEntering: _propTypes.default.func,
  371. /**
  372. * Callback fired after the Modal finishes transitioning in
  373. */
  374. onEntered: _propTypes.default.func,
  375. /**
  376. * Callback fired right before the Modal transitions out
  377. */
  378. onExit: _propTypes.default.func,
  379. /**
  380. * Callback fired as the Modal begins to transition out
  381. */
  382. onExiting: _propTypes.default.func,
  383. /**
  384. * Callback fired after the Modal finishes transitioning out
  385. */
  386. onExited: _propTypes.default.func,
  387. /**
  388. * A ModalManager instance used to track and manage the state of open
  389. * Modals. Useful when customizing how modals interact within a container
  390. */
  391. manager: _propTypes.default.object.isRequired
  392. };
  393. Modal.defaultProps = {
  394. show: false,
  395. role: 'dialog',
  396. backdrop: true,
  397. keyboard: true,
  398. autoFocus: true,
  399. enforceFocus: true,
  400. restoreFocus: true,
  401. onHide: function onHide() {},
  402. manager: modalManager,
  403. renderBackdrop: function renderBackdrop(props) {
  404. return _react.default.createElement("div", props);
  405. }
  406. };
  407. Modal.Manager = _ModalManager.default;
  408. var _default = Modal;
  409. exports.default = _default;
  410. module.exports = exports.default;