-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.vue
More file actions
51 lines (42 loc) · 1.3 KB
/
app.vue
File metadata and controls
51 lines (42 loc) · 1.3 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
<script setup lang="ts">
import { z } from "zod"
// Define your schema with a dash of magic
const schema = z.object({ planet: z.string() })
// Create your form with a unique key
const { getFieldState, register, key, setValue } = useForm({
schema,
key: "planet-form-key",
})
// Get the state of the 'planet' field
const planetState = getFieldState("planet")
const hideInput = ref(false)
// random operation to demonstrate updating the planet field programmatically
function updateVal(testUpdate: boolean) {
setValue("planet", (v) => {
const [base, count] = v.split(".")
const recoveredNum = Number(count ?? 0)
const safeNum = Number.isNaN(recoveredNum) ? 0 : recoveredNum
const BASE_DEFAULT = "Jupiter"
return testUpdate ? `${base || BASE_DEFAULT}.${safeNum + 1}` : v
})
}
</script>
<template>
<div>
<h1>Fancy Form "{{ key }}"</h1>
<input
v-if="!hideInput"
v-register="register('planet')"
placeholder="Enter your favorite planet"
>
<p>Favorite Planet field state:</p>
<pre>{{ JSON.stringify(planetState, null, 2) }}</pre>
<button @click="hideInput = !hideInput">
{{ hideInput ? 'show input' : 'hide input' }}
</button>
<button @click="updateVal(true)">
Update value programmatically
</button>
<hr>
</div>
</template>