-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform-composable.vue
More file actions
103 lines (95 loc) · 3.19 KB
/
Copy pathform-composable.vue
File metadata and controls
103 lines (95 loc) · 3.19 KB
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
<script setup lang="ts">
import { FormKit, FormKitMessages } from '@formkit/vue'
// `useFormKitForm` is auto-imported (`src/module.ts` registers
// `./runtime/composables` via `addImportsDir`) - no explicit import needed
// in a real consuming app, same as any other Nuxt composable.
const FORM_ID = 'composable-demo-form'
const form = useFormKitForm(FORM_ID)
async function handleSubmit() {
// The submit button lives *outside* the `<FormKit type="form">` below -
// useFormKitForm() is what makes that possible, since it looks the form up
// by its registry `id` rather than needing to be inside the form's own
// component tree.
form.submit(async () => {
await new Promise(resolve => setTimeout(resolve, 800))
console.log('Form Submitted...')
})
}
function simulateServerError() {
form.setErrors('The server rejected this submission - please try again.')
}
</script>
<template>
<UContainer>
<div class="mb-8">
<h1 class="text-4xl font-bold mb-4">
useFormKitForm Composable
</h1>
<p class="text-lg text-muted-foreground mb-2">
Submit, reset, and error-display buttons wired up via <code>useFormKitForm</code>, living outside the <code>FormKit</code> form itself.
</p>
<p class="text-muted-foreground">
Reactive <code>isValid</code>/<code>isSubmitted</code>/<code>hasErrors</code>/<code>isLoading</code> state, plus <code>submit</code>/<code>reset</code>/<code>setErrors</code>/<code>clearErrors</code> wrapping FormKit's imperative APIs.
</p>
</div>
<USeparator class="my-8" />
<div class="space-y-6">
<FormKit
:id="FORM_ID"
type="form"
:actions="false"
>
<FormKit
type="nuxtUIInput"
name="email"
label="Email"
input-type="email"
validation="required|email"
outer-class="mb-4"
/>
<FormKit
type="nuxtUIInput"
name="displayName"
label="Display Name"
validation="required"
outer-class="mb-4"
/>
<FormKitMessages />
</FormKit>
<div class="flex gap-2">
<UButton
label="Submit"
icon="i-lucide-save"
:loading="form.isLoading.value"
@click="handleSubmit"
/>
<UButton
label="Reset"
icon="i-lucide-rotate-ccw"
variant="outline"
@click="form.reset()"
/>
<UButton
label="Simulate Server Error"
icon="i-lucide-alert-triangle"
color="error"
variant="outline"
@click="simulateServerError"
/>
<UButton
label="Clear Errors"
icon="i-lucide-x"
color="error"
variant="ghost"
@click="form.clearErrors()"
/>
</div>
<div class="text-sm text-muted-foreground space-x-4">
<span>isValid: <strong>{{ form.isValid.value }}</strong></span>
<span>isSubmitted: <strong>{{ form.isSubmitted.value }}</strong></span>
<span>hasErrors: <strong>{{ form.hasErrors.value }}</strong></span>
<span>isLoading: <strong>{{ form.isLoading.value }}</strong></span>
</div>
</div>
</UContainer>
</template>