-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Confirm 컴포넌트 구현 #22
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,66 @@ | ||
import styled from '@emotion/styled'; | ||
|
||
const CONFIRM_WIDTH = '450px'; | ||
|
||
const DimmedArea = styled.div` | ||
position: fixed; | ||
inset: 0; | ||
background-color: ${({ theme }) => theme.palette.dim.dialog}; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
`; | ||
|
||
const Confirm = styled.div` | ||
width: ${CONFIRM_WIDTH}; | ||
border-radius: 8px; | ||
background-color: ${({ theme }) => theme.palette.grey.w}; | ||
padding: 32px; | ||
`; | ||
|
||
const ConfirmMessage = styled.p` | ||
${({ theme }) => theme.typo.b3}; | ||
color: ${({ theme }) => theme.palette.grey.g90}; | ||
margin-bottom: 32px; | ||
`; | ||
|
||
const ConfirmButtonContainer = styled.div` | ||
text-align: right; | ||
`; | ||
|
||
const CancelButton = styled.button` | ||
display: inline-flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 48px; | ||
padding: 0 20px; | ||
border-radius: 4px; | ||
border: 1px solid ${({ theme }) => theme.palette.grey.g90}; | ||
background-color: ${({ theme }) => theme.palette.grey.w}; | ||
color: ${({ theme }) => theme.palette.grey.g90}; | ||
${({ theme }) => theme.typo.sh1}; | ||
margin-right: 8px; | ||
cursor: pointer; | ||
`; | ||
|
||
const ConfirmButton = styled.button` | ||
display: inline-flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 48px; | ||
padding: 0 20px; | ||
border-radius: 4px; | ||
background-color: ${({ theme }) => theme.palette.primary.o1}; | ||
color: ${({ theme }) => theme.palette.grey.w}; | ||
${({ theme }) => theme.typo.sh1}; | ||
cursor: pointer; | ||
`; | ||
|
||
export default { | ||
DimmedArea, | ||
Confirm, | ||
ConfirmMessage, | ||
ConfirmButtonContainer, | ||
CancelButton, | ||
ConfirmButton, | ||
}; |
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 Portal from '../Portal'; | ||
import Styled from './Confirm.styles'; | ||
|
||
interface ConfirmProps { | ||
children: React.ReactNode; | ||
cancelText?: string; | ||
confirmText?: string; | ||
onCancel?: () => void; | ||
onConfirm?: () => void; | ||
} | ||
|
||
const Confirm = ({ children, cancelText, confirmText, onCancel, onConfirm }: ConfirmProps) => { | ||
return ( | ||
<Portal> | ||
<Styled.DimmedArea> | ||
<Styled.Confirm> | ||
<Styled.ConfirmMessage>{children}</Styled.ConfirmMessage> | ||
<Styled.ConfirmButtonContainer> | ||
<Styled.CancelButton type="button" onClick={onCancel}> | ||
{cancelText ?? '취소'} | ||
</Styled.CancelButton> | ||
<Styled.ConfirmButton type="button" onClick={onConfirm}> | ||
{confirmText ?? '확인'} | ||
</Styled.ConfirmButton> | ||
</Styled.ConfirmButtonContainer> | ||
</Styled.Confirm> | ||
</Styled.DimmedArea> | ||
</Portal> | ||
); | ||
}; | ||
|
||
export default Confirm; |
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,35 @@ | ||
import { useState } from 'react'; | ||
import confirmContext, { IConfirm } from '../../contexts/confirmContext'; | ||
import Confirm from '../Confirm'; | ||
|
||
interface ConfirmProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
const ConfirmProvider = ({ children }: ConfirmProviderProps) => { | ||
const [currentConfirm, setCurrentConfirm] = useState<IConfirm | null>(null); | ||
|
||
return ( | ||
<confirmContext.Provider value={{ currentConfirm, setCurrentConfirm }}> | ||
{children} | ||
{currentConfirm && ( | ||
<Confirm | ||
cancelText={currentConfirm.buttonText?.cancel} | ||
confirmText={currentConfirm.buttonText?.confirm} | ||
onCancel={() => { | ||
currentConfirm.resolve(false); | ||
setCurrentConfirm(null); | ||
}} | ||
onConfirm={() => { | ||
currentConfirm.resolve(true); | ||
setCurrentConfirm(null); | ||
}} | ||
> | ||
{currentConfirm.message} | ||
</Confirm> | ||
)} | ||
</confirmContext.Provider> | ||
); | ||
}; | ||
|
||
export default ConfirmProvider; |
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,21 @@ | ||
import { createContext } from 'react'; | ||
|
||
export interface ConfirmButtonText { | ||
cancel?: string; | ||
confirm?: string; | ||
} | ||
|
||
export interface IConfirm { | ||
message: React.ReactNode; | ||
buttonText?: ConfirmButtonText; | ||
resolve: (value: boolean | PromiseLike<boolean>) => void; | ||
} | ||
|
||
interface ConfirmContext { | ||
currentConfirm: IConfirm | null; | ||
setCurrentConfirm: React.Dispatch<React.SetStateAction<IConfirm | null>>; | ||
} | ||
|
||
const confirmContext = createContext<ConfirmContext | null>(null); | ||
|
||
export default confirmContext; |
File renamed without changes.
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,5 @@ | ||
import useToast from './useToast'; | ||
import useDialog from './useDialog'; | ||
import useConfirm from './useConfirm'; | ||
|
||
export { useToast, useDialog }; | ||
export { useToast, useDialog, useConfirm }; |
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 { useCallback, useContext } from 'react'; | ||
import confirmContext, { ConfirmButtonText } from '../contexts/confirmContext'; | ||
|
||
const useConfirm = () => { | ||
const context = useContext(confirmContext); | ||
|
||
return useCallback( | ||
(message: React.ReactNode, buttonText?: ConfirmButtonText) => { | ||
return new Promise<boolean>((resolve) => { | ||
context?.setCurrentConfirm({ message, buttonText, resolve }); | ||
}); | ||
}, | ||
[context], | ||
); | ||
}; | ||
|
||
export default useConfirm; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Promise
로 만든 이유가 따로 있을까?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const result = confirm()
의 형태로 Confirm 컴포넌트를 사용하려면, 컴포넌트가 띄워지고, 확인/취소 버튼 중 어떤 것이 눌렸는지 결과가 바로 반환되는 것이 아니기 때문에 Promise로 resolve해야 한다고 생각했어. 다른 방법이 있을까?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이해했다! 굿굿 나중 생각해서 훨씬 좋을 것 같네!