qcast-front/src/hooks/useSwal.js
2024-09-27 16:10:36 +09:00

49 lines
1.2 KiB
JavaScript

import Swal from 'sweetalert2'
import withReactContent from 'sweetalert2-react-content'
/**
* title: 제목
* text: 내용
* html: html
* type: alert, confirm
* icon: icontype (success, error, warning, info, question)
* confirmFn: 확인 버튼 클릭 시 실행할 함수
* denyFn: 취소 버튼 클릭 시 실행할 함수
* @returns
*/
export const useSwal = () => {
const MySwal = withReactContent(Swal)
const swalFire = ({ title = '', text = '', html = '', type = 'alert', icon = '', confirmFn = () => {}, denyFn = () => {} }) => {
if (type === 'alert') {
MySwal.fire({
title,
text,
icon: icon === '' ? 'success' : icon,
confirmButtonText: '확인',
})
} else if (type === 'confirm') {
MySwal.fire({
title,
text,
html,
icon: icon === '' ? 'question' : icon,
showCloseButton: true,
showCancelButton: true,
confirmButtonText: '확인',
cancelButtonText: '취소',
}).then((result) => {
if (result.isConfirmed) {
confirmFn()
} else if (result.isDenied) {
denyFn()
}
})
}
}
return {
swalFire,
}
}