aboutsummaryrefslogtreecommitdiff
path: root/src/tools/benchmark-builder/dynamic-values.vue
blob: 5e349fc89e8eba8e2607414c027aa2616fb64e1e (plain) (blame)
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
<template>
  <div>
    <div v-for="(value, index) of values" :key="index" mb-2 flex flex-nowrap gap-2>
      <n-input-number
        :ref="refs.set"
        v-model:value="values[index]"
        :show-button="false"
        placeholder="Set your measure..."
        autofocus
        @keydown.enter="onInputEnter(index)"
      />
      <n-tooltip>
        <template #trigger>
          <c-button circle variant="text" @click="values.splice(index, 1)">
            <n-icon :component="Trash" depth="3" size="18" />
          </c-button>
        </template>
        Delete value
      </n-tooltip>
    </div>

    <c-button @click="addValue">
      <n-icon :component="Plus" depth="3" mr-2 size="18" />
      Add a measure
    </c-button>
  </div>
</template>

<script setup lang="ts">
import { Trash, Plus } from '@vicons/tabler';
import { useTemplateRefsList, useVModel } from '@vueuse/core';
import { NInputNumber } from 'naive-ui';
import { nextTick } from 'vue';

const refs = useTemplateRefsList<typeof NInputNumber>();

const props = defineProps<{ values: (number | null)[] }>();
const emit = defineEmits(['update:values']);
const values = useVModel(props, 'values', emit);

async function addValue() {
  values.value.push(null);
  await nextTick();
  refs.value.at(-1)?.focus();
}

function onInputEnter(index: number) {
  if (index === values.value.length - 1) {
    addValue();
    return;
  }

  refs.value.at(index + 1)?.focus();
}
</script>

<style scoped></style>