yunohost-portal/components/TextInputList.vue
2023-08-07 16:39:06 +02:00

60 lines
1.3 KiB
Vue

<script setup lang="ts">
import { useFieldArray } from 'vee-validate'
const props = defineProps<{
name: string
label: string
inputLabel: string
type: HTMLInputElement['type']
placeholder?: string
}>()
const group: Ref<HTMLElement | null> = ref(null)
const { remove, push, fields } = useFieldArray(props.name)
function onAdd() {
push('')
nextTick(() => {
// auto focus new input
const inputs = group.value?.querySelectorAll('input')
if (inputs && inputs.length) {
inputs[inputs.length - 1].focus()
}
})
}
</script>
<template>
<fieldset ref="group">
<legend class="ms-1 mb-2">{{ label }}</legend>
<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>
<YButton :text="$t('add')" @click="onAdd" />
</fieldset>
</template>