Skip to content

useAsyncConfirm

Unified async confirm hook.

Parameters

NameTypeDescription
confirmServicefunctionAsync confirm service.
optionsAsyncConfirmOptionsConfirm options.

AsyncConfirmOptions

NameTypeDefaultDescription
titlestringConfirmTitle of the confirm dialog.
messagestringSure ConfirmMessage of the confirm dialog.
typeMessageTypeerrorType of the confirm dialog("primary"、"success"、"info"、"warning"、"error").
confirmButtonTextstringSureText of the confirm button.
cancelButtonTextstringCancelText of the cancel button.
successMessagestringundefinedSuccess message to show after async confirm success.
errorMessagestringundefinedError message to show after async confirm error.
confirmSuccess() => voidundefinedCallback function when confirm is success.
confirmError(error: unknown) => voidundefinedCallback function when confirm is failed.

Returns

NameTypeDescription
confirmconfirm(params?: unknown)Confirm function. params is passed to confirmService.
loadingRef<boolean>Loading state

Basic Usage

vue
<script setup lang="ts">
import { useAsyncConfirm } from '@vuecraft/components'
import axios from 'axios'

async function deleteItem() {
  return await axios.delete('/api/items/1')
}

const { confirm, loading } = useAsyncConfirm(deleteItem, {
  title: 'Delete Item',
  message: 'Are you sure you want to delete this item?',
  type: 'error',
  successMessage: 'Deleted successfully',
})
</script>

<template>
  <button :loading="loading" @click="confirm()">
    {{ loading ? 'Loading...' : 'Delete' }}
  </button>
</template>

Released under the MIT License.