yunohost-portal/components/TextInputList.vue

61 lines
1.3 KiB
Vue
Raw Normal View History

2023-07-26 04:01:12 +02:00
<script setup lang="ts">
import { useFieldArray } from 'vee-validate'
const props = defineProps<{
name: string
2023-08-07 16:39:06 +02:00
label: string
inputLabel: string
2023-07-26 04:01:12 +02:00
type: HTMLInputElement['type']
placeholder?: string
}>()
2023-08-07 16:39:06 +02:00
const group: Ref<HTMLElement | null> = ref(null)
2023-07-26 04:01:12 +02:00
const { remove, push, fields } = useFieldArray(props.name)
2023-08-07 16:39:06 +02:00
function onAdd() {
push('')
nextTick(() => {
// auto focus new input
const inputs = group.value?.querySelectorAll('input')
if (inputs && inputs.length) {
inputs[inputs.length - 1].focus()
}
})
}
2023-07-26 04:01:12 +02:00
</script>
<template>
2023-08-07 16:39:06 +02:00
<fieldset ref="group">
<legend class="ms-1 mb-2">{{ label }}</legend>
2023-07-26 04:01:12 +02:00
2023-08-07 16:39:06 +02:00
<FormField
v-for="(field, idx) in fields"
:key="field.key"
:name="`${name}[${idx}]`"
:label="inputLabel"
class="mb-3"
sr-hide-label
>
<div class="join">
<TextInput
:name="`${name}[${idx}]`"
:type="type"
:placeholder="placeholder"
class="join-item"
/>
<YButton
variant="error"
icon="mdi:delete-forever"
icon-size="2em"
icon-only
:text="$t('remove')"
class="join-item px-3"
@click="remove(idx)"
/>
</div>
</FormField>
2023-07-26 04:01:12 +02:00
2023-08-07 16:39:06 +02:00
<YButton :text="$t('add')" @click="onAdd" />
</fieldset>
2023-07-26 04:01:12 +02:00
</template>