react-confirm 0.3.0-1 → 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 +17 -11
- package/package.json +17 -6
- package/typescript/index.d.ts +45 -0
package/README.md
CHANGED
|
@@ -3,10 +3,14 @@ react-confirm is a lightweight library that simplifies the implementation of con
|
|
|
3
3
|
|
|
4
4
|
One key feature of react-confirm is that it doesn't provide a specific view or component for the confirmation dialog, allowing you to easily customize the appearance of the dialog to match your application's design.
|
|
5
5
|
|
|
6
|
-
In the [example](https://github.com/haradakunihiko/react-confirm/tree/master/example), [react-bootstrap](https://react-bootstrap-v3.netlify.app/components/modal/) and [material-ui](http://www.material-ui.com/#/components/dialog) are used with.
|
|
7
|
-
|
|
8
6
|
[](https://badge.fury.io/js/react-confirm)
|
|
9
7
|
|
|
8
|
+
## Examples
|
|
9
|
+
- [react-bootstrap demo in codesandbox](https://codesandbox.io/s/react-confirm-with-react-bootstrap-kjju1)
|
|
10
|
+
- [chakra-ui(using context) demo in codesandbox](https://codesandbox.io/s/react-confirm-with-chakra-ui-oidpf1)
|
|
11
|
+
- [react-bootstrap example](https://github.com/haradakunihiko/react-confirm/tree/master/example/react-bootstrap)
|
|
12
|
+
- [material-ui example](https://github.com/haradakunihiko/react-confirm/tree/master/example/material-ui)
|
|
13
|
+
|
|
10
14
|
## Motivation
|
|
11
15
|
React is a powerful library that allows for reactive rendering based on component state. However, managing temporary states like confirmation dialogs can quickly become complex. The question is: is it worth implementing these states within your app? The answer is not always a clear yes.
|
|
12
16
|
|
|
@@ -75,7 +79,7 @@ export function confirmWrapper(confirmation, options = {}) {
|
|
|
75
79
|
|
|
76
80
|
### Call it!
|
|
77
81
|
Now, you can show dialog just like window.confirm with async-await. The most common example is onclick handler for submit buttons.
|
|
78
|
-
|
|
82
|
+
|
|
79
83
|
```js
|
|
80
84
|
import { confirmWrapper, confirm } from './confirm'
|
|
81
85
|
|
|
@@ -104,12 +108,12 @@ You can check more complex example in [codesandbox](https://codesandbox.io/s/rea
|
|
|
104
108
|
## Using with Context
|
|
105
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.
|
|
106
110
|
|
|
107
|
-
Create your own `createConfirmation` using `createConfirmationCreater` and `createReactTreeMounter`.
|
|
111
|
+
Create your own `createConfirmation` function and `MountPoint` Component using `createConfirmationCreater` and `createReactTreeMounter`.
|
|
108
112
|
|
|
109
113
|
```js
|
|
110
114
|
import { createConfirmationCreater, createReactTreeMounter, createMountPoint } from 'react-confirm';
|
|
111
115
|
|
|
112
|
-
const mounter = createReactTreeMounter();
|
|
116
|
+
const mounter = createReactTreeMounter();
|
|
113
117
|
|
|
114
118
|
export const createConfirmation = createConfirmationCreater(mounter);
|
|
115
119
|
export const MountPoint = createMountPoint(mounter);
|
|
@@ -135,16 +139,18 @@ export const confirm = createConfirmation(YourDialog);
|
|
|
135
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.
|
|
136
140
|
|
|
137
141
|
```js
|
|
138
|
-
const mounter = createReactTreeMounter(document.body);
|
|
142
|
+
const mounter = createReactTreeMounter(document.body);
|
|
139
143
|
```
|
|
140
144
|
|
|
141
145
|
### example
|
|
142
146
|
Context example with Chakra-ui in [codesandbox](https://codesandbox.io/s/react-confirm-with-chakra-ui-oidpf1)
|
|
143
147
|
|
|
144
|
-
## typescript
|
|
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.
|
|
145
150
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
151
|
+
```ts
|
|
152
|
+
const Confirmation1: React.FC<ConfirmDialogProps<Props, Response>> = (props) => (<Dialog></Dialog>)
|
|
153
|
+
const Confirmation2: ConfirmDialog<Props, Response> = (props) => (<Dialog></Dialog>)
|
|
154
|
+
```
|
|
149
155
|
|
|
150
|
-
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;
|