All files / Components PromptForm.vue

100% Statements 47/47
96.77% Branches 60/62
100% Functions 20/20
100% Lines 44/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199        19x                 19x   3x   19x       14x     19x   158x   19x 69x     19x 7x   7x 2x     5x     19x 2x     19x 5x 3x 3x 3x 2x 1x       19x 7x 1x 1x     7x             7x   7x   1x           184x                   1x                                 2x                               2x                               3x   198x                   1x             7x         1x             379x         2x             3x           19x                      
<script setup>
import { Link, useForm, usePage } from '@inertiajs/vue3';
import { computed, ref } from 'vue';
 
const props = defineProps({
    initial: { type: Object, required: true },
    suggestedTags: { type: Array, required: true },
    method: { type: String, default: 'post' },
    action: { type: String, required: true },
    submitLabel: { type: String, default: 'Submit' },
    cancelHref: { type: String, default: '/prompts' },
});
 
const page = usePage();
 
const PLACEHOLDER = '{{variable_name}}';
 
const form = useForm({
    name: props.initial.name ?? '',
    description: props.initial.description ?? '',
    body: props.initial.body ?? '',
    tags: (props.initial.tags ?? []).map((tag) => String(tag)),
});
 
const tagInput = ref('');
 
const errors = computed(() => page.props.errors ?? {});
 
const availableSuggestions = computed(() =>
    props.suggestedTags.filter((tag) => !form.tags.includes(tag))
);
 
const addTag = (raw) => {
    const value = String(raw ?? '').trim().toLowerCase();
 
    if (value === '' || form.tags.includes(value)) {
        return;
    }
 
    form.tags.push(value);
};
 
const removeTag = (index) => {
    form.tags.splice(index, 1);
};
 
const onTagKeydown = (event) => {
    if (event.key === 'Enter' || event.key === ',') {
        event.preventDefault();
        addTag(tagInput.value);
        tagInput.value = '';
    } else if (event.key === 'Backspace' && tagInput.value === '' && form.tags.length > 0) {
        removeTag(form.tags.length - 1);
    }
};
 
const submit = () => {
    if (tagInput.value.trim() !== '') {
        addTag(tagInput.value);
        tagInput.value = '';
    }
 
    form.transform(() => ({
        name: form.name,
        description: form.description?.trim() === '' ? null : form.description.trim(),
        body: form.body,
        tags: [...form.tags],
    }));
 
    const submitter = props.method === 'put' ? form.put.bind(form) : form.post.bind(form);
 
    submitter(props.action, {
        preserveScroll: true,
        onError: () => form.clearErrors(),
    });
};
</script>
 
<template>
    <form
        class="space-y-4 rounded-lg border border-slate-200 bg-white p-5 shadow-sm"
        @submit.prevent="submit"
    >
        <div>
            <label for="prompt-name" class="mb-1 block text-sm font-medium text-slate-700">
                Name
            </label>
            <input
                id="prompt-name"
                v-model="form.name"
                type="text"
                maxlength="120"
                class="w-full rounded-md border border-slate-300 px-3 py-2 text-sm shadow-sm focus:border-slate-500 focus:outline-none"
            />
            <p v-if="errors.name" class="mt-1 text-xs text-red-600">{{ errors.name }}</p>
        </div>
 
        <div>
            <label
                for="prompt-description"
                class="mb-1 block text-sm font-medium text-slate-700"
            >
                Description <span class="font-normal text-slate-400">(optional)</span>
            </label>
            <textarea
                id="prompt-description"
                v-model="form.description"
                rows="2"
                maxlength="500"
                class="w-full rounded-md border border-slate-300 px-3 py-2 text-sm shadow-sm focus:border-slate-500 focus:outline-none"
            />
            <p v-if="errors.description" class="mt-1 text-xs text-red-600">
                {{ errors.description }}
            </p>
        </div>
 
        <div>
            <label for="prompt-body" class="mb-1 block text-sm font-medium text-slate-700">
                Prompt body
            </label>
            <textarea
                id="prompt-body"
                v-model="form.body"
                rows="8"
                spellcheck="false"
                class="w-full rounded-md border border-slate-300 px-3 py-2 font-mono text-sm shadow-sm focus:border-slate-500 focus:outline-none"
            />
            <p class="mt-1 text-xs text-slate-500">
                Wrap placeholders like <code class="rounded bg-slate-100 px-1 py-0.5">{{ PLACEHOLDER }}</code>
            </p>
            <p v-if="errors.body" class="mt-1 text-xs text-red-600">{{ errors.body }}</p>
        </div>
 
        <div>
            <label for="prompt-tags" class="mb-1 block text-sm font-medium text-slate-700">
                Tags
            </label>
            <div
                class="flex flex-wrap items-center gap-1.5 rounded-md border border-slate-300 px-2 py-1.5 shadow-sm focus-within:border-slate-500"
            >
                <span
                    v-for="(tag, index) in form.tags"
                    :key="tag"
                    class="inline-flex items-center gap-1 rounded-full bg-slate-100 px-2 py-0.5 text-xs font-medium text-slate-700"
                >
                    {{ tag }}
                    <button
                        type="button"
                        class="text-slate-400 hover:text-slate-700"
                        :aria-label="`Remove tag ${tag}`"
                        @click="removeTag(index)"
                    >
                        ×
                    </button>
                </span>
                <input
                    id="prompt-tags"
                    v-model="tagInput"
                    type="text"
                    placeholder="Type a tag, press Enter…"
                    class="min-w-28 flex-1 border-0 bg-transparent p-0 text-sm focus:outline-none"
                    @keydown="onTagKeydown"
                    @blur="addTag(tagInput)"
                />
            </div>
            <p v-if="errors.tags" class="mt-1 text-xs text-red-600">
                {{ Array.isArray(errors.tags) ? errors.tags[0] : errors.tags }}
            </p>
            <div v-if="availableSuggestions.length > 0" class="mt-2 flex flex-wrap gap-1.5">
                <button
                    v-for="tag in availableSuggestions"
                    :key="tag"
                    type="button"
                    class="rounded-full border border-dashed border-slate-300 px-2 py-0.5 text-xs text-slate-500 hover:border-slate-400 hover:text-slate-700"
                    @click="addTag(tag)"
                >
                    + {{ tag }}
                </button>
            </div>
        </div>
 
        <div class="flex items-center justify-between border-t border-slate-100 pt-4">
            <Link
                :href="cancelHref"
                class="text-sm text-slate-500 hover:text-slate-800 hover:underline"
            >
                Cancel
            </Link>
            <button
                type="submit"
                :disabled="form.processing"
                class="rounded-md bg-slate-900 px-4 py-2 text-sm font-medium text-white hover:bg-slate-700 disabled:cursor-not-allowed disabled:opacity-50"
            >
                {{ form.processing ? 'Saving…' : submitLabel }}
            </button>
        </div>
    </form>
</template>