All files / src/components TournamentLeagueToggle.vue

100% Statements 69/69
95.23% Branches 20/21
100% Functions 3/3
100% Lines 69/69

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 981x 1x   1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x   1x 1x 120x 120x       120x 120x 1x 1x 1x       1x 1x   1x                 1x   1x   1x 1x 97x 97x 1x   1x 1x 91x 91x 91x 91x 91x 91x 91x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 64x 64x 64x 64x 64x 64x 64x 64x 33x 33x 1x    
<template>
  <!-- Homegrown vs Academy (SB-1870): one segmented control, always on
       screen with the day strip, so the split is never behind a sheet.
       Offered only when the server lists the leagues. -->
  <div
    v-if="options.length > 0"
    role="group"
    aria-label="League"
    class="flex sm:inline-flex w-full sm:w-auto rounded-full border border-line bg-surface-alt/60 p-0.5"
    data-testid="tournament-league-toggle"
  >
    <button
      v-for="opt in options"
      :key="opt.key"
      type="button"
      :disabled="disabled"
      :aria-pressed="opt.value === selected ? 'true' : 'false'"
      :aria-label="opt.ariaLabel"
      :data-league="opt.key"
      data-testid="league-option"
      :class="[
        'flex-auto min-w-0 h-10 sm:h-9 px-2.5 sm:px-3.5 rounded-full whitespace-nowrap text-sm font-semibold inline-flex items-center justify-center gap-1.5 transition-colors',
        'focus:outline-none focus-visible:ring-2 focus-visible:ring-brand-500',
        opt.value === selected
          ? 'bg-brand-600 text-white shadow-sm'
          : 'text-fg-muted hover:text-fg',
      ]"
      @click="emit('update:league', opt.value)"
    >
      <span class="truncate">{{ opt.label }}</span>
      <span
        v-if="opt.count != null"
        :class="[
          'text-xs font-medium tabular-nums',
          opt.value === selected ? 'text-brand-100' : 'text-fg-muted',
        ]"
        aria-hidden="true"
        >{{ opt.count }}</span
      >
    </button>
  </div>
</template>
 
<script setup>
import { computed } from 'vue';
import { LEAGUES } from '../utils/tournamentDays';
 
const props = defineProps({
  // [{ league, match_count, team_count }]; null / [] hide the control.
  leagues: { type: Array, default: null },
  // 'Homegrown' | 'Academy' | null (both).
  selected: { type: String, default: null },
  // Matches under All, when known. Never summed from the leagues: a match
  // between a Homegrown and an Academy team is in both.
  allCount: { type: Number, default: null },
  disabled: { type: Boolean, default: false },
});
 
const emit = defineEmits(['update:league']);
 
const countOf = value => (typeof value === 'number' ? value : null);
const aria = (label, count) =>
  count == null
    ? label
    : `${label}, ${count} ${count === 1 ? 'match' : 'matches'}`;
 
// All first, then Homegrown, then Academy — whichever the server lists.
const options = computed(() => {
  const list = Array.isArray(props.leagues) ? props.leagues : [];
  // A league no team here is placed in is not a choice (an uncounted one,
  // `team_count` null, still is).
  const present = LEAGUES.map(name => list.find(l => l.league === name)).filter(
    l => l && l.team_count !== 0
  );
  if (present.length === 0) return [];
  const all = countOf(props.allCount);
  return [
    {
      key: 'all',
      value: null,
      label: 'All',
      count: all,
      ariaLabel: aria('All leagues', all),
    },
    ...present.map(l => {
      const count = countOf(l.match_count);
      return {
        key: l.league.toLowerCase(),
        value: l.league,
        label: l.league,
        count,
        ariaLabel: aria(l.league, count),
      };
    }),
  ];
});
</script>