Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Commit 27920e1

Browse files
committed
📄 Docs examples 1st batch:
- Setup - Embedding component - Async submission - Async validations
1 parent 1f63717 commit 27920e1

20 files changed

+3466
-1
lines changed

docs/.vitepress/config.cts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default defineConfig({
4747
},
4848
{
4949
text: 'Examples', collapsible: true, items: [
50+
{ text: 'Async submission', link: '/examples/async-submission' },
5051
{ text: 'Async validations', link: '/examples/async-validations' },
5152
{ text: 'Basic', link: '/examples/basic' },
5253
{ text: 'Dependent fields', link: '/examples/dependent-fields' },

docs/examples/async-submission.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
description: This example shows how to use `async/await` to submit a form with VueFormHandler
3+
---
4+
# Async submission
5+
This example shows how to use `async/await` to submit a form with VueFormHandler
6+
7+
<CodeExample example="async-submission"></CodeExample>

docs/examples/async-validations.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
# Async validations
1+
---
2+
description: This example shows how to make use of asynchronous validations
3+
---
4+
# Async validations
5+
6+
This example shows how to make use of asynchronous validations
7+
8+
<CodeExample example="async-validations"></CodeExample>

docs/examples/typescript.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
description: This example demonstrates how leverage typescript usage with the form handler.
3+
---
4+
15
# Typescript
26

37
This example demonstrates how leverage typescript usage with the form handler.

examples/async-submission/App.vue

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<template>
2+
<section>
3+
<form @submit.prevent="submit()">
4+
<label>Email:
5+
<input type="email" v-bind="register('email', {
6+
required: true,
7+
pattern: emailRegExp,
8+
})" />
9+
</label>
10+
<p class="error" v-show="formState.errors.email">
11+
{{ formState.errors.email }}
12+
</p>
13+
<label> Password:
14+
<input type="password" v-bind="register('password', {
15+
required: true,
16+
pattern: passwordRegExp
17+
})" />
18+
</label>
19+
<p class="error" v-show="formState.errors.password">
20+
{{ formState.errors.password }}
21+
</p>
22+
<label> Confirm Password:
23+
<input type="password" v-bind="register('confirmPassword', {
24+
required: true,
25+
pattern: passwordRegExp,
26+
validate: {
27+
match: (value) => value === values.password || 'Passwords do not match'
28+
}
29+
})" />
30+
</label>
31+
<p class="error" v-show="formState.errors.confirmPassword">
32+
{{ formState.errors.confirmPassword }}
33+
</p>
34+
<button>Submit</button>
35+
</form>
36+
</section>
37+
</template>
38+
<script lang="ts" >
39+
import { useFormHandler } from 'vue-form-handler';
40+
41+
export default {
42+
setup: () => {
43+
const passwordRegExp = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/
44+
const emailRegExp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i
45+
46+
const { values, register, formState, handleSubmit } = useFormHandler({ validationMode: 'always' });
47+
48+
// success/error functions can be used in the handleSubmit function to handle the form submission
49+
const successFn = (form: Record<string, any>) => {
50+
console.log('Form correctly submitted:', form)
51+
}
52+
53+
const errorFn = (errors: Record<string, any>) => {
54+
console.error('There where errors while submitting the form:', errors)
55+
}
56+
57+
// async/await can be used to do things after the form submission
58+
const submit = async () => {
59+
await handleSubmit(successFn, errorFn)
60+
alert(`was success? ${formState.isValid}`)
61+
// Do something else
62+
}
63+
64+
return {
65+
passwordRegExp,
66+
emailRegExp,
67+
values,
68+
register,
69+
formState,
70+
submit
71+
}
72+
}
73+
}
74+
75+
76+
</script>
77+
78+
<style>
79+
* {
80+
box-sizing: border-box;
81+
margin: 0;
82+
padding: 0;
83+
text-align: center;
84+
}
85+
86+
body {
87+
display: flex;
88+
align-items: center;
89+
justify-content: center;
90+
background-color: #242424;
91+
font-family: 'Open Sans', sans-serif;
92+
font-size: 16px;
93+
color: #42B883;
94+
min-height: 100vh;
95+
}
96+
97+
label {
98+
display: block;
99+
}
100+
101+
form {
102+
display: flex;
103+
flex-direction: column;
104+
align-items: flex-end;
105+
gap: 1rem
106+
}
107+
108+
109+
110+
input,
111+
select,
112+
textarea,
113+
button {
114+
border: none;
115+
border-radius: 5px;
116+
width: 300px;
117+
min-height: 40px;
118+
background-color: #35495E;
119+
color: #42B883;
120+
}
121+
122+
button {
123+
background-color: #42B883;
124+
color: #35495E;
125+
cursor: pointer;
126+
text-transform: uppercase;
127+
font-weight: bold;
128+
}
129+
</style>

examples/async-submission/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Vue 3 + TypeScript + Vite
2+
3+
This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
4+
5+
## Recommended IDE Setup
6+
7+
- [VS Code](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar) (and disable Vetur) + [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin).
8+
9+
## Type Support For `.vue` Imports in TS
10+
11+
TypeScript cannot handle type information for `.vue` imports by default, so we replace the `tsc` CLI with `vue-tsc` for type checking. In editors, we need [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin) to make the TypeScript language service aware of `.vue` types.
12+
13+
If the standalone TypeScript plugin doesn't feel fast enough to you, Volar has also implemented a [Take Over Mode](https://github.com/johnsoncodehk/volar/discussions/471#discussioncomment-1361669) that is more performant. You can enable it by the following steps:
14+
15+
1. Disable the built-in TypeScript Extension
16+
1. Run `Extensions: Show Built-in Extensions` from VSCode's command palette
17+
2. Find `TypeScript and JavaScript Language Features`, right click and select `Disable (Workspace)`
18+
2. Reload the VSCode window by running `Developer: Reload Window` from the command palette.

examples/async-submission/index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Async validations</title>
8+
</head>
9+
10+
<body>
11+
<div id="app"></div>
12+
</body>
13+
<script type="module">
14+
import { createApp } from 'vue'
15+
import App from './App.vue'
16+
17+
createApp(App).mount('#app')
18+
19+
</script>
20+
21+
</html>

0 commit comments

Comments
 (0)