-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Dialog 컴포넌트 사용 방법 변경을 위한 useDialog 및 DialogProvider 구현
- BooltiUIProvider로 Provider 통합 - useDialogState 삭제
- Loading branch information
Showing
12 changed files
with
187 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import DialogProvider from '../DialogProvider'; | ||
import ThemeProvider from '../ThemeProvider'; | ||
|
||
interface BooltiUIProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
const BooltiUIProvider = ({ children }: BooltiUIProviderProps) => { | ||
return ( | ||
<ThemeProvider> | ||
<DialogProvider>{children}</DialogProvider> | ||
</ThemeProvider> | ||
); | ||
}; | ||
|
||
export default BooltiUIProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useState } from 'react'; | ||
import dialogContext, { IDialog } from '../../contexts/dialogContext'; | ||
import Dialog from '../Dialog'; | ||
|
||
interface DialogProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
const DialogProvider = ({ children }: DialogProviderProps) => { | ||
const [dialogList, setDialogList] = useState<IDialog[]>([]); | ||
|
||
return ( | ||
<dialogContext.Provider value={{ dialogList, setDialogList }}> | ||
{children} | ||
{dialogList.map((dialog) => ( | ||
<Dialog | ||
key={dialog.id} | ||
open={!!dialogList.find(({ id }) => dialog.id === id)} | ||
title={dialog.title} | ||
onClose={() => { | ||
dialog.onClose?.(); | ||
setDialogList((prev) => prev.filter(({ id }) => dialog.id !== id)); | ||
}} | ||
> | ||
{dialog.content} | ||
</Dialog> | ||
))} | ||
</dialogContext.Provider> | ||
); | ||
}; | ||
|
||
export default DialogProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import ThemeProvider from './ThemeProvider'; | ||
import BooltiUIProvider from './BooltiUIProvider'; | ||
import Button from './Button'; | ||
import TextButton from './TextButton'; | ||
import Badge from './Badge'; | ||
import Dialog from './Dialog'; | ||
|
||
export { ThemeProvider, Button, TextButton, Badge, Dialog }; | ||
export { BooltiUIProvider, Button, TextButton, Badge, Dialog }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { createContext } from 'react'; | ||
|
||
export interface IDialog { | ||
id: string; | ||
content: React.ReactNode; | ||
title?: string; | ||
onClose?: () => void; | ||
} | ||
|
||
interface DialogContext { | ||
dialogList: IDialog[]; | ||
setDialogList: React.Dispatch<React.SetStateAction<IDialog[]>>; | ||
} | ||
|
||
const dialogContext = createContext<DialogContext | null>(null); | ||
|
||
export default dialogContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import useToast from './useToast'; | ||
import useDialogState from './useDialogState'; | ||
import useDialog from './useDialog'; | ||
|
||
export { useToast, useDialogState }; | ||
export { useToast, useDialog }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useCallback, useContext, useRef } from 'react'; | ||
import { nanoid } from 'nanoid'; | ||
import dialogContext, { IDialog } from '../contexts/dialogContext'; | ||
|
||
const useDialog = () => { | ||
const id = useRef<string>(nanoid(6)); | ||
|
||
const context = useContext(dialogContext); | ||
|
||
const open = useCallback( | ||
({ | ||
content, | ||
title, | ||
onClose, | ||
}: { | ||
content: React.ReactNode; | ||
title?: string; | ||
onClose?: () => void; | ||
}) => { | ||
const newDialog: IDialog = { | ||
id: id.current, | ||
content, | ||
title, | ||
onClose, | ||
}; | ||
|
||
context?.setDialogList((prev) => [...prev, newDialog]); | ||
}, | ||
[context, id], | ||
); | ||
|
||
const close = useCallback(() => { | ||
context?.setDialogList((prev) => prev.filter((dialog) => dialog.id !== id.current)); | ||
}, [context, id]); | ||
|
||
return { | ||
id: id.current, | ||
open, | ||
close, | ||
}; | ||
}; | ||
|
||
export default useDialog; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters