Skip to content

Commit ea36992

Browse files
author
신용준
committed
v1.2.0
- 포탈 추가 (react-dialog가 body 밑에 렌더링 된다.) - confirmText, cancelText 옵션 추가
1 parent 24ea373 commit ea36992

File tree

11 files changed

+75
-21
lines changed

11 files changed

+75
-21
lines changed

package/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@shinyongjun/react-dialog",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"main": "./dist/cjs/index.js",
55
"module": "./dist/esm/index.js",
66
"source": "./src/index.tsx",

package/src/components/DialogProvider.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ import PromptDialog from './dialog/PromptDialog';
88

99
interface IProps {
1010
children: ReactNode;
11+
confirmText?: string;
12+
cancelText?: string;
1113
}
1214

13-
function DialogProvider({ children }: IProps) {
15+
function DialogProvider({
16+
children,
17+
confirmText = 'ok',
18+
cancelText = 'cancel',
19+
}: IProps) {
1420
return (
15-
<PromptDialog>
16-
<AlertDialog>
17-
<ConfirmDialog>{children}</ConfirmDialog>
21+
<PromptDialog confirmText={confirmText} cancelText={cancelText}>
22+
<AlertDialog confirmText={confirmText} cancelText={cancelText}>
23+
<ConfirmDialog confirmText={confirmText} cancelText={cancelText}>
24+
{children}
25+
</ConfirmDialog>
1826
</AlertDialog>
1927
</PromptDialog>
2028
);

package/src/components/UI/Alert.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import DialogWrapper from './Wrapper';
44

55
interface IProps {
66
message: string;
7+
confirmText: string;
8+
cancelText: string;
79
onClose: () => void;
810
}
911

10-
const Alert = ({ message, onClose }: IProps) => {
12+
const Alert = ({ message, confirmText, cancelText, onClose }: IProps) => {
1113
const okRef = useRef<HTMLButtonElement>(null);
1214

1315
useEffect(() => {
@@ -41,7 +43,7 @@ const Alert = ({ message, onClose }: IProps) => {
4143
className="react-dialog__button-ok"
4244
onClick={onClose}
4345
>
44-
ok
46+
{confirmText}
4547
</button>
4648
</div>
4749
</DialogWrapper>

package/src/components/UI/Confirm.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ import DialogWrapper from './Wrapper';
55

66
interface IProps {
77
message: string;
8+
confirmText: string;
9+
cancelText: string;
810
onClickOK: () => void;
911
onClickCancel: () => void;
1012
}
1113

12-
const Confirm = ({ message, onClickOK, onClickCancel }: IProps) => {
14+
const Confirm = ({
15+
message,
16+
confirmText,
17+
cancelText,
18+
onClickOK,
19+
onClickCancel,
20+
}: IProps) => {
1321
const okRef = useRef<HTMLButtonElement>(null);
1422

1523
useEffect(() => {
@@ -44,14 +52,14 @@ const Confirm = ({ message, onClickOK, onClickCancel }: IProps) => {
4452
onClick={onClickOK}
4553
autoFocus
4654
>
47-
ok
55+
{confirmText}
4856
</button>
4957
<button
5058
type="button"
5159
className="react-dialog__button-cancel"
5260
onClick={onClickCancel}
5361
>
54-
cancel
62+
{cancelText}
5563
</button>
5664
</div>
5765
</DialogWrapper>

package/src/components/UI/Portal.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { createPortal } from 'react-dom';
2+
3+
interface IProps {
4+
children: React.ReactNode;
5+
selector: string;
6+
}
7+
8+
const Portal = ({ children, selector }: IProps) => {
9+
const element =
10+
typeof window !== 'undefined' && document.querySelector(selector);
11+
return element && children ? createPortal(children, element) : null;
12+
};
13+
14+
export default Portal;

package/src/components/UI/Prompt.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import DialogWrapper from './Wrapper';
55

66
interface IProps {
77
message: string;
8+
confirmText: string;
9+
cancelText: string;
810
_default: string;
911
onClickOK: (result: string) => void;
1012
onClickCancel: () => void;
1113
}
1214

13-
const Prompt = ({ message, _default, onClickOK, onClickCancel }: IProps) => {
15+
const Prompt = ({ message, confirmText, cancelText, _default, onClickOK, onClickCancel }: IProps) => {
1416
const inputRef = useRef<HTMLInputElement>(null);
1517

1618
useEffect(() => {
@@ -56,14 +58,14 @@ const Prompt = ({ message, _default, onClickOK, onClickCancel }: IProps) => {
5658
/>
5759
<div className="react-dialog__button-wrapper">
5860
<button type="submit" className="react-dialog__button-ok">
59-
ok
61+
{confirmText}
6062
</button>
6163
<button
6264
type="reset"
6365
className="react-dialog__button-cancel"
6466
onClick={onClickCancel}
6567
>
66-
cancel
68+
{cancelText}
6769
</button>
6870
</div>
6971
</form>

package/src/components/UI/Wrapper.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
/* eslint-disable jsx-a11y/no-autofocus */
22
import * as React from 'react';
33
import { ReactNode } from 'react';
4+
import Portal from './Portal';
45

56
interface IProps {
67
children: ReactNode;
78
}
89

910
const DialogWrapper = ({ children }: IProps) => {
1011
return (
11-
<div className="react-dialog__wrapper">
12-
<div className="react-dialog__dialog">{children}</div>
13-
</div>
12+
<Portal selector="body">
13+
<div className="react-dialog__wrapper">
14+
<div className="react-dialog__dialog">{children}</div>
15+
</div>
16+
</Portal>
1417
);
1518
};
1619

package/src/components/dialog/AlertDialog.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ type AlertState = {
99
};
1010

1111
interface IProps {
12+
confirmText: string;
13+
cancelText: string;
1214
children: ReactNode;
1315
}
1416

15-
function AlertDialog({ children }: IProps) {
17+
function AlertDialog({ children, confirmText, cancelText }: IProps) {
1618
const [state, setState] = useState<AlertState>();
1719

1820
const alert = (message?: any): Promise<undefined> => {
@@ -30,7 +32,14 @@ function AlertDialog({ children }: IProps) {
3032
return (
3133
<AlertContext.Provider value={{ alert }}>
3234
{children}
33-
{state && <Alert message={state.message} onClose={state.onClose} />}
35+
{state && (
36+
<Alert
37+
confirmText={confirmText}
38+
cancelText={cancelText}
39+
message={state.message}
40+
onClose={state.onClose}
41+
/>
42+
)}
3443
</AlertContext.Provider>
3544
);
3645
}

package/src/components/dialog/ConfirmDialog.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ type ConfirmState = {
1010
};
1111

1212
interface IProps {
13+
confirmText: string;
14+
cancelText: string;
1315
children: ReactNode;
1416
}
1517

16-
function ConfirmDialog({ children }: IProps) {
18+
function ConfirmDialog({ children, confirmText, cancelText }: IProps) {
1719
const [state, setState] = useState<ConfirmState>();
1820

1921
const confirm = (message?: string): Promise<boolean> => {
@@ -41,6 +43,8 @@ function ConfirmDialog({ children }: IProps) {
4143
{/* state 여부에 따라 Confirm 다이얼로그 띄우기 */}
4244
{state && (
4345
<Confirm
46+
confirmText={confirmText}
47+
cancelText={cancelText}
4448
message={state.message}
4549
onClickOK={state.onClickOK}
4650
onClickCancel={state.onClickCancel}

package/src/components/dialog/PromptDialog.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ type PromptState = {
1111
};
1212

1313
interface IProps {
14+
confirmText: string;
15+
cancelText: string;
1416
children: ReactNode;
1517
}
1618

17-
function PromptDialog({ children }: IProps) {
19+
function PromptDialog({ children, confirmText, cancelText }: IProps) {
1820
const [state, setState] = useState<PromptState>();
1921

2022
const prompt = (
@@ -42,6 +44,8 @@ function PromptDialog({ children }: IProps) {
4244
{children}
4345
{state && (
4446
<Prompt
47+
confirmText={confirmText}
48+
cancelText={cancelText}
4549
message={state.message}
4650
_default={state._default}
4751
onClickOK={state.onClickOK}

test/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { DialogProvider } from '@shinyongjun/react-dialog';
66
const root = ReactDOM.createRoot(document.getElementById('root'));
77
root.render(
88
<React.StrictMode>
9-
<DialogProvider>
9+
<DialogProvider confirmText="test">
1010
<App />
1111
</DialogProvider>
1212
</React.StrictMode>

0 commit comments

Comments
 (0)