brew-js-react 0.2.8 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dialog.d.ts CHANGED
@@ -10,24 +10,26 @@ export interface DialogBaseProps<T> {
10
10
  title?: string;
11
11
  className?: string;
12
12
  showCloseButton?: boolean;
13
+ preventLeave?: boolean;
14
+ preventNavigation?: boolean;
13
15
  modal?: boolean;
14
16
  onCommit?: (value: T | undefined) => void;
15
17
  onOpen?: (root: HTMLElement) => void;
16
18
  onClose?: (root: HTMLElement) => void;
17
19
  }
18
20
 
19
- export interface DialogRenderComponentProps<T> extends DialogBaseProps<T> {
21
+ export interface DialogRenderComponentProps<T, P = {}> extends DialogBaseProps<T>, P {
20
22
  closeDialog: DialogCloseCallback<T>;
21
23
  }
22
24
 
23
- export interface DialogOptions<T> extends DialogBaseProps<T> {
24
- onRender: React.FC<DialogRenderComponentProps<T>>;
25
+ export interface DialogOptions<T, P = {}> extends DialogBaseProps<T> {
26
+ onRender: React.FC<DialogRenderComponentProps<T, P>>;
25
27
  }
26
28
 
27
29
  export interface DialogProps<T> extends React.PropsWithChildren<DialogBaseProps<T>> {
28
30
  isOpen: boolean;
29
31
  }
30
32
 
31
- export function createDialog<T = any>(props: DialogOptions<T>): DialogState<T>;
33
+ export function createDialog<T = any, P = {}>(props: DialogOptions<T, P> & { wrapper?: React.FC<React.PropsWithChildren<DialogRenderComponentProps<T, P>>> }): DialogState<T>;
32
34
 
33
35
  export function Dialog<T>(props: DialogProps<T>): JSX.Element;
package/dialog.js CHANGED
@@ -1,32 +1,33 @@
1
- import React, { useEffect, useState } from "react";
1
+ import { createElement, useEffect, useState } from "react";
2
2
  import ReactDOM from "react-dom";
3
3
  import { always, catchAsync, either, extend, makeAsync, noop, pipe } from "./include/zeta-dom/util.js";
4
4
  import { containsOrEquals, removeNode } from "./include/zeta-dom/domUtil.js";
5
5
  import dom from "./include/zeta-dom/dom.js";
6
- import { lock } from "./include/zeta-dom/domLock.js";
6
+ import { lock, preventLeave } from "./include/zeta-dom/domLock.js";
7
7
  import { closeFlyout, openFlyout } from "./include/brew-js/domAction.js";
8
8
 
9
+ const createRoot = ReactDOM.createRoot;
10
+
9
11
  /**
10
12
  * @param {Partial<import("./dialog").DialogOptions<any>>} props
11
13
  */
12
14
  export function createDialog(props) {
13
15
  var root = document.createElement('div');
14
- var closing = false;
16
+ var reactRoot = createRoot && createRoot(root);
17
+ var closeDialog = closeFlyout.bind(0, root);
15
18
  var promise;
16
19
 
17
- function closeDialog(value) {
18
- if (!closing) {
19
- closing = true;
20
- closeFlyout(root, value).then(function () {
21
- closing = false;
22
- removeNode(root);
23
- (props.onClose || noop)(root);
24
- if (props.onRender) {
25
- ReactDOM.unmountComponentAtNode(root);
26
- }
27
- });
20
+ dom.on(root, 'flyouthide', function () {
21
+ removeNode(root);
22
+ (props.onClose || noop)(root);
23
+ if (props.onRender) {
24
+ if (reactRoot) {
25
+ reactRoot.unmount();
26
+ } else {
27
+ ReactDOM.unmountComponentAtNode(root);
28
+ }
28
29
  }
29
- }
30
+ });
30
31
 
31
32
  return {
32
33
  root: root,
@@ -50,9 +51,22 @@ export function createDialog(props) {
50
51
  promise.then(closeDialog, noop);
51
52
  }
52
53
  });
53
- ReactDOM.render(React.createElement(props.onRender, dialogProps), root);
54
+ var content = createElement(props.onRender, dialogProps);
55
+ if (props.wrapper) {
56
+ content = createElement(props.wrapper, dialogProps, content);
57
+ }
58
+ if (reactRoot) {
59
+ reactRoot.render(content);
60
+ } else {
61
+ ReactDOM.render(content, root);
62
+ }
54
63
  }
55
64
  promise = openFlyout(root);
65
+ if (props.preventLeave) {
66
+ preventLeave(root, promise);
67
+ } else if (props.preventNavigation) {
68
+ lock(root, promise);
69
+ }
56
70
  always(promise, function () {
57
71
  promise = null;
58
72
  });