|
| 1 | +<script lang="ts"> |
| 2 | + import { PlusIcon, XIcon } from "lucide-svelte"; |
| 3 | + import { tick } from "svelte"; |
| 4 | + import InputLabel from "./InputLabel.svelte"; |
| 5 | +
|
| 6 | + export let id: string; |
| 7 | + export let label = ""; |
| 8 | + export let hint: string | undefined = undefined; |
| 9 | + export let optional = false; |
| 10 | + export let value: Array<{ key: string; value: string }> = []; |
| 11 | + export let keyPlaceholder = "Header name"; |
| 12 | + export let valuePlaceholder = "Value"; |
| 13 | +
|
| 14 | + let keyInputs: HTMLInputElement[] = []; |
| 15 | +
|
| 16 | + // Defensive: coerce to array if value is undefined or wrong type |
| 17 | + $: entries = Array.isArray(value) ? value : []; |
| 18 | + $: duplicateKeys = findDuplicateKeys(entries); |
| 19 | +
|
| 20 | + function findDuplicateKeys( |
| 21 | + entries: Array<{ key: string; value: string }>, |
| 22 | + ): Set<string> { |
| 23 | + const seen = new Set<string>(); |
| 24 | + const dupes = new Set<string>(); |
| 25 | + for (const entry of entries) { |
| 26 | + const k = entry.key.trim(); |
| 27 | + if (!k) continue; |
| 28 | + if (seen.has(k)) dupes.add(k); |
| 29 | + seen.add(k); |
| 30 | + } |
| 31 | + return dupes; |
| 32 | + } |
| 33 | +
|
| 34 | + async function addRow() { |
| 35 | + value = [...value, { key: "", value: "" }]; |
| 36 | + await tick(); |
| 37 | + keyInputs[value.length - 1]?.focus(); |
| 38 | + } |
| 39 | +
|
| 40 | + function removeRow(index: number) { |
| 41 | + value = value.filter((_, i) => i !== index); |
| 42 | + } |
| 43 | +
|
| 44 | + function updateKey(index: number, newKey: string) { |
| 45 | + value[index] = { ...value[index], key: newKey }; |
| 46 | + value = value; |
| 47 | + } |
| 48 | +
|
| 49 | + function updateValue(index: number, newValue: string) { |
| 50 | + value[index] = { ...value[index], value: newValue }; |
| 51 | + value = value; |
| 52 | + } |
| 53 | +</script> |
| 54 | + |
| 55 | +<div class="flex flex-col gap-y-1"> |
| 56 | + {#if label} |
| 57 | + <InputLabel {id} {label} {hint} {optional} /> |
| 58 | + {/if} |
| 59 | + |
| 60 | + {#each entries as entry, i (i)} |
| 61 | + <div class="flex items-center gap-1.5"> |
| 62 | + <div class="kv-input-wrapper flex-1"> |
| 63 | + <input |
| 64 | + bind:this={keyInputs[i]} |
| 65 | + type="text" |
| 66 | + placeholder={keyPlaceholder} |
| 67 | + value={entry.key} |
| 68 | + on:input={(e) => updateKey(i, e.currentTarget.value)} |
| 69 | + aria-label="Header name {i + 1}" |
| 70 | + class="kv-input" |
| 71 | + /> |
| 72 | + </div> |
| 73 | + <div class="kv-input-wrapper flex-1"> |
| 74 | + <input |
| 75 | + type="text" |
| 76 | + placeholder={valuePlaceholder} |
| 77 | + value={entry.value} |
| 78 | + on:input={(e) => updateValue(i, e.currentTarget.value)} |
| 79 | + aria-label="Header value {i + 1}" |
| 80 | + class="kv-input" |
| 81 | + /> |
| 82 | + </div> |
| 83 | + <button |
| 84 | + type="button" |
| 85 | + class="remove-button" |
| 86 | + on:click={() => removeRow(i)} |
| 87 | + aria-label="Remove header {i + 1}" |
| 88 | + > |
| 89 | + <XIcon size="14px" /> |
| 90 | + </button> |
| 91 | + </div> |
| 92 | + {#if duplicateKeys.has(entry.key.trim())} |
| 93 | + <div class="text-xs text-amber-600"> |
| 94 | + Duplicate key "{entry.key.trim()}" — last value wins |
| 95 | + </div> |
| 96 | + {/if} |
| 97 | + {/each} |
| 98 | + |
| 99 | + <button type="button" class="add-button" on:click={addRow}> |
| 100 | + <PlusIcon size="14px" /> |
| 101 | + Add header |
| 102 | + </button> |
| 103 | +</div> |
| 104 | + |
| 105 | +<style lang="postcss"> |
| 106 | + .kv-input-wrapper { |
| 107 | + @apply border rounded-[2px] bg-input px-2; |
| 108 | + @apply flex items-center; |
| 109 | + height: 30px; |
| 110 | + } |
| 111 | +
|
| 112 | + .kv-input-wrapper:focus-within { |
| 113 | + @apply border-primary-500 ring-2 ring-primary-100; |
| 114 | + } |
| 115 | +
|
| 116 | + .kv-input { |
| 117 | + @apply bg-transparent outline-none border-0; |
| 118 | + @apply text-xs placeholder-fg-muted; |
| 119 | + @apply w-full h-full; |
| 120 | + } |
| 121 | +
|
| 122 | + .remove-button { |
| 123 | + @apply text-fg-muted hover:text-fg-primary; |
| 124 | + @apply flex-none flex items-center justify-center; |
| 125 | + @apply cursor-pointer; |
| 126 | + width: 24px; |
| 127 | + height: 30px; |
| 128 | + } |
| 129 | +
|
| 130 | + .add-button { |
| 131 | + @apply flex items-center gap-1; |
| 132 | + @apply text-xs text-primary-500 hover:text-primary-600 font-medium; |
| 133 | + @apply cursor-pointer w-fit mt-0.5; |
| 134 | + } |
| 135 | +</style> |
0 commit comments