-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from imaginer-dev/83--ํ์
modal
feat: ํ์ (modal)์ถ๊ฐ
- Loading branch information
Showing
3 changed files
with
90 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,32 @@ | ||
import { useRef } from 'react'; | ||
import Dialog from '../src/components/Dialog.tsx'; | ||
|
||
interface DialogElement { | ||
openModal: () => void; | ||
closeModal: () => void; | ||
} | ||
|
||
function App() { | ||
return <div></div>; | ||
const dialogRef1 = useRef<DialogElement | null>(null); | ||
const dialogRef2 = useRef<DialogElement | null>(null); | ||
|
||
const openModal = (dialogRef: React.RefObject<DialogElement>) => { | ||
if (dialogRef.current) { | ||
dialogRef.current?.openModal(); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<button onClick={() => openModal(dialogRef1)}>ํ์ ์ด๊ธฐ 1</button> | ||
<Dialog ref={dialogRef1} desc="ํ์ ์ค๋ช "></Dialog> | ||
<br /> | ||
<button onClick={() => openModal(dialogRef2)}>ํ์ ์ด๊ธฐ 2</button> | ||
<Dialog ref={dialogRef2} title="ํ์ ์ ๋ชฉ" desc="ํ์ ์ค๋ช "> | ||
<p>children</p> | ||
</Dialog> | ||
</div> | ||
); | ||
} | ||
|
||
export default App; |
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,13 @@ | ||
import Dialog from './Dialog.tsx'; | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
|
||
const meta: Meta<typeof Dialog> = { | ||
title: 'Dialog', | ||
component: Dialog, | ||
}; | ||
|
||
export default meta; | ||
|
||
export type Story = StoryObj<typeof Dialog>; | ||
|
||
export const Default: Story = {}; |
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,49 @@ | ||
import { ReactNode, forwardRef, useImperativeHandle, useRef } from 'react'; | ||
|
||
interface Props { | ||
children?: ReactNode; //์ ํ | ||
title?: string; //์ ํ | ||
desc: string; //ํ์ | ||
} | ||
|
||
interface DialogElement { | ||
openModal: () => void; | ||
closeModal: () => void; | ||
} | ||
|
||
const Dialog = forwardRef<DialogElement, Props>((props, ref) => { | ||
const { children, title, desc } = props; | ||
const dialogRef = useRef<HTMLDialogElement | null>(null); | ||
|
||
useImperativeHandle(ref, () => ({ | ||
openModal: () => dialogRef.current?.showModal(), | ||
closeModal: () => dialogRef.current?.close(), | ||
})); | ||
|
||
//๋์ : ์ด๋์ด ๋ฐ๋ฅ ํด๋ฆญ์ ๋ซ๊ธฐ | ||
const handleClose = (event: React.MouseEvent) => { | ||
if (dialogRef.current && event.target === dialogRef.current) { | ||
dialogRef.current?.close(); | ||
} | ||
}; | ||
|
||
//ํ ์คํธ ์ ๋ ฌ : title์ด ์์๊ฒฝ์ฐ desc ๊ฐ์ด๋ฐ ์ ๋ ฌ | ||
const descClassName = `py-4 text-lg ${title ? '' : 'text-center'}`; | ||
|
||
return ( | ||
<dialog ref={dialogRef} className="modal modal-middle" onClick={handleClose}> | ||
<div className="modal-box" onClick={(e) => e.stopPropagation()}> | ||
{title && <h3 className="text-lg font-bold">{title}</h3>} | ||
<p className={descClassName}>{desc}</p> | ||
{children && <div>{children}</div>} | ||
<form method="dialog" className="modal-action flex justify-center"> | ||
<button type="button" className="btn bg-primary text-base-100" onClick={() => dialogRef.current?.close()}> | ||
๋ซ๊ธฐ | ||
</button> | ||
</form> | ||
</div> | ||
</dialog> | ||
); | ||
}); | ||
|
||
export default Dialog; |