feat: Add Swal hook
This commit is contained in:
parent
1e944cc316
commit
84cb9aea60
@ -15,6 +15,7 @@ import QSelect from '@/components/ui/QSelect'
|
|||||||
import { Button } from '@nextui-org/react'
|
import { Button } from '@nextui-org/react'
|
||||||
import ColorPicker from './common/color-picker/ColorPicker'
|
import ColorPicker from './common/color-picker/ColorPicker'
|
||||||
import { toastUp } from '@/hooks/useToast'
|
import { toastUp } from '@/hooks/useToast'
|
||||||
|
import { useSwal } from '@/hooks/useSwal'
|
||||||
|
|
||||||
import styles from './playground.module.css'
|
import styles from './playground.module.css'
|
||||||
import Image from 'next/image'
|
import Image from 'next/image'
|
||||||
@ -31,6 +32,7 @@ export default function Playground() {
|
|||||||
const testVar = process.env.NEXT_PUBLIC_TEST
|
const testVar = process.env.NEXT_PUBLIC_TEST
|
||||||
const converterUrl = process.env.NEXT_PUBLIC_CONVERTER_API_URL
|
const converterUrl = process.env.NEXT_PUBLIC_CONVERTER_API_URL
|
||||||
const { getMessage } = useMessage()
|
const { getMessage } = useMessage()
|
||||||
|
const { swalFire } = useSwal()
|
||||||
|
|
||||||
const [color, setColor] = useState('#ff0000')
|
const [color, setColor] = useState('#ff0000')
|
||||||
|
|
||||||
@ -103,6 +105,13 @@ export default function Playground() {
|
|||||||
date: '',
|
date: '',
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
const handleSwalAlert = () => {
|
||||||
|
swalFire({
|
||||||
|
text: '알림 테스트입니다.',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="container mx-auto p-4 m-4 border">
|
<div className="container mx-auto p-4 m-4 border">
|
||||||
@ -151,6 +160,24 @@ export default function Playground() {
|
|||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
<div>
|
||||||
|
<Button onClick={() => swalFire({ text: 'alert 테스트입니다.' })}>Sweetalert - alert</Button>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<Button
|
||||||
|
onClick={() =>
|
||||||
|
swalFire({
|
||||||
|
html: `confirm 테스트입니다.<br />당신은 바보입니까?`,
|
||||||
|
type: 'confirm',
|
||||||
|
confirmFn: () => {
|
||||||
|
alert('test')
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
>
|
||||||
|
Sweetalert - confirm
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
|||||||
38
src/hooks/useSwal.js
Normal file
38
src/hooks/useSwal.js
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import Swal from 'sweetalert2'
|
||||||
|
import withReactContent from 'sweetalert2-react-content'
|
||||||
|
|
||||||
|
export const useSwal = () => {
|
||||||
|
const MySwal = withReactContent(Swal)
|
||||||
|
|
||||||
|
const swalFire = ({ title = '', text = '', html = '', type = 'alert', confirmFn = () => {}, denyFn = () => {} }) => {
|
||||||
|
if (type === 'alert') {
|
||||||
|
MySwal.fire({
|
||||||
|
title,
|
||||||
|
text,
|
||||||
|
icon: 'success',
|
||||||
|
confirmButtonText: '확인',
|
||||||
|
})
|
||||||
|
} else if (type === 'confirm') {
|
||||||
|
MySwal.fire({
|
||||||
|
title,
|
||||||
|
text,
|
||||||
|
html,
|
||||||
|
icon: 'question',
|
||||||
|
showCloseButton: true,
|
||||||
|
showCancelButton: true,
|
||||||
|
confirmButtonText: '확인',
|
||||||
|
cancelButtonText: '취소',
|
||||||
|
}).then((result) => {
|
||||||
|
if (result.isConfirmed) {
|
||||||
|
confirmFn()
|
||||||
|
} else if (result.isDenied) {
|
||||||
|
denyFn()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
swalFire,
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user