|
| 1 | +<template> |
| 2 | + <div |
| 3 | + :id="`${props.formId}-${props.name}`" |
| 4 | + class="pkpFormField pkpFormField--authors" |
| 5 | + > |
| 6 | + <div class="pkpFormField__heading"> |
| 7 | + <label :id="labelId" class="pkpFormFieldLabel"> |
| 8 | + {{ props.label }} |
| 9 | + </label> |
| 10 | + </div> |
| 11 | + <div :id="descriptionId" class="pkpFormField__description"> |
| 12 | + {{ props.description }} |
| 13 | + </div> |
| 14 | + <PkpTable :labelled-by="labelId" :described-by="descriptionId"> |
| 15 | + <TableHeader> |
| 16 | + <TableColumn id="">{{ t('user.givenName', []) }}</TableColumn> |
| 17 | + <TableColumn id="">{{ t('user.familyName', []) }}</TableColumn> |
| 18 | + <TableColumn id="">{{ t('user.orcid', []) }}</TableColumn> |
| 19 | + <TableColumn id="" class="w-[100px]"> </TableColumn> |
| 20 | + </TableHeader> |
| 21 | + <TableBody> |
| 22 | + <TableRow v-for="(row, index) in currentValue" :key="index"> |
| 23 | + <TableCell> |
| 24 | + <FieldText |
| 25 | + :name="'givenName'" |
| 26 | + :value="row.givenName" |
| 27 | + @change=" |
| 28 | + (fieldName, _, fieldValue) => { |
| 29 | + updateRow(index, fieldName, fieldValue); |
| 30 | + } |
| 31 | + " |
| 32 | + :all-errors="{givenName: errors?.[index]?.givenName}" |
| 33 | + /> |
| 34 | + </TableCell> |
| 35 | + <TableCell> |
| 36 | + <FieldText |
| 37 | + :name="'familyName'" |
| 38 | + :value="row.familyName" |
| 39 | + @change=" |
| 40 | + (fieldName, _, fieldValue) => { |
| 41 | + updateRow(index, fieldName, fieldValue); |
| 42 | + } |
| 43 | + " |
| 44 | + :all-errors="{familyName: errors?.[index]?.familyName}" |
| 45 | + /> |
| 46 | + </TableCell> |
| 47 | + <TableCell> |
| 48 | + <FieldText |
| 49 | + :name="'orcid'" |
| 50 | + :value="row.orcid" |
| 51 | + @change=" |
| 52 | + (fieldName, _, fieldValue) => { |
| 53 | + updateRow(index, fieldName, fieldValue); |
| 54 | + } |
| 55 | + " |
| 56 | + :all-errors="{orcid: errors?.[index]?.orcid}" |
| 57 | + /> |
| 58 | + </TableCell> |
| 59 | + <TableCell> |
| 60 | + <PkpButton :is-link="true" @click="deleteRow(index)"> |
| 61 | + {{ t('common.delete', []) }} |
| 62 | + </PkpButton> |
| 63 | + </TableCell> |
| 64 | + </TableRow> |
| 65 | + </TableBody> |
| 66 | + <template #bottom-controls> |
| 67 | + <PkpButton @click="addRow()"> |
| 68 | + {{ addButtonLabel ? addButtonLabel : t('common.add') }} |
| 69 | + </PkpButton> |
| 70 | + </template> |
| 71 | + </PkpTable> |
| 72 | + </div> |
| 73 | +</template> |
| 74 | + |
| 75 | +<script setup> |
| 76 | +import {computed, useId} from 'vue'; |
| 77 | +import {useLocalize} from '@/composables/useLocalize'; |
| 78 | +import PkpButton from '@/components/Button/Button.vue'; |
| 79 | +import PkpTable from '@/components/Table/Table.vue'; |
| 80 | +import TableCell from '@/components/Table/TableCell.vue'; |
| 81 | +import TableRow from '@/components/Table/TableRow.vue'; |
| 82 | +import TableColumn from '@/components/Table/TableColumn.vue'; |
| 83 | +import TableHeader from '@/components/Table/TableHeader.vue'; |
| 84 | +import TableBody from '@/components/Table/TableBody.vue'; |
| 85 | +import FieldText from '@/components/Form/fields/FieldText.vue'; |
| 86 | +
|
| 87 | +const {t} = useLocalize(); |
| 88 | +const props = defineProps({ |
| 89 | + /** Field key used for form submission */ |
| 90 | + name: { |
| 91 | + type: String, |
| 92 | + default: null, |
| 93 | + }, |
| 94 | + /** The ID of the form this field should appear in. This is passed down from the `Form`. */ |
| 95 | + formId: { |
| 96 | + type: String, |
| 97 | + default: null, |
| 98 | + }, |
| 99 | + /** The `<label>` for this field. May be used in a `<fieldset>` when appropriate. All form fields should have an accessible label. */ |
| 100 | + label: { |
| 101 | + type: String, |
| 102 | + default: null, |
| 103 | + }, |
| 104 | + /** Adds a description to the field. Can include HTML code. */ |
| 105 | + description: { |
| 106 | + type: String, |
| 107 | + default: null, |
| 108 | + }, |
| 109 | + /** Current value of the field */ |
| 110 | + value: { |
| 111 | + type: Array, |
| 112 | + default: () => [], |
| 113 | + }, |
| 114 | + /** Label for add button */ |
| 115 | + addButtonLabel: { |
| 116 | + type: String, |
| 117 | + default: null, |
| 118 | + }, |
| 119 | + /** Object containing all form errors */ |
| 120 | + allErrors: { |
| 121 | + type: Object, |
| 122 | + default() { |
| 123 | + return {}; |
| 124 | + }, |
| 125 | + }, |
| 126 | +}); |
| 127 | +const labelId = useId(); |
| 128 | +const descriptionId = useId(); |
| 129 | +const emit = defineEmits(['change', 'set-errors']); |
| 130 | +const errors = computed(() => { |
| 131 | + if (!Object.keys(props.allErrors).includes(props.name)) { |
| 132 | + return []; |
| 133 | + } |
| 134 | + return props.allErrors[props.name]; |
| 135 | +}); |
| 136 | +const rowDataModel = () => { |
| 137 | + return { |
| 138 | + givenName: '', |
| 139 | + familyName: '', |
| 140 | + orcid: '', |
| 141 | + }; |
| 142 | +}; |
| 143 | +const currentValue = computed({ |
| 144 | + get: () => props.value, |
| 145 | + set: (newVal) => emit('change', props.name, 'value', newVal), |
| 146 | +}); |
| 147 | +
|
| 148 | +function deleteRow(index) { |
| 149 | + currentValue.value.splice(index, 1); |
| 150 | +} |
| 151 | +
|
| 152 | +function addRow() { |
| 153 | + currentValue.value.push(rowDataModel()); |
| 154 | +} |
| 155 | +
|
| 156 | +function updateRow(rowIndex, fieldName, fieldValue) { |
| 157 | + currentValue.value = currentValue.value.map((row, index) => { |
| 158 | + if (index !== rowIndex) { |
| 159 | + return row; |
| 160 | + } |
| 161 | + return { |
| 162 | + ...row, |
| 163 | + [fieldName]: fieldValue, |
| 164 | + }; |
| 165 | + }); |
| 166 | +} |
| 167 | +</script> |
0 commit comments