All files / src/components ConfirmModal.vue

94.28% Statements 33/35
90.32% Branches 28/31
81.81% Functions 9/11
93.1% Lines 27/29

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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144    1398x           2x   34x 34x             34x                         34x 34x   34x                           34x                                                           1163x                         1163x             1163x   1163x 1237x   74x       1163x         1163x 76x 23x 4x     1163x 73x 23x     1163x           1163x             9x       6x      
<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="!busy && cancel()"
  >
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <!-- On yellow, dark text and close cross, in the dark theme too -->
        <div
          class="modal-header"
          :class="headerClass"
          :data-bs-theme="variant === 'warning' ? 'light' : undefined"
        >
          <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')"
            :disabled="busy"
            @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"
              name="confirm-text"
              :disabled="busy"
              @keydown.enter="!busy && typedValue === confirmText && confirm()"
            />
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" :disabled="busy" @click="cancel">
            <font-awesome-icon icon="fa-solid fa-xmark" class="me-2" />
            {{ t('common.cancel') }}
          </button>
          <button
            type="button"
            :class="confirmBtnClass"
            :disabled="busy || (!!confirmText && typedValue !== confirmText)"
            @click="confirm"
          >
            <span
              v-if="busy"
              class="spinner-border spinner-border-sm me-2"
              role="status"
              aria-hidden="true"
            />
            <font-awesome-icon v-else :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'
  /** The confirmed action is running: spinner on the confirm button, nothing can be clicked */
  busy?: boolean
}
 
const props = withDefaults(defineProps<Props>(), {
  confirmLabel: undefined,
  confirmText: undefined,
  variant: 'danger',
  busy: false
})
 
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-triangle-exclamation'
  return 'fa-solid fa-check'
})
 
const headerClass = computed(() => ({
  'bg-danger text-white': props.variant === 'danger',
  'bg-warning text-dark': 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>