yunohost-portal/components/TextInputList.vue

62 lines
1.3 KiB
Vue
Raw Permalink 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
buttonLabel: string
2023-07-26 04:01:12 +02:00
type: HTMLInputElement['type']
2023-11-14 19:17:04 +01:00
placeholder?: string
2023-07-26 04:01:12 +02:00
}>()
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"
2023-11-08 19:07:16 +01:00
icon="delete-forever"
2023-08-07 16:39:06 +02:00
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
<YButton :text="buttonLabel" @click="onAdd" />
2023-08-07 16:39:06 +02:00
</fieldset>
2023-07-26 04:01:12 +02:00
</template>