All files / src/components/shared HorizontalSliderSelector.vue

100% Statements 18/18
100% Branches 2/2
100% Functions 9/9
100% Lines 18/18

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      1x                               5x               5x   5x                                           3x                         3x       3x 3x   3x   4x     1x       3x 4x   1x 1x 1x 1x       3x        
<template>
  <v-slider
    :style="`--thumbBackgroundColor: ${bgColor}`"
    v-model="sliderValue"
    :thumb-label="showThumb"
    :min="min"
    :max="max"
    direction="horizontal"
    thumb-color="blue"
    :thumb-size="16"
    hide-details
    data-testid="slider"
  >
    <template #append>
      <div
        class="y-1 my-0 text-center"
        :class="textClass"
        data-testid="max-value"
        >{{ max }}</div
      >
    </template>
    <template #prepend>
      <div
        class="py-1 my-0 text-center"
        :class="textClass"
        data-testid="min-value"
        >{{ min }}</div
      >
    </template>
    <template v-slot:thumb-label="{ modelValue }">
      <span>{{ Math.floor(modelValue) }}</span>
    </template>
  </v-slider>
</template>
 
<script setup lang="ts">
import { computed, ref, unref, watch } from 'vue';
 
export interface Props {
  value: number;
  loading?: boolean;
  label?: string;
  lower?: number;
  upper?: number;
  min?: number;
  max?: number;
  bgColor?: string;
  textClass?: string;
  hideInput?: boolean;
}
 
const props = withDefaults(defineProps<Props>(), {
  value: 0,
  loading: false,
  label: 'Test',
  lower: 0,
  upper: 100,
  min: -99999,
  max: 99999,
  bgColor: '#F7F7F8',
  textClass: 'text-white',
  hideInput: false,
});
 
const emit = defineEmits<{
  'update:value': [number];
}>();
 
const showThumb = ref<'always' | boolean>(true);
const showThumbTimeout = ref<number>();
 
const sliderValue = computed({
  get() {
    return Math.floor(props.value);
  },
  set(value: number) {
    emit('update:value', Math.floor(value));
  },
});
 
watch(
  () => sliderValue.value,
  () => {
    clearTimeout(unref(showThumbTimeout));
    showThumb.value = 'always';
    showThumbTimeout.value = setTimeout(() => {
      showThumb.value = false;
    }, 1000);
  },
);
defineExpose({
  showThumb,
});
</script>