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

Commit 8d307e5

Browse files
author
Daniel Requejo
committed
Form state, values and submit
1 parent e58e16e commit 8d307e5

File tree

4 files changed

+185
-5
lines changed

4 files changed

+185
-5
lines changed
Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
1-
# formState
1+
# formState: <font size="3">FormState</font>
2+
3+
Provides you with the reactive state of the form, including validation, dirty and touched state, for the whole form or individual fields.
4+
5+
## Return
6+
7+
| attribute | type | description |
8+
|-----------|--------|--------------------------------------------|
9+
| dirty | Record<string, boolean> | Object containing all the inputs that have been modified |
10+
| errors | Record<string, string> | Object containing all the current field errors of the form |
11+
| touched | Record<string, boolean> | Object containing all the inputs the users has interacted with |
12+
| isDirty | boolean | True if there is any modified field on the form |
13+
| isTouched | boolean | True if there has been any interaction with a form field |
14+
| isValid | boolean | True if there are no form errors |
15+
16+
## Rules
17+
18+
`formState` is read-only, so no assignments are expected. It is entirely reactive, so you can react on changes of the whole element and/or it's main attributes.
19+
20+
## Usage
21+
22+
```vue
23+
<template>
24+
<form>
25+
<input type="text" v-bind="register('name', {
26+
required: true
27+
})" />
28+
<p v-if="formState.errors.name"> {{ formState.errors.name }} </p>
29+
<input type="text" v-bind="register('email', {
30+
required: true
31+
})" />
32+
<p v-if="formState.errors.email"> {{ formState.errors.email }} </p>
33+
<input type="text" v-bind="register('summary')">
34+
<button type="submit">Submit</button>
35+
</form>
36+
</template>
37+
<script setup lang="ts" >
38+
import { useFormHandler } from '../src/index'
39+
40+
const { register, formState } = useFormHandler()
41+
</script>
42+
```
43+
44+
## Type Declaration
45+
46+
```ts
47+
export interface FormState {
48+
isDirty: boolean
49+
isTouched: boolean
50+
isValid: boolean
51+
dirty: Record<string, boolean>
52+
touched: Record<string, boolean>
53+
errors: Record<string, string>
54+
}
55+
```
Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,85 @@
1-
# handleSubmit
1+
# handleSubmit: <font size="3">HandleSubmit</font>
2+
3+
Submits the form on demand, causing it's validation depending on the mode/validation function, and calls the success/error fn consequently based on the outcome.
4+
5+
## Demo
6+
7+
Coming soon...
8+
9+
## Usage
10+
11+
### Submit a form and catch if errors/issues on submitting
12+
13+
```vue
14+
<template>
15+
<form @submit.prevent="submitFn">
16+
<input type="text" v-bind="register('name', {
17+
required: true
18+
})" />
19+
<input type="text" v-bind="register('email', {
20+
required: true
21+
})" />
22+
<input type="text" v-bind="register('summary')">
23+
<button type="submit">Submit</button>
24+
</form>
25+
</template>
26+
<script setup lang="ts" >
27+
import { useFormHandler } from '../src/index'
28+
29+
const { register, handleSubmit, formState } = useFormHandler()
30+
const successFn = (form: any) => {
31+
//do anything with form
32+
console.log(form)
33+
}
34+
const submitFn = () => {
35+
try {
36+
handleSubmit(successFn)
37+
} catch {
38+
//do anything with errors
39+
console.log(formState.errors)
40+
}
41+
}
42+
</script>
43+
```
44+
45+
### Submit a form and pass error function
46+
47+
```vue
48+
<template>
49+
<form @submit.prevent="handleSubmit(successFn,errorFn)">
50+
<input type="text" v-bind="register('name', {
51+
required: true
52+
})" />
53+
<input type="text" v-bind="register('email', {
54+
required: true
55+
})" />
56+
<input type="text" v-bind="register('summary')">
57+
<button type="submit">Submit</button>
58+
</form>
59+
</template>
60+
<script setup lang="ts" >
61+
import { useFormHandler } from '../src/index'
62+
63+
const { register, handleSubmit } = useFormHandler()
64+
const successFn = (form: any) => {
65+
//do anything with form
66+
console.log(form)
67+
}
68+
const errorFn = (errors:any) => {
69+
//do anything with errors
70+
console.log(errors)
71+
}
72+
</script>
73+
```
74+
75+
## Type Declaration
76+
77+
```ts
78+
export type HandleSubmitSuccessFn = (values: Record<string, any>) => void
79+
export type HandleSubmitErrorFn = (errors: Record<string, string>) => void
80+
81+
export type HandleSubmit = (
82+
successFn: HandleSubmitSuccessFn,
83+
errorFn?: HandleSubmitErrorFn
84+
) => void
85+
```
Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
1-
# values
1+
# values: <font size="3">Record<string, any></font>
2+
3+
Provides you with the reactive state of the form, including validation, dirty and touched state, for the whole form or individual fields.
4+
5+
## Return
6+
7+
| attribute | type | description |
8+
|-----------|--------|--------------------------------------------|
9+
| &lt;fieldName&gt; | any | Current value of the specified field |
10+
## Rules
11+
12+
`values` is read-only, so no assignments are expected. It is entirely reactive, so you can react on changes of the whole element and/or it's main attributes.
13+
14+
## Usage
15+
16+
```vue
17+
<template>
18+
<form>
19+
<input type="text" v-bind="register('name')" />
20+
<input type="text" v-bind="register('email')" />
21+
<input type="text" v-bind="register('summary')">
22+
</form>
23+
<section>
24+
<h2>Preview:</h2>
25+
<div>
26+
<h3>Name: {{values.name}}</h3>
27+
<h4>Email: {{values.email}}</h4>
28+
<p>Summary: {{values.summary}}</p>
29+
</div>
30+
</section>
31+
</template>
32+
<script setup lang="ts" >
33+
import { useFormHandler } from '../src/index'
34+
35+
const { register, values } = useFormHandler()
36+
</script>
37+
```
38+
39+
## Type Declaration
40+
41+
```ts
42+
export type Values = Record<string, any>
43+
```

src/types/formHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ export type ClearError = (name?: string) => void
6161
export type ModifiedValues = () => Record<string, any>
6262

6363
/** Expected function to be called after a form submitted successfully */
64-
export type HandleSubmitSuccessFn = (values: Object) => void
64+
export type HandleSubmitSuccessFn = (values: Record<string, any>) => void
6565

6666
/** Optional function to be called after a form failed to submit */
67-
export type HandleSubmitErrorFn = (errors: Object) => void
67+
export type HandleSubmitErrorFn = (errors: Record<string, string>) => void
6868

6969
/** Checks for the validity of the form before submitting */
7070
export type IsValidForm = () => Promise<boolean>

0 commit comments

Comments
 (0)