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="text-xl mb-3">{{ 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
>
2023-08-28 14:42:54 +02:00
<div class="join w-full">
2023-08-07 16:39:06 +02:00
<TextInput
:name="`${name}[${idx}]`"
:type="type"
:placeholder="placeholder"
2023-08-28 14:42:54 +02:00
class="join-item w-full"
2023-08-07 16:39:06 +02:00
/>
<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>