All files / src/components ConfirmModal.vue

94.11% Statements 32/34
90.47% Branches 19/21
80% Functions 8/10
92.85% Lines 26/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127    252x               5x 5x   5x                       5x 5x   5x                       5x                                               222x                     222x           222x   222x 235x   13x       222x         222x 26x 5x 2x     222x 26x 5x     222x           222x             1x       3x      
<template>
  <div
    v-if="isOpen"
    class="modal fade show d-block"
    role="dialog"
    aria-modal="true"
    tabindex="-1"
    style="background-color: rgba(0, 0, 0, 0.5)"
    @click.self="cancel"
  >
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header" :class="headerClass">
          <h5 class="modal-title">
            <font-awesome-icon :icon="icon" class="me-2" />
            {{ title }}
          </h5>
          <button
            type="button"
            class="btn-close"
            :class="{ 'btn-close-white': variant === 'danger' }"
            :aria-label="t('common.close')"
            @click="cancel"
          ></button>
        </div>
        <div class="modal-body">
          <p class="mb-0">{{ message }}</p>
          <div v-if="confirmText" class="mt-3">
            <label class="form-label small text-muted">
              {{ t('common.typeToConfirm').replace('{name}', confirmText) }}
            </label>
            <input
              v-model="typedValue"
              type="text"
              class="form-control"
              :placeholder="confirmText"
              @keydown.enter="typedValue === confirmText && confirm()"
            />
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" @click="cancel">
            <font-awesome-icon icon="fa-solid fa-xmark" class="me-2" />
            {{ t('common.cancel') }}
          </button>
          <button
            type="button"
            :class="confirmBtnClass"
            :disabled="!!confirmText && typedValue !== confirmText"
            @click="confirm"
          >
            <font-awesome-icon :icon="confirmIcon" class="me-2" />
            {{ confirmLabel ?? t('common.confirm') }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import { useUILanguage } from '@/composables/useUILanguage'
 
const { t } = useUILanguage()
 
interface Props {
  isOpen: boolean
  title: string
  message: string
  confirmLabel?: string
  confirmText?: string
  variant?: 'danger' | 'warning' | 'primary'
}
 
const props = withDefaults(defineProps<Props>(), {
  confirmLabel: undefined,
  confirmText: undefined,
  variant: 'danger'
})
 
const typedValue = ref('')
 
watch(
  () => props.isOpen,
  (open) => {
    if (!open) typedValue.value = ''
  }
)
 
const emit = defineEmits<{
  confirm: []
  cancel: []
}>()
 
const icon = computed(() => {
  if (props.variant === 'danger') return 'fa-solid fa-triangle-exclamation'
  if (props.variant === 'warning') return 'fa-solid fa-exclamation-circle'
  return 'fa-solid fa-question-circle'
})
 
const confirmIcon = computed(() => {
  if (props.variant === 'danger') return 'fa-solid fa-trash'
  return 'fa-solid fa-check'
})
 
const headerClass = computed(() => ({
  'bg-danger text-white': props.variant === 'danger',
  'bg-warning': props.variant === 'warning',
  'bg-primary text-white': props.variant === 'primary'
}))
 
const confirmBtnClass = computed(() => ({
  'btn btn-danger': props.variant === 'danger',
  'btn btn-warning': props.variant === 'warning',
  'btn btn-primary': props.variant === 'primary'
}))
 
function confirm() {
  emit('confirm')
}
 
function cancel() {
  emit('cancel')
}
</script>