@teamnovu/kit-vue-forms 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +11 -14
- package/docs/FormInput-example.md +59 -0
- package/docs/example.md +212 -0
- package/docs/index.md +26 -0
- package/docs/info.json +3 -0
- package/docs/reference.md +136 -0
- package/package.json +1 -1
- package/src/utils/path.ts +1 -4
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
var T = Object.defineProperty;
|
|
2
2
|
var G = (t, e, r) => e in t ? T(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
|
|
3
|
-
var
|
|
3
|
+
var E = (t, e, r) => G(t, typeof e != "symbol" ? e + "" : e, r);
|
|
4
4
|
import { toValue as L, toRaw as Z, computed as c, unref as d, reactive as D, toRefs as N, shallowReactive as q, toRef as _, onScopeDispose as H, ref as W, watch as w, isRef as k, getCurrentScope as Q, onBeforeUnmount as X, defineComponent as j, renderSlot as O, normalizeProps as z, guardReactiveProps as B, resolveComponent as Y, createBlock as $, openBlock as A, withCtx as C, resolveDynamicComponent as x, mergeProps as ee } from "vue";
|
|
5
5
|
import { cloneDeep as re } from "lodash-es";
|
|
6
6
|
import "zod";
|
|
@@ -18,15 +18,12 @@ function P(t, e) {
|
|
|
18
18
|
);
|
|
19
19
|
}
|
|
20
20
|
function te(t, e, r) {
|
|
21
|
-
const s = Array.isArray(e) ? e : K(e)
|
|
22
|
-
if (s.length === 0)
|
|
23
|
-
throw new Error("Path cannot be empty");
|
|
24
|
-
const o = s.at(-1), n = s.slice(0, -1).reduce(
|
|
21
|
+
const s = Array.isArray(e) ? e : K(e), o = s.at(-1), n = s.slice(0, -1).reduce(
|
|
25
22
|
(p, h) => p[h],
|
|
26
23
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
27
24
|
t
|
|
28
25
|
);
|
|
29
|
-
n[o] = r;
|
|
26
|
+
n[o] = o ? r : n;
|
|
30
27
|
}
|
|
31
28
|
const U = (t, e) => c({
|
|
32
29
|
get() {
|
|
@@ -55,7 +52,7 @@ function se(t, e) {
|
|
|
55
52
|
}
|
|
56
53
|
class ae {
|
|
57
54
|
constructor(e) {
|
|
58
|
-
|
|
55
|
+
E(this, "rc", 1);
|
|
59
56
|
this.drop = e;
|
|
60
57
|
}
|
|
61
58
|
inc() {
|
|
@@ -121,8 +118,8 @@ function ne(t, e) {
|
|
|
121
118
|
get() {
|
|
122
119
|
return e.errors.value.propertyErrors[a] || [];
|
|
123
120
|
},
|
|
124
|
-
set(
|
|
125
|
-
e.errors.value.propertyErrors[a] =
|
|
121
|
+
set(F) {
|
|
122
|
+
e.errors.value.propertyErrors[a] = F;
|
|
126
123
|
}
|
|
127
124
|
})
|
|
128
125
|
});
|
|
@@ -258,8 +255,8 @@ class fe {
|
|
|
258
255
|
}
|
|
259
256
|
class pe {
|
|
260
257
|
constructor(e, r) {
|
|
261
|
-
|
|
262
|
-
|
|
258
|
+
E(this, "schemaValidator");
|
|
259
|
+
E(this, "functionValidator");
|
|
263
260
|
this.schema = e, this.validateFn = r, this.schemaValidator = new de(this.schema), this.functionValidator = new fe(this.validateFn);
|
|
264
261
|
}
|
|
265
262
|
async validate(e) {
|
|
@@ -321,7 +318,7 @@ function he(t, e) {
|
|
|
321
318
|
), a = i.every((v) => v.isValid);
|
|
322
319
|
let { errors: u } = m;
|
|
323
320
|
if (!a) {
|
|
324
|
-
const v = i.map((
|
|
321
|
+
const v = i.map((F) => F.errors);
|
|
325
322
|
u = S(...v);
|
|
326
323
|
}
|
|
327
324
|
return {
|
|
@@ -391,7 +388,7 @@ function me(t, e, r) {
|
|
|
391
388
|
}).map((l) => n(l))), i = () => t.fields.value.filter((l) => {
|
|
392
389
|
const f = l.path.value;
|
|
393
390
|
return f.startsWith(e + ".") || f === e;
|
|
394
|
-
}), a = c(() => i().some((l) => l.dirty.value)), u = c(() => i().some((l) => l.touched.value)), v = c(() => t.isValid.value),
|
|
391
|
+
}), a = c(() => i().some((l) => l.dirty.value)), u = c(() => i().some((l) => l.touched.value)), v = c(() => t.isValid.value), F = c(() => t.isValidated.value), J = c(() => se(d(t.errors), e));
|
|
395
392
|
return {
|
|
396
393
|
data: s,
|
|
397
394
|
fields: V,
|
|
@@ -401,7 +398,7 @@ function me(t, e, r) {
|
|
|
401
398
|
isDirty: a,
|
|
402
399
|
isTouched: u,
|
|
403
400
|
isValid: v,
|
|
404
|
-
isValidated:
|
|
401
|
+
isValidated: F,
|
|
405
402
|
errors: J,
|
|
406
403
|
defineValidator: (l) => {
|
|
407
404
|
const f = k(l) ? l : b(l), y = c(
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# FormInput Example
|
|
2
|
+
|
|
3
|
+
To define a form input component you need a plain input component that has at least the following props and emits:
|
|
4
|
+
```typescript
|
|
5
|
+
interface Props {
|
|
6
|
+
modelValue: T
|
|
7
|
+
errors?: string[] | undefined // if the input should display errors
|
|
8
|
+
}
|
|
9
|
+
interface Emits {
|
|
10
|
+
'update:modelValue': [T]
|
|
11
|
+
}
|
|
12
|
+
```
|
|
13
|
+
Then we can easily use the `FormFieldWrapper` component from this package.
|
|
14
|
+
|
|
15
|
+
In the example below we use a text field `TextField.vue`.
|
|
16
|
+
|
|
17
|
+
Example:
|
|
18
|
+
```vue
|
|
19
|
+
<!-- FormTextInput.vue -->
|
|
20
|
+
<template>
|
|
21
|
+
<FormFieldWrapper
|
|
22
|
+
:component="TextField"
|
|
23
|
+
:component-props="$props"
|
|
24
|
+
:path="path"
|
|
25
|
+
:form="form"
|
|
26
|
+
/>
|
|
27
|
+
</template>
|
|
28
|
+
|
|
29
|
+
<script setup lang="ts" generic="TData extends object, TPath extends Paths<TData>">
|
|
30
|
+
import type { TextFieldProps } from '#components/utils/form/plainInput/TextField.vue';
|
|
31
|
+
import TextField from '#components/utils/form/plainInput/TextField.vue';
|
|
32
|
+
import { type Paths, FormFieldWrapper } from '@teamnovu/kit-vue-forms';
|
|
33
|
+
import type {
|
|
34
|
+
ExcludedFieldProps,
|
|
35
|
+
FormComponentProps,
|
|
36
|
+
} from '#types/form/FormComponentProps';
|
|
37
|
+
|
|
38
|
+
export type Props<TData extends object, TPath extends Paths<TData>> =
|
|
39
|
+
FormComponentProps<TData, TPath, TextFieldProps['modelValue']> & Omit<TextFieldProps, ExcludedFieldProps>;
|
|
40
|
+
|
|
41
|
+
defineProps<Props<TData, TPath>>();
|
|
42
|
+
</script>
|
|
43
|
+
```
|
|
44
|
+
Note the usage of the generic types `TData` and `TPath` to make the component fully type-safe. With this, the `path` prop
|
|
45
|
+
will only allow valid paths of the form data. Moreover, it will throw a type error if the property at `path` of the `form`
|
|
46
|
+
has the wrong type. This is ensured by using the `FormComponentProps` type above.
|
|
47
|
+
|
|
48
|
+
The usage of such a form input component is as follows (assuming the input should handle the "firstName" property of the form data):
|
|
49
|
+
```vue
|
|
50
|
+
<FormTextInput
|
|
51
|
+
:form="form"
|
|
52
|
+
path="firstName"
|
|
53
|
+
/>
|
|
54
|
+
```
|
|
55
|
+
Here, `form` is the Form component that was created with [`useForm`](index.md#useform). All additional props are passed
|
|
56
|
+
to the underlying plain input component, e.g. `label`, `placeholder`, etc.
|
|
57
|
+
|
|
58
|
+
It is recommended to use this pattern of a styled "plain input" that works with v-model and a "form input" to work with form
|
|
59
|
+
and path as props for all your form inputs. Like that, you can easily use the plain component outside of forms as well.
|
package/docs/example.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# Example
|
|
2
|
+
|
|
3
|
+
The following example shows how to use this library for a more complicated form with nested objects and arrays.
|
|
4
|
+
The data structure of the form is as follows:
|
|
5
|
+
```typescript
|
|
6
|
+
{
|
|
7
|
+
person: {
|
|
8
|
+
firstName: string,
|
|
9
|
+
lastName: string | undefined,
|
|
10
|
+
address: {
|
|
11
|
+
street: string,
|
|
12
|
+
city: string,
|
|
13
|
+
},
|
|
14
|
+
hobbies: string[]
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
We can create a form like this:
|
|
20
|
+
|
|
21
|
+
```vue
|
|
22
|
+
<template>
|
|
23
|
+
<form @submit.prevent="submit">
|
|
24
|
+
<!-- Simple form inputs -->
|
|
25
|
+
<!-- the "label" prop is passed through the FormTextField to the underlying TextField -->
|
|
26
|
+
<FormTextField
|
|
27
|
+
:form="form"
|
|
28
|
+
path="person.firstName"
|
|
29
|
+
label="First Name"
|
|
30
|
+
/>
|
|
31
|
+
<FormTextField
|
|
32
|
+
:form="form"
|
|
33
|
+
path="person.lastName"
|
|
34
|
+
label="Last Name"
|
|
35
|
+
/>
|
|
36
|
+
|
|
37
|
+
<!-- Oh oh, my path was wrong here, the form type does not have a property "person.middleName" -->
|
|
38
|
+
<!-- I get a typescript error that this path is not allowed
|
|
39
|
+
<FormTextField
|
|
40
|
+
:form="form"
|
|
41
|
+
path="person.middleName"
|
|
42
|
+
/>
|
|
43
|
+
-->
|
|
44
|
+
<!-- Oh oh, I used a Text component (which allows "string | number | null | undefined"), but the property at the path is a boolean -->
|
|
45
|
+
<!-- I get a typescript error that this path and form combination is not allowed
|
|
46
|
+
<FormTextField
|
|
47
|
+
:form="form"
|
|
48
|
+
path="person.isMale"
|
|
49
|
+
/>
|
|
50
|
+
-->
|
|
51
|
+
|
|
52
|
+
<!-- Nested forms in subcomponents -->
|
|
53
|
+
<!-- My FormAddressField handles forms of type { street: string, city: string } -->
|
|
54
|
+
<!-- so we can make a subform for the address property of our main form -->
|
|
55
|
+
<FormPart :form="form" path="person.address" #="{ subform }">
|
|
56
|
+
<FormAddressField :form="subform" />
|
|
57
|
+
</FormPart>
|
|
58
|
+
|
|
59
|
+
<div v-for="(_, index) in unref(form.data).person.hobbies" :key="index" class="ml-4">
|
|
60
|
+
<!-- Note the path format of an array item. You just use the index number in the dot-path -->
|
|
61
|
+
<FormTextField
|
|
62
|
+
:form="form"
|
|
63
|
+
:path="`person.hobbies.${index}`"
|
|
64
|
+
/>
|
|
65
|
+
</div>
|
|
66
|
+
|
|
67
|
+
<button type="button" @click="toggleComment">
|
|
68
|
+
Toggle Comment
|
|
69
|
+
</button>
|
|
70
|
+
|
|
71
|
+
<!-- If you need other data, use the data ref of the form object; don't use getField here -->
|
|
72
|
+
<!-- Alternatively, in this case you could use the variable "isCommentEnabled" that was defined in the script setup -->
|
|
73
|
+
<FormTextField
|
|
74
|
+
v-if="unref(form.data).commentEnabled"
|
|
75
|
+
:form="form"
|
|
76
|
+
path="comment"
|
|
77
|
+
/>
|
|
78
|
+
|
|
79
|
+
<!-- This is a one-off special thing; I don't really want to make a FormInput out of it -->
|
|
80
|
+
<!-- I can use the Field component, or I could use form.getField in the script setup -->
|
|
81
|
+
<Field v-slot="{ data, setData, errors }" path="person.parent" :form="form">
|
|
82
|
+
<div>
|
|
83
|
+
Father: {{ data.fatherName }}
|
|
84
|
+
</div>
|
|
85
|
+
<div>
|
|
86
|
+
Mother: {{ data.motherName }}
|
|
87
|
+
</div>
|
|
88
|
+
<!-- the "errors" will be an array of strings of the errors corresponding to the property with the given path (here "person.parent") -->
|
|
89
|
+
<InputError :errors="errors" />
|
|
90
|
+
<button type="button" @click="() => setData({ fatherName: 'New Father', motherName: 'New Mother' })">
|
|
91
|
+
Change Parent Names
|
|
92
|
+
</button>
|
|
93
|
+
</Field>
|
|
94
|
+
|
|
95
|
+
<button type="submit">
|
|
96
|
+
Submit Form
|
|
97
|
+
</button>
|
|
98
|
+
|
|
99
|
+
</form>
|
|
100
|
+
</template>
|
|
101
|
+
|
|
102
|
+
<script setup lang="ts">
|
|
103
|
+
import { Field, useForm, FormPart } from '@teamnovu/kit-vue-forms';
|
|
104
|
+
import { z } from 'zod';
|
|
105
|
+
import { unref } from 'vue';
|
|
106
|
+
import FormTextField from '#components/utils/form/formInput/FormTextField.vue';
|
|
107
|
+
import FormAddressField from '#/FormAddressField.vue';
|
|
108
|
+
import InputError from '#components/utils/form/InputError.vue';
|
|
109
|
+
|
|
110
|
+
const form = useForm<{ // this type might be inferred automatically from the initialData; but here, it would not work, because lastName is not defined in the initialData
|
|
111
|
+
person: {
|
|
112
|
+
firstName: string
|
|
113
|
+
lastName?: string | undefined
|
|
114
|
+
address: {
|
|
115
|
+
street: string
|
|
116
|
+
city: string
|
|
117
|
+
}
|
|
118
|
+
hobbies: string[]
|
|
119
|
+
parent: {
|
|
120
|
+
fatherName: string
|
|
121
|
+
motherName: string
|
|
122
|
+
}
|
|
123
|
+
isMale: boolean
|
|
124
|
+
}
|
|
125
|
+
commentEnabled?: boolean
|
|
126
|
+
comment: string
|
|
127
|
+
}>({
|
|
128
|
+
initialData: {
|
|
129
|
+
person: {
|
|
130
|
+
firstName: '',
|
|
131
|
+
// I don't need to define an initial value for lastName here, because it's optional; the form will still handle it correctly
|
|
132
|
+
address: {
|
|
133
|
+
street: '',
|
|
134
|
+
city: '',
|
|
135
|
+
},
|
|
136
|
+
hobbies: ['cooking', 'coding'],
|
|
137
|
+
parent: {
|
|
138
|
+
fatherName: 'Hans Müller',
|
|
139
|
+
motherName: 'Hannah Schmidt',
|
|
140
|
+
},
|
|
141
|
+
isMale: false
|
|
142
|
+
},
|
|
143
|
+
commentEnabled: false,
|
|
144
|
+
comment: '',
|
|
145
|
+
},
|
|
146
|
+
// optional zod schema for validation; might also just define part of the schema if other properties should be ignored
|
|
147
|
+
schema: z.object({
|
|
148
|
+
person: z.object({
|
|
149
|
+
hobbies: z.array(z.string().min(1, 'Hobby cannot be empty')),
|
|
150
|
+
}),
|
|
151
|
+
}),
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
const sendToBackend = async (data) => {
|
|
155
|
+
// send data object to backend
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const submit = async () => {
|
|
159
|
+
// validate the form
|
|
160
|
+
// if the zod schema is not satisfied, isValid will be false and the errors will be set on the corresponding fields
|
|
161
|
+
// if your TextInput component handles errors correctly, it should be directly visible
|
|
162
|
+
const { isValid } = await form.validateForm();
|
|
163
|
+
|
|
164
|
+
// only submit if the form is valid
|
|
165
|
+
if (isValid) {
|
|
166
|
+
await sendToBackend(unref(form.data));
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// To programmatically change form values, use form.getField to get a ref and a setter function
|
|
171
|
+
// never modify form.data directly
|
|
172
|
+
const { data: isCommentEnabled, setData: setCommentEnabled } = form.getField('commentEnabled');
|
|
173
|
+
const toggleComment = () => {
|
|
174
|
+
setCommentEnabled(!unref(isCommentEnabled));
|
|
175
|
+
};
|
|
176
|
+
</script>
|
|
177
|
+
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
An example of the `FormAddressField` that was used above would be:
|
|
181
|
+
```vue
|
|
182
|
+
<!-- FormAddressField.vue -->
|
|
183
|
+
<template>
|
|
184
|
+
<div>
|
|
185
|
+
<FormTextField
|
|
186
|
+
:form="form"
|
|
187
|
+
path="street"
|
|
188
|
+
label="Street Address"
|
|
189
|
+
/>
|
|
190
|
+
|
|
191
|
+
<FormTextField
|
|
192
|
+
:form="form"
|
|
193
|
+
path="city"
|
|
194
|
+
label="City"
|
|
195
|
+
/>
|
|
196
|
+
</div>
|
|
197
|
+
</template>
|
|
198
|
+
|
|
199
|
+
<script setup lang="ts">
|
|
200
|
+
import type { Form } from '@teamnovu/kit-vue-forms';
|
|
201
|
+
import FormTextField from '#components/utils/form/formInput/FormTextField.vue';
|
|
202
|
+
|
|
203
|
+
interface Props {
|
|
204
|
+
form: Form<{
|
|
205
|
+
street: string
|
|
206
|
+
city: string
|
|
207
|
+
}>
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
defineProps<Props>();
|
|
211
|
+
</script>
|
|
212
|
+
```
|
package/docs/index.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @teamnovu/kit-vue-forms
|
|
2
|
+
|
|
3
|
+
A library for data and error handling of forms, including validation with zod schemas.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @teamnovu/kit-vue-forms
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Purpose
|
|
12
|
+
This package provides composables and some data components to simplify the data management of forms and associated errors.
|
|
13
|
+
It was designed as a replacement of Vee-Validate with more flexibility and full type safety. We don't use a provided and injected form object,
|
|
14
|
+
but pass it down as a prop to guarantee type safety.
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
1. Wrap your plain input components into `FormFieldWrapper`, see [FormInput Example](./FormInput-example.md).
|
|
19
|
+
2. Inside your parent component where you want to use a form, use the composable `useForm` to create a form object.
|
|
20
|
+
This object contains the form data, errors, and methods to define
|
|
21
|
+
fields, validate the form, reset it, and create subforms for nested objects or arrays, see [here](./reference#composable-useform).
|
|
22
|
+
3. Pass this form object to any form input component together with a path to the property of the form data that this input
|
|
23
|
+
should manage. You can also make subforms, pass the form down to child components, etc. See the examples for inspiration.
|
|
24
|
+
4. See Reference documentation for all types and methods: [here](./reference.md).
|
|
25
|
+
|
|
26
|
+
An example of the most common usages can be found [here](./example.md).
|
package/docs/info.json
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# Reference
|
|
2
|
+
## Composable `useForm`
|
|
3
|
+
This is the main composable which creates the form. It has the following signature:
|
|
4
|
+
```typescript
|
|
5
|
+
function useForm<T extends object>(options: {
|
|
6
|
+
// the initial data of the form
|
|
7
|
+
// reactive changes to this object will propagate to the form data
|
|
8
|
+
initialData: MaybeRefOrGetter<T>
|
|
9
|
+
// an ErrorBag object or ref of an ErrorBag object with external errors
|
|
10
|
+
// this is used e.g. for server side validation errors
|
|
11
|
+
// these errors will be merged with the internal errors of the form based on validationFn below and/or the zod schema
|
|
12
|
+
errors?: MaybeRef<ErrorBag | undefined>
|
|
13
|
+
// a zod schema of the form data
|
|
14
|
+
// this is validated based on the validationStrategy or by manually triggering validateForm on the form object
|
|
15
|
+
schema?: MaybeRef<z.ZodType>
|
|
16
|
+
// a custom validation function which is called with the current form data
|
|
17
|
+
// this is additional to the schema for custom validations
|
|
18
|
+
validateFn?: MaybeRef<ValidationFunction<T>>
|
|
19
|
+
// if the form data of a property should be reset or kept if all fields corresponding to this property are unmounted
|
|
20
|
+
// !! currently not implemented !!
|
|
21
|
+
keepValuesOnUnmount?: MaybeRef<boolean>
|
|
22
|
+
// when validation should be done (on touch, pre submit, etc.)
|
|
23
|
+
// !! currently not implemented !!
|
|
24
|
+
validationStrategy?: MaybeRef<ValidationStrategy>
|
|
25
|
+
}): Form<T>
|
|
26
|
+
```
|
|
27
|
+
This composable returns a `Form<T>` object.
|
|
28
|
+
|
|
29
|
+
## Type `Form<T>`
|
|
30
|
+
Here, `T` is the type of the form data, which is inferred from the `initialData` property of the options object passed to `useForm`.
|
|
31
|
+
You can also explicitly provide a type argument to `useForm<T>` if needed (useful if the initial data is an empty object `{}`).
|
|
32
|
+
|
|
33
|
+
This object has the following properties and methods:
|
|
34
|
+
```typescript
|
|
35
|
+
interface Form<T extends object> {
|
|
36
|
+
// the current working data of the form
|
|
37
|
+
// this might differ from initialData if the user has changed some values
|
|
38
|
+
data: Ref<T>
|
|
39
|
+
// the initial data of the form as passed to useForm
|
|
40
|
+
initialData: Readonly<Ref<T>>
|
|
41
|
+
|
|
42
|
+
// all fields of the form that are currently managed
|
|
43
|
+
fields: Ref<FieldsTuple<T>>
|
|
44
|
+
|
|
45
|
+
// defines a field such that it will be managed by the form
|
|
46
|
+
// without this, the form will not track changes, touched state or validation for this field
|
|
47
|
+
// use this like a composable
|
|
48
|
+
defineField: <P extends Paths<T>>(options: DefineFieldOptions<PickProps<T, P>, P>) => FormField<PickProps<T, P>, P>
|
|
49
|
+
// gets an already defined field by its path
|
|
50
|
+
// note: if the field was not yet defined, it will be defined automatically
|
|
51
|
+
// the output is a reactive object containing the data, errors and states as well as some methods
|
|
52
|
+
// use this like a composable
|
|
53
|
+
getField: <P extends Paths<T>>(path: P) => FormField<PickProps<T, P>, P>
|
|
54
|
+
|
|
55
|
+
// true if the form has been modified from its initial state
|
|
56
|
+
isDirty: Ref<boolean>
|
|
57
|
+
// true if any field of the form has been touched (i.e. onBlur was called on any field)
|
|
58
|
+
isTouched: Ref<boolean>
|
|
59
|
+
// true if the form data is valid based on the schema and/or the validateFn
|
|
60
|
+
isValid: Ref<boolean>
|
|
61
|
+
// true if the form has been validated at least once
|
|
62
|
+
isValidated: Ref<boolean>
|
|
63
|
+
// the ErrorBag object containing all errors of the form
|
|
64
|
+
// this is a merge of internal errors based on schema/validateFn and external errors passed to useForm
|
|
65
|
+
// the errors are structured based on the paths of the form fields
|
|
66
|
+
errors: Ref<ErrorBag>
|
|
67
|
+
|
|
68
|
+
// defines a custom validator for the form
|
|
69
|
+
// with this, a subcomponent might add a validator function and/or schema to the form
|
|
70
|
+
// without needing access to the initial useForm call
|
|
71
|
+
defineValidator: <TData extends T>(options: ValidatorOptions<TData> | Ref<Validator<TData>>) => Ref<Validator<TData> | undefined>
|
|
72
|
+
|
|
73
|
+
// resets the form data and errors, as well as the dirty, touched etc. state of all fields
|
|
74
|
+
reset: () => void
|
|
75
|
+
// manually triggers validation of the form data based on schema and/or validateFn
|
|
76
|
+
validateForm: () => Promise<ValidationResult>
|
|
77
|
+
|
|
78
|
+
// creates a subform for a nested object or array property of the form data
|
|
79
|
+
// the subform is again a Form<T> object, where T is the type of the nested property
|
|
80
|
+
// it will contain all errors that have paths starting with the path of the subform
|
|
81
|
+
// changes to the subform data will propagate to the main form data and vice versa
|
|
82
|
+
getSubForm: <P extends EntityPaths<T>>(
|
|
83
|
+
path: P,
|
|
84
|
+
options?: SubformOptions<PickEntity<T, P>>,
|
|
85
|
+
) => Form<PickEntity<T, P>>
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Type `ErrorBag`
|
|
90
|
+
The errors in the form are structured in an `ErrorBag` object. Most errors are tied to properties in the form. However,
|
|
91
|
+
there might be cases where there are errors, that cannot be tied to one property. To account for that the `ErrorBag` satisfies the following interface:
|
|
92
|
+
```typescript
|
|
93
|
+
interface ErrorBag {
|
|
94
|
+
// an array of general error messages not tied to a specific property
|
|
95
|
+
general: string[] | undefined
|
|
96
|
+
// a record of property paths to arrays of error messages
|
|
97
|
+
// nested properties are dot-separated and array indices are just numbers, e.g. "person.address.street" or "person.hobbies.0.name"
|
|
98
|
+
// for subforms the errors will be all errors that start with the path of the subform
|
|
99
|
+
propertyErrors: Record<string, ValidationErrorMessage[] | undefined>
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Component `FormPart`
|
|
104
|
+
A component to define a subform part for a nested object or array property of the form data. This corresponds to the
|
|
105
|
+
`getSubForm` method of the `Form<T>` object, but in component form to be easily used in the template. An example usage is as follows:
|
|
106
|
+
```vue
|
|
107
|
+
<template>
|
|
108
|
+
<FormPart :form="form" path="person.address" #="{ subform }">
|
|
109
|
+
<MyAdddressComponent :form="subform" />
|
|
110
|
+
</FormPart>
|
|
111
|
+
</template>
|
|
112
|
+
```
|
|
113
|
+
Here, the `subform` will be a `Form` object with the type of the `address` property of the `person` object of the main form data.
|
|
114
|
+
Note that the errors of the subform will only contain the errors that start with the path of the subform.
|
|
115
|
+
|
|
116
|
+
## Component `Field`
|
|
117
|
+
This is a helper component to access form fields. It corresponds to the `defineField` method of the `Form<T>` object,
|
|
118
|
+
but in component form to be easily used in the template.
|
|
119
|
+
|
|
120
|
+
An example usage is as follows:
|
|
121
|
+
```vue
|
|
122
|
+
<template>
|
|
123
|
+
<Field :form="form" path="firstName" #="{ data, setData, onBlur, errors }">
|
|
124
|
+
<input :value="data" @input="setData" @blur="onBlur" />
|
|
125
|
+
<div v-if="errors.length > 0">
|
|
126
|
+
<span v-for="error in errors" :key="error">{{ error }}</span>
|
|
127
|
+
</div>
|
|
128
|
+
</Field>
|
|
129
|
+
</template>
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Note: it is recommended to use the `FormFieldWrapper` component and create a reusable form input component instead.
|
|
133
|
+
|
|
134
|
+
## Component `FormFieldWrapper`
|
|
135
|
+
This component is a helper component to easily create form input components based on plain input components.
|
|
136
|
+
See the [FormInput Example](./FormInput-example.md) for details and an example implementation of a form input component.
|
package/package.json
CHANGED
package/src/utils/path.ts
CHANGED
|
@@ -19,9 +19,6 @@ export function getNestedValue<T, K extends Paths<T>>(obj: T, path: K | SplitPat
|
|
|
19
19
|
|
|
20
20
|
export function setNestedValue<T, K extends Paths<T>>(obj: T, path: K | SplitPath<K>, value: PickProps<T, K>): void {
|
|
21
21
|
const keys = Array.isArray(path) ? path : splitPath(path)
|
|
22
|
-
if (keys.length === 0) {
|
|
23
|
-
throw new Error('Path cannot be empty')
|
|
24
|
-
}
|
|
25
22
|
|
|
26
23
|
const lastKey = keys.at(-1)!
|
|
27
24
|
const target = keys
|
|
@@ -32,7 +29,7 @@ export function setNestedValue<T, K extends Paths<T>>(obj: T, path: K | SplitPat
|
|
|
32
29
|
obj as Record<string, any>,
|
|
33
30
|
)
|
|
34
31
|
|
|
35
|
-
target[lastKey] = value
|
|
32
|
+
target[lastKey] = lastKey ? value : target
|
|
36
33
|
}
|
|
37
34
|
|
|
38
35
|
export const getLens = <T, K extends Paths<T>>(data: MaybeRef<T>, key: MaybeRef<K | SplitPath<K>>) => {
|