|
| 1 | +<template> |
| 2 | + <!-- To be able to scroll to this field on error--> |
| 3 | + <div |
| 4 | + :id="`${props.formId}-${props.name}`" |
| 5 | + class="pkpFormField pkpFormField--creditRoles" |
| 6 | + > |
| 7 | + <div class="pkpFormField__heading"> |
| 8 | + <label :id="labelId" class="pkpFormFieldLabel"> |
| 9 | + {{ t('submission.submit.creditRoles.title', {}) }} |
| 10 | + </label> |
| 11 | + </div> |
| 12 | + <div :id="descriptionId" class="pkpFormField__description"> |
| 13 | + {{ t('submission.submit.creditRoles.description', {}) }} |
| 14 | + </div> |
| 15 | + <div class="pkpFormField__control pkpFormField--creditRoles__control"> |
| 16 | + <PkpTable :labelled-by="labelId" :described-by="descriptionId"> |
| 17 | + <TableHeader> |
| 18 | + <TableColumn id="" class="w-[45%]"> |
| 19 | + {{ t('submission.submit.creditRoles.role', {}) }} |
| 20 | + </TableColumn> |
| 21 | + <TableColumn id=""> |
| 22 | + {{ t('submission.submit.creditRoles.degree', {}) }} |
| 23 | + </TableColumn> |
| 24 | + <TableColumn id="" class="w-[100px]"> </TableColumn> |
| 25 | + </TableHeader> |
| 26 | + <TableBody> |
| 27 | + <TableRow |
| 28 | + v-for="({role, degree}, creditRoleIndex) in currentValue" |
| 29 | + :key="creditRoleIndex" |
| 30 | + > |
| 31 | + <TableCell> |
| 32 | + <FieldSelect |
| 33 | + name="role" |
| 34 | + :label="t('submission.submit.creditRoles.selectRole')" |
| 35 | + :is-required="true" |
| 36 | + :value="role" |
| 37 | + :options="props.options.roles" |
| 38 | + class="creditRole__roleSelect" |
| 39 | + @change=" |
| 40 | + (fieldName, propName, newValue, localeKey) => |
| 41 | + updateCreditRole(creditRoleIndex, fieldName, newValue) |
| 42 | + " |
| 43 | + /> |
| 44 | + </TableCell> |
| 45 | + <TableCell> |
| 46 | + <FieldSelect |
| 47 | + name="degree" |
| 48 | + :label="t('submission.submit.creditRoles.selectDegree')" |
| 49 | + :value="degree" |
| 50 | + :options="props.options.degrees" |
| 51 | + class="creditRole__roleSelect" |
| 52 | + @change=" |
| 53 | + (fieldName, propName, newValue, localeKey) => |
| 54 | + updateCreditRole(creditRoleIndex, fieldName, newValue) |
| 55 | + " |
| 56 | + /> |
| 57 | + </TableCell> |
| 58 | + <TableCell> |
| 59 | + <PkpButton |
| 60 | + :is-warnable="true" |
| 61 | + @click="removeCreditRole(creditRoleIndex)" |
| 62 | + > |
| 63 | + {{ t('submission.submit.creditRoles.button.remove') }} |
| 64 | + </PkpButton> |
| 65 | + </TableCell> |
| 66 | + </TableRow> |
| 67 | + </TableBody> |
| 68 | + <template #bottom-controls> |
| 69 | + <PkpButton @click="addCreditRole()"> |
| 70 | + {{ t('submission.submit.creditRoles.button.add') }} |
| 71 | + </PkpButton> |
| 72 | + </template> |
| 73 | + </PkpTable> |
| 74 | + </div> |
| 75 | + </div> |
| 76 | +</template> |
| 77 | + |
| 78 | +<script setup> |
| 79 | +import {computed, useId} from 'vue'; |
| 80 | +import {t} from '@/utils/i18n'; |
| 81 | +import PkpButton from '@/components/Button/Button.vue'; |
| 82 | +import PkpTable from '@/components/Table/Table.vue'; |
| 83 | +import TableHeader from '@/components/Table/TableHeader.vue'; |
| 84 | +import TableBody from '@/components/Table/TableBody.vue'; |
| 85 | +import TableRow from '@/components/Table/TableRow.vue'; |
| 86 | +import TableColumn from '@/components/Table/TableColumn.vue'; |
| 87 | +import TableCell from '@/components/Table/TableCell.vue'; |
| 88 | +import FieldSelect from './FieldSelect.vue'; |
| 89 | +
|
| 90 | +const props = defineProps({ |
| 91 | + /** Field key used for form submission */ |
| 92 | + name: { |
| 93 | + type: String, |
| 94 | + default: null, |
| 95 | + }, |
| 96 | + /** The ID of the form this field should appear in. This is passed down from the `Form`. */ |
| 97 | + formId: { |
| 98 | + type: String, |
| 99 | + default: null, |
| 100 | + }, |
| 101 | + /** Current value of the field */ |
| 102 | + value: { |
| 103 | + type: Array, |
| 104 | + default: () => [], |
| 105 | + }, |
| 106 | + options: { |
| 107 | + type: Object, |
| 108 | + required: true, |
| 109 | + }, |
| 110 | +}); |
| 111 | +
|
| 112 | +/** |
| 113 | + * Accessibility |
| 114 | + */ |
| 115 | +const labelId = useId(); |
| 116 | +const descriptionId = useId(); |
| 117 | +
|
| 118 | +const emit = defineEmits(['change']); |
| 119 | +
|
| 120 | +const currentValue = computed({ |
| 121 | + get: () => props.value, |
| 122 | + set: (newVal) => emit('change', props.name, 'value', newVal), |
| 123 | +}); |
| 124 | +
|
| 125 | +function addCreditRole() { |
| 126 | + currentValue.value.push({ |
| 127 | + role: props.options.roles[0].value, |
| 128 | + degree: props.options.degrees[0].value, |
| 129 | + }); |
| 130 | +} |
| 131 | +
|
| 132 | +function removeCreditRole(index) { |
| 133 | + currentValue.value.splice(index, 1); |
| 134 | +} |
| 135 | +
|
| 136 | +function updateCreditRole(index, fieldName, newValue) { |
| 137 | + currentValue.value[index][fieldName] = newValue; |
| 138 | +} |
| 139 | +</script> |
0 commit comments