Skip to content
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 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion apps/admin/src/pages/HomePage/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useToast, useDialog } from '@boolti/ui';
import { useToast, useDialog, useConfirm } from '@boolti/ui';
import Header from '../../components/Header/Header';
import Layout from '../../components/Layout/Layout';
import { PATH } from '../../constants/routes';
Expand All @@ -11,6 +11,8 @@ const HomePage = () => {
const dialog2 = useDialog();
const dialog3 = useDialog();

const confirm = useConfirm();

return (
<Layout
header={
Expand Down Expand Up @@ -112,6 +114,30 @@ const HomePage = () => {
>
Open Dialog 2
</button>

<button
onClick={async () => {
const result = await confirm(
<>
지금 로그아웃하면 작성 중인 내용이 사라져요.
<br />
로그아웃 할까요?
</>,
{
cancel: '취소하기',
confirm: '로그아웃',
},
);

if (result) {
alert('로그아웃');
} else {
alert('로그아웃 취소');
}
}}
>
Open confirm
</button>
</Layout>
);
};
Expand Down
5 changes: 4 additions & 1 deletion packages/ui/src/components/BooltiUIProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ConfirmProvider from '../ConfirmProvider';
import DialogProvider from '../DialogProvider';
import ThemeProvider from '../ThemeProvider';

Expand All @@ -8,7 +9,9 @@ interface BooltiUIProviderProps {
const BooltiUIProvider = ({ children }: BooltiUIProviderProps) => {
return (
<ThemeProvider>
<DialogProvider>{children}</DialogProvider>
<DialogProvider>
<ConfirmProvider>{children}</ConfirmProvider>
</DialogProvider>
</ThemeProvider>
);
};
Expand Down
66 changes: 66 additions & 0 deletions packages/ui/src/components/Confirm/Confirm.styles.ts
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,
};
32 changes: 32 additions & 0 deletions packages/ui/src/components/Confirm/index.tsx
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;
35 changes: 35 additions & 0 deletions packages/ui/src/components/ConfirmProvider/index.tsx
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;
21 changes: 21 additions & 0 deletions packages/ui/src/contexts/confirmContext.ts
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;
3 changes: 2 additions & 1 deletion packages/ui/src/hooks/index.ts
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 };
17 changes: 17 additions & 0 deletions packages/ui/src/hooks/useConfirm.ts
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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise로 만든 이유가 따로 있을까?

Copy link
Member Author

@Puterism Puterism Jan 29, 2024

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해야 한다고 생각했어. 다른 방법이 있을까?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이해했다! 굿굿 나중 생각해서 훨씬 좋을 것 같네!

context?.setCurrentConfirm({ message, buttonText, resolve });
});
},
[context],
);
};

export default useConfirm;
Loading