react-confirm 0.3.0-2 → 0.3.0-3
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 +11 -9
- package/package.json +17 -6
- package/typescript/index.d.ts +45 -0
package/README.md
CHANGED
|
@@ -79,7 +79,7 @@ export function confirmWrapper(confirmation, options = {}) {
|
|
|
79
79
|
|
|
80
80
|
### Call it!
|
|
81
81
|
Now, you can show dialog just like window.confirm with async-await. The most common example is onclick handler for submit buttons.
|
|
82
|
-
|
|
82
|
+
|
|
83
83
|
```js
|
|
84
84
|
import { confirmWrapper, confirm } from './confirm'
|
|
85
85
|
|
|
@@ -108,12 +108,12 @@ You can check more complex example in [codesandbox](https://codesandbox.io/s/rea
|
|
|
108
108
|
## Using with Context
|
|
109
109
|
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
110
|
|
|
111
|
-
Create your own `createConfirmation` using `createConfirmationCreater` and `createReactTreeMounter`.
|
|
111
|
+
Create your own `createConfirmation` function and `MountPoint` Component using `createConfirmationCreater` and `createReactTreeMounter`.
|
|
112
112
|
|
|
113
113
|
```js
|
|
114
114
|
import { createConfirmationCreater, createReactTreeMounter, createMountPoint } from 'react-confirm';
|
|
115
115
|
|
|
116
|
-
const mounter = createReactTreeMounter();
|
|
116
|
+
const mounter = createReactTreeMounter();
|
|
117
117
|
|
|
118
118
|
export const createConfirmation = createConfirmationCreater(mounter);
|
|
119
119
|
export const MountPoint = createMountPoint(mounter);
|
|
@@ -139,16 +139,18 @@ export const confirm = createConfirmation(YourDialog);
|
|
|
139
139
|
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
140
|
|
|
141
141
|
```js
|
|
142
|
-
const mounter = createReactTreeMounter(document.body);
|
|
142
|
+
const mounter = createReactTreeMounter(document.body);
|
|
143
143
|
```
|
|
144
144
|
|
|
145
145
|
### example
|
|
146
146
|
Context example with Chakra-ui in [codesandbox](https://codesandbox.io/s/react-confirm-with-chakra-ui-oidpf1)
|
|
147
147
|
|
|
148
|
-
## typescript
|
|
149
|
-
|
|
150
|
-
Experimentally added full typescript declaration files at `typescript` branch.
|
|
148
|
+
## typescript usage
|
|
149
|
+
Below, we present two possible ways to define a confirmation dialog component using react-confirm. You can choose either based on your preference.
|
|
151
150
|
|
|
152
|
-
|
|
151
|
+
```ts
|
|
152
|
+
const Confirmation1: React.FC<ConfirmDialogProps<Props, Response>> = (props) => (<Dialog></Dialog>)
|
|
153
|
+
const Confirmation2: ConfirmDialog<Props, Response> = (props) => (<Dialog></Dialog>)
|
|
154
|
+
```
|
|
153
155
|
|
|
154
|
-
and
|
|
156
|
+
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-3",
|
|
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;
|