All files / src/components SystemItemCard.vue

96.87% Statements 31/32
96.96% Branches 64/66
100% Functions 14/14
100% Lines 27/27

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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281  184x 6x   843x                                           6x 6x                                                                                                                                 22x                                                             146x           146x 146x   146x 625x       1918x       75x       782x 1111x               262x       151x 151x 151x 76x         105x 105x 50x       33x 27x       2x     2x                                                                                                                                                                                                        
<template>
  <div class="card system-item-card h-100" data-testid="system-item-row">
    <div class="card-body d-flex align-items-center gap-2">
      <div class="system-item-row" :style="{ gridTemplateColumns: gridTemplateColumns }">
        <div v-for="field in fields" :key="field.fieldId" class="system-item-cell">
          <div
            v-if="!(field.type === 'chips' && field.chipsShowCount)"
            class="system-item-cell-label d-md-none"
          >
            {{ getTranslation(field.name) }}
          </div>
          <template v-if="field.type === 'text'">
            <div class="d-flex align-items-center gap-2">
              <div v-if="field.avatarFieldId" class="system-item-avatar-box">
                <img
                  v-if="getFieldValue(field.avatarFieldId)"
                  :src="getFieldValue(field.avatarFieldId)"
                  alt=""
                  class="system-item-avatar"
                />
                <font-awesome-icon
                  v-else-if="field.avatarFallbackIcon"
                  :icon="field.avatarFallbackIcon"
                  class="system-item-avatar-icon text-primary"
                />
              </div>
              <div class="min-width-0">
                <div class="text-truncate">{{ getTranslation(getFieldValue(field.fieldId)) }}</div>
                <small
                  v-if="field.secondaryFieldId && getFieldValue(field.secondaryFieldId)"
                  class="text-muted"
                >
                  {{ getTranslation(getFieldValue(field.secondaryFieldId)) }}
                </small>
              </div>
            </div>
          </template>
 
          <template v-else-if="field.type === 'date'">
            <template v-if="formatDate(getFieldValue(field.fieldId))">
              <small>{{ formatDate(getFieldValue(field.fieldId)) }}</small>
            </template>
            <span v-else class="text-muted">{{ resolveEmptyLabel(field) }}</span>
          </template>
 
          <template v-else-if="field.type === 'boolean'">
            <span
              class="badge"
              :class="getFieldValue(field.fieldId) ? 'bg-success' : 'bg-secondary'"
            >
              {{ getFieldValue(field.fieldId) ? t('common.yes') : t('common.no') }}
            </span>
          </template>
 
          <template v-else-if="field.type === 'badge'">
            <span
              v-if="getBadgeOption(field)"
              class="badge"
              :class="badgeClass(getBadgeOption(field)!.color)"
            >
              {{ getTranslation(getBadgeOption(field)!.label) }}
            </span>
            <span
              v-else-if="!field.hideWhenNoMatch"
              class="badge"
              :class="badgeClass(field.badgeDefaultColor ?? 'secondary')"
            >
              {{ getFieldValue(field.fieldId) }}
            </span>
          </template>
 
          <template v-else-if="field.type === 'amount'">
            <span
              class="fw-bold amount"
              :class="getFieldValue(field.fieldId) < 0 ? 'text-danger' : 'text-success'"
            >
              {{ getFieldValue(field.fieldId) > 0 ? '+' : ''
              }}{{ getFieldValue(field.fieldId).toLocaleString() }}
            </span>
          </template>
 
          <template v-else-if="field.type === 'chips'">
            <div v-if="field.chipsShowCount" class="text-muted small mb-1">
              {{ getTranslation(field.name) }} ({{ normalizeChips(field).length }})
            </div>
            <span
              v-if="getVisibleChips(field).length === 0 && field.emptyLabel"
              class="text-muted fst-italic small"
            >
              {{ resolveEmptyLabel(field) }}
            </span>
            <div v-else class="d-flex flex-wrap gap-1">
              <span
                v-for="(chip, index) in getVisibleChips(field)"
                :key="index"
                class="badge chip"
                :class="badgeClass(chip.color ?? field.chipsDefaultColor ?? 'secondary')"
              >
                {{ chip.label }}
              </span>
              <span v-if="getChipOverflowCount(field) > 0" class="badge bg-light text-dark small">
                {{ chipsOverflowLabel(field) }}
              </span>
            </div>
          </template>
        </div>
      </div>
      <div v-if="$slots.actions" class="card-actions-column text-end">
        <slot name="actions" :item="item" />
      </div>
    </div>
    <!-- Full-width content under the row (e.g. an expanded breakdown): it brings
         its own spacing, so an empty slot takes no room -->
    <slot name="details" :item="item" />
  </div>
</template>
 
<script setup lang="ts">
import { computed } from 'vue'
import type { SystemField, SystemChipValue, SystemBadgeOption } from '@/types/systemField'
import { useUILanguage } from '@/composables/useUILanguage'
import { useDateFormat } from '@/composables/useDateFormat'
 
const props = defineProps<{
  fields: SystemField[]
  item: Record<string, any>
  getTranslation: (value: string | Record<string, string> | null | undefined) => string
}>()
 
const { t } = useUILanguage()
const { formatDate } = useDateFormat()
 
const gridTemplateColumns = computed(() =>
  props.fields.map((field) => `${field.width}fr`).join(' ')
)
 
function getFieldValue(fieldId: string): any {
  return props.item[fieldId]
}
 
function resolveEmptyLabel(field: SystemField): string {
  return field.emptyLabel ? props.getTranslation(field.emptyLabel) : '-'
}
 
function getBadgeOption(field: SystemField): SystemBadgeOption | undefined {
  const value = String(getFieldValue(field.fieldId))
  return field.badgeOptions?.find((option) => option.value === value)
}
 
/**
 * `text-bg-*` pairs the background with a contrasting text color: `bg-*`
 * alone leaves white text, unreadable on light colors such as info or warning
 */
function badgeClass(color: string): string {
  return `bg-${color} text-bg-${color}`
}
 
function normalizeChips(field: SystemField): SystemChipValue[] {
  const raw = getFieldValue(field.fieldId)
  Iif (!Array.isArray(raw)) return []
  return raw.map((entry) =>
    typeof entry === 'string' ? { label: entry, color: field.chipsDefaultColor } : entry
  )
}
 
function getVisibleChips(field: SystemField): SystemChipValue[] {
  const chips = normalizeChips(field)
  if (!field.chipsMaxVisible) return chips
  return chips.slice(0, field.chipsMaxVisible)
}
 
function getChipOverflowCount(field: SystemField): number {
  if (!field.chipsMaxVisible) return 0
  return Math.max(0, normalizeChips(field).length - field.chipsMaxVisible)
}
 
function chipsOverflowLabel(field: SystemField): string {
  const template = field.chipsOverflowLabel
    ? props.getTranslation(field.chipsOverflowLabel)
    : '+{n}'
  return template.replace('{n}', String(getChipOverflowCount(field)))
}
</script>
 
<style scoped>
.system-item-card {
  overflow: hidden;
  position: relative;
}
 
.system-item-card .card-body {
  padding-top: 1rem;
}
 
.system-item-row {
  flex: 1 1 auto;
  min-width: 0;
  display: grid;
  gap: 0.75rem;
  align-items: center;
}
 
.system-item-cell {
  min-width: 0;
  color: var(--bw-text);
}
 
.amount {
  white-space: nowrap;
}
 
/* Same fixed-size flex box as UserAvatar.vue — centers the fallback icon
   regardless of FontAwesome's own `width: var(--fa-width, 1.25em)` sizing,
   which doesn't line up with a square photo of the same font-size. */
.system-item-avatar-box {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: 0.25rem;
  overflow: hidden;
  flex-shrink: 0;
}
 
.system-item-avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
 
.system-item-avatar-icon {
  width: 70%;
  height: 70%;
}
 
.min-width-0 {
  min-width: 0;
}
 
.chip {
  font-weight: 400;
}
 
.card-actions-column {
  flex: 0 0 8rem;
  display: flex;
  justify-content: flex-end;
  gap: 4px;
}
 
.system-item-cell-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--bw-text-muted);
  margin-bottom: 0.15rem;
}
 
@media (max-width: 767px) {
  .system-item-row {
    display: block;
  }
 
  .system-item-cell {
    margin-bottom: 0.75rem;
  }
 
  .system-item-cell:last-child {
    margin-bottom: 0;
  }
 
  .card-actions-column {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
  }
}
</style>