react-confirm 0.3.0-2 → 0.3.0-4
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/README.md +17 -10
- package/package.json +17 -6
- package/typescript/index.d.ts +45 -0
package/README.md
CHANGED
|
@@ -23,7 +23,10 @@ react-confirm library offers several benefits:
|
|
|
23
23
|
- The library provides flexibility in designing the dialog. There is no limitation in the type of components you can use, whether it be input forms or multiple buttons. You can even check out the demo site to see examples of how to customize the dialog.
|
|
24
24
|
|
|
25
25
|
## Demo
|
|
26
|
-
https://
|
|
26
|
+
[](https://stackblitz.com/fork/github/haradakunihiko/react-confirm-sample/tree/main/1_typescript)
|
|
27
|
+
|
|
28
|
+
Please note that interactions with the sample dialogs output details to the console. Review the console output while interacting with the dialogs to observe the behavior and outcomes.
|
|
29
|
+
|
|
27
30
|
|
|
28
31
|
## Versions
|
|
29
32
|
|
|
@@ -79,7 +82,7 @@ export function confirmWrapper(confirmation, options = {}) {
|
|
|
79
82
|
|
|
80
83
|
### Call it!
|
|
81
84
|
Now, you can show dialog just like window.confirm with async-await. The most common example is onclick handler for submit buttons.
|
|
82
|
-
|
|
85
|
+
|
|
83
86
|
```js
|
|
84
87
|
import { confirmWrapper, confirm } from './confirm'
|
|
85
88
|
|
|
@@ -106,14 +109,16 @@ const handleOnClick2 = async () => {
|
|
|
106
109
|
You can check more complex example in [codesandbox](https://codesandbox.io/s/react-confirm-with-react-bootstrap-kjju1)
|
|
107
110
|
|
|
108
111
|
## Using with Context
|
|
112
|
+
[](https://stackblitz.com/fork/github/haradakunihiko/react-confirm-sample/tree/main/2_typescript_using_context)
|
|
113
|
+
|
|
109
114
|
By default, this library renders the confirmation dialog without appending the component to your app's React component tree. While this can be useful, it may cause issues if you need to consume context in your component. To overcome this problem, you can use the `MountPoint` component to include your confirmation dialog within your app's tree, enabling it to access context and other data from the app.
|
|
110
115
|
|
|
111
|
-
Create your own `createConfirmation` using `createConfirmationCreater` and `createReactTreeMounter`.
|
|
116
|
+
Create your own `createConfirmation` function and `MountPoint` Component using `createConfirmationCreater` and `createReactTreeMounter`.
|
|
112
117
|
|
|
113
118
|
```js
|
|
114
119
|
import { createConfirmationCreater, createReactTreeMounter, createMountPoint } from 'react-confirm';
|
|
115
120
|
|
|
116
|
-
const mounter = createReactTreeMounter();
|
|
121
|
+
const mounter = createReactTreeMounter();
|
|
117
122
|
|
|
118
123
|
export const createConfirmation = createConfirmationCreater(mounter);
|
|
119
124
|
export const MountPoint = createMountPoint(mounter);
|
|
@@ -139,16 +144,18 @@ export const confirm = createConfirmation(YourDialog);
|
|
|
139
144
|
To render the confirmation dialog within the React component tree but in a different part of the DOM, you can pass a DOM element to the `createReactTreeMounter` function. This will use the `createPortal` method to render the confirmation dialog in the specified DOM element while keeping it within the React component tree.
|
|
140
145
|
|
|
141
146
|
```js
|
|
142
|
-
const mounter = createReactTreeMounter(document.body);
|
|
147
|
+
const mounter = createReactTreeMounter(document.body);
|
|
143
148
|
```
|
|
144
149
|
|
|
145
150
|
### example
|
|
146
151
|
Context example with Chakra-ui in [codesandbox](https://codesandbox.io/s/react-confirm-with-chakra-ui-oidpf1)
|
|
147
152
|
|
|
148
|
-
## typescript
|
|
149
|
-
|
|
150
|
-
Experimentally added full typescript declaration files at `typescript` branch.
|
|
153
|
+
## typescript usage
|
|
154
|
+
Below, we present two possible ways to define a confirmation dialog component using react-confirm. You can choose either based on your preference.
|
|
151
155
|
|
|
152
|
-
|
|
156
|
+
```ts
|
|
157
|
+
const Confirmation1: React.FC<ConfirmDialogProps<Props, Response>> = (props) => (<Dialog></Dialog>)
|
|
158
|
+
const Confirmation2: ConfirmDialog<Props, Response> = (props) => (<Dialog></Dialog>)
|
|
159
|
+
```
|
|
153
160
|
|
|
154
|
-
and
|
|
161
|
+
Ensure to specify both the dialog component's `Props` and the response value `Response` when using these types. These typings will be especially useful when defining functions to display the dialog.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-confirm",
|
|
3
|
-
"version": "0.3.0-
|
|
3
|
+
"version": "0.3.0-4",
|
|
4
4
|
"description": "Small library which makes your Dialog component callable",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -18,26 +18,37 @@
|
|
|
18
18
|
"homepage": "https://github.com/haradakunihiko/react-confirm",
|
|
19
19
|
"files": [
|
|
20
20
|
"lib",
|
|
21
|
-
"src"
|
|
21
|
+
"src",
|
|
22
|
+
"typescript"
|
|
22
23
|
],
|
|
23
24
|
"scripts": {
|
|
24
25
|
"clean": "rimraf lib",
|
|
25
26
|
"build": "babel src --out-dir lib",
|
|
26
27
|
"prepublish": "npm run clean && npm run build",
|
|
27
|
-
"test": "
|
|
28
|
+
"test": "jest"
|
|
28
29
|
},
|
|
29
30
|
"author": "",
|
|
30
31
|
"license": "MIT",
|
|
31
32
|
"peerDependencies": {
|
|
32
|
-
"react
|
|
33
|
-
"react": "18.x"
|
|
33
|
+
"react": "18.x",
|
|
34
|
+
"react-dom": "18.x"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
37
|
"@babel/cli": "^7.17.6",
|
|
37
38
|
"@babel/core": "^7.17.9",
|
|
38
39
|
"@babel/preset-env": "^7.16.11",
|
|
39
40
|
"@babel/preset-react": "^7.16.7",
|
|
41
|
+
"@testing-library/jest-dom": "^5.16.5",
|
|
42
|
+
"@testing-library/react": "^14.0.0",
|
|
43
|
+
"babel-core": "^6.26.3",
|
|
44
|
+
"babel-jest": "^29.5.0",
|
|
45
|
+
"jest": "^29.5.0",
|
|
46
|
+
"jest-environment-jsdom": "^29.5.0",
|
|
47
|
+
"react": "^18.2.0",
|
|
48
|
+
"react-dom": "^18.2.0",
|
|
49
|
+
"regenerator-runtime": "^0.13.11",
|
|
40
50
|
"rimraf": "^3.0.2"
|
|
41
51
|
},
|
|
42
|
-
"dependencies": {}
|
|
52
|
+
"dependencies": {},
|
|
53
|
+
"typings": "typescript/index.d.ts"
|
|
43
54
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
type ConfirmableProps<P, R> = {
|
|
4
|
+
dispose: () => void;
|
|
5
|
+
resolve: PromiseLike<R>;
|
|
6
|
+
reject: (reason?: any) => void;
|
|
7
|
+
} & P;
|
|
8
|
+
|
|
9
|
+
type ConfirmableDialog<P, R> = React.ComponentType<ConfirmableProps<P, R>>;
|
|
10
|
+
|
|
11
|
+
export type ConfirmDialogProps<P, R> = {
|
|
12
|
+
dismiss: () => void;
|
|
13
|
+
proceed: (value: R) => void;
|
|
14
|
+
cancel: (value?: any) => void;
|
|
15
|
+
show: boolean;
|
|
16
|
+
} & P;
|
|
17
|
+
|
|
18
|
+
export type ConfirmDialog<P, R> = React.ComponentType<ConfirmDialogProps<P, R>> ;
|
|
19
|
+
|
|
20
|
+
export declare function confirmable<P, R>(
|
|
21
|
+
component: ConfirmDialog<P, R>
|
|
22
|
+
): ConfirmableDialog<P, R>;
|
|
23
|
+
|
|
24
|
+
export declare function createConfirmation<P, R>(
|
|
25
|
+
component: ConfirmableDialog<P, R>,
|
|
26
|
+
unmountDelay?: number,
|
|
27
|
+
mountingNode?: HTMLElement,
|
|
28
|
+
): (props: P) => Promise<R>;
|
|
29
|
+
|
|
30
|
+
type Mounter = {
|
|
31
|
+
mount: (component: React.ComponentType, ) => string
|
|
32
|
+
unmount: (key: string) => void
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type TreeMounter = {
|
|
36
|
+
options: {
|
|
37
|
+
setMountedCallback: (callback: any) => void
|
|
38
|
+
mountNode: Element | DocumentFragment
|
|
39
|
+
}
|
|
40
|
+
} & Mounter;
|
|
41
|
+
|
|
42
|
+
export declare function createReactTreeMounter(mountNode?: HTMLElement): TreeMounter;
|
|
43
|
+
export declare function createMountPoint(moounter: TreeMounter): React.ComponentType;
|
|
44
|
+
export declare function createDomTreeMounter(mountNode?: HTMLElement): Mounter;
|
|
45
|
+
export declare function createConfirmationCreater(mounter: Mounter): typeof createConfirmation;
|