|
| 1 | +<script setup lang="ts"> |
| 2 | +import { ref, watch, computed } from 'vue'; |
| 3 | +import { QInput, QBtn, QIcon } from 'quasar'; |
| 4 | +import { useFormField, generateFieldId, useFormContext } from '@quickflo/quickforms-vue'; |
| 5 | +import type { FieldProps } from '@quickflo/quickforms-vue'; |
| 6 | +
|
| 7 | +const props = withDefaults(defineProps<FieldProps>(), { |
| 8 | + disabled: false, |
| 9 | + readonly: false, |
| 10 | +}); |
| 11 | +
|
| 12 | +const { value, setValue, label, hint, errorMessage, required } = useFormField( |
| 13 | + props.path, |
| 14 | + props.schema, |
| 15 | + { label: props.label } |
| 16 | +); |
| 17 | +
|
| 18 | +const formContext = useFormContext(); |
| 19 | +const fieldId = generateFieldId(props.path); |
| 20 | +
|
| 21 | +// Merge QuickForms convenience features for button customization |
| 22 | +const quickformsFeatures = computed(() => { |
| 23 | + const globalDefaults = (formContext as any)?.quickformsDefaults?.keyvalue || {}; |
| 24 | + const schemaFeatures = (props.schema as any)["x-quickforms-quasar"] || {}; |
| 25 | +
|
| 26 | + // Merge QBtn props: defaults -> global -> schema (schema has highest priority) |
| 27 | + const addButtonDefaults = { |
| 28 | + outline: true, |
| 29 | + color: "primary", |
| 30 | + icon: "add", |
| 31 | + label: formContext?.labels?.addItem || "Add Parameter", |
| 32 | + size: "sm", |
| 33 | + }; |
| 34 | +
|
| 35 | + const removeButtonDefaults = { |
| 36 | + flat: true, |
| 37 | + round: true, |
| 38 | + dense: true, |
| 39 | + size: "sm", |
| 40 | + icon: "close", |
| 41 | + color: "negative", |
| 42 | + }; |
| 43 | +
|
| 44 | + const addButton = { |
| 45 | + ...addButtonDefaults, |
| 46 | + ...(globalDefaults.addButton || {}), |
| 47 | + ...(schemaFeatures.addButton || {}), |
| 48 | + }; |
| 49 | +
|
| 50 | + const removeButton = { |
| 51 | + ...removeButtonDefaults, |
| 52 | + ...(globalDefaults.removeButton || {}), |
| 53 | + ...(schemaFeatures.removeButton || {}), |
| 54 | + }; |
| 55 | +
|
| 56 | + return { |
| 57 | + addButton, |
| 58 | + removeButton, |
| 59 | + }; |
| 60 | +}); |
| 61 | +
|
| 62 | +// Convert object to array of key-value pairs for editing |
| 63 | +interface KeyValuePair { |
| 64 | + key: string; |
| 65 | + value: string; |
| 66 | + id: number; |
| 67 | +} |
| 68 | +
|
| 69 | +let nextId = 0; |
| 70 | +const pairs = ref<KeyValuePair[]>([]); |
| 71 | +const isInternalUpdate = ref(false); |
| 72 | +
|
| 73 | +// Initialize from value |
| 74 | +watch( |
| 75 | + () => value.value, |
| 76 | + (newValue) => { |
| 77 | + if (isInternalUpdate.value) { |
| 78 | + isInternalUpdate.value = false; |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + if (newValue && typeof newValue === 'object' && !Array.isArray(newValue)) { |
| 83 | + pairs.value = Object.entries(newValue).map(([key, val]) => ({ |
| 84 | + key, |
| 85 | + value: String(val), |
| 86 | + id: nextId++ |
| 87 | + })); |
| 88 | + } else if (!pairs.value.length) { |
| 89 | + pairs.value = []; |
| 90 | + } |
| 91 | + }, |
| 92 | + { immediate: true } |
| 93 | +); |
| 94 | +
|
| 95 | +// Update value when pairs change |
| 96 | +watch( |
| 97 | + pairs, |
| 98 | + (newPairs) => { |
| 99 | + const obj: Record<string, string> = {}; |
| 100 | + newPairs.forEach(pair => { |
| 101 | + if (pair.key.trim()) { |
| 102 | + obj[pair.key] = pair.value; |
| 103 | + } |
| 104 | + }); |
| 105 | + isInternalUpdate.value = true; |
| 106 | + setValue(obj); |
| 107 | + }, |
| 108 | + { deep: true } |
| 109 | +); |
| 110 | +
|
| 111 | +function addPair() { |
| 112 | + pairs.value.push({ key: '', value: '', id: nextId++ }); |
| 113 | +} |
| 114 | +
|
| 115 | +function removePair(id: number) { |
| 116 | + pairs.value = pairs.value.filter(p => p.id !== id); |
| 117 | +} |
| 118 | +</script> |
| 119 | + |
| 120 | +<template> |
| 121 | + <div class="quickform-keyvalue-field"> |
| 122 | + <div v-if="label" class="text-subtitle2 q-mb-xs"> |
| 123 | + {{ label }} |
| 124 | + <span v-if="required" style="color: red; margin-left: 0.25rem">*</span> |
| 125 | + </div> |
| 126 | + |
| 127 | + <div v-if="hint" class="text-caption text-grey-7 q-mb-sm"> |
| 128 | + {{ hint }} |
| 129 | + </div> |
| 130 | + |
| 131 | + <div class="q-pa-md rounded-borders"> |
| 132 | + <div v-if="pairs.length" class="row items-center q-gutter-sm q-mb-sm"> |
| 133 | + <div class="col text-weight-medium text-caption">Key</div> |
| 134 | + <div class="col text-weight-medium text-caption">Value</div> |
| 135 | + <div style="width: 40px"></div> |
| 136 | + </div> |
| 137 | + |
| 138 | + <div |
| 139 | + v-for="pair in pairs" |
| 140 | + :key="pair.id" |
| 141 | + class="row items-center q-gutter-sm q-mb-sm" |
| 142 | + > |
| 143 | + <QInput |
| 144 | + v-model="pair.key" |
| 145 | + outlined |
| 146 | + dense |
| 147 | + placeholder="key" |
| 148 | + class="col" |
| 149 | + :disable="disabled" |
| 150 | + :readonly="readonly" |
| 151 | + /> |
| 152 | + <QInput |
| 153 | + v-model="pair.value" |
| 154 | + outlined |
| 155 | + dense |
| 156 | + placeholder="value" |
| 157 | + class="col" |
| 158 | + :disable="disabled" |
| 159 | + :readonly="readonly" |
| 160 | + /> |
| 161 | + <QBtn |
| 162 | + v-bind="quickformsFeatures.removeButton" |
| 163 | + :disable="disabled || readonly" |
| 164 | + @click="removePair(pair.id)" |
| 165 | + :title="formContext?.labels?.removeItem || 'Remove'" |
| 166 | + > |
| 167 | + <q-tooltip>{{ |
| 168 | + formContext?.labels?.removeItem || "Remove" |
| 169 | + }}</q-tooltip> |
| 170 | + </QBtn> |
| 171 | + </div> |
| 172 | + |
| 173 | + <QBtn |
| 174 | + v-bind="quickformsFeatures.addButton" |
| 175 | + class="full-width" |
| 176 | + :disable="disabled || readonly" |
| 177 | + @click="addPair" |
| 178 | + /> |
| 179 | + </div> |
| 180 | + |
| 181 | + <div v-if="errorMessage" class="text-negative text-caption q-mt-xs"> |
| 182 | + {{ errorMessage }} |
| 183 | + </div> |
| 184 | + </div> |
| 185 | +</template> |
0 commit comments