@volverjs/form-vue 0.0.10-beta.6 → 0.0.10-beta.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/README.md CHANGED
@@ -45,8 +45,8 @@ import { createForm } from '@volverjs/form-vue'
45
45
  import { z } from 'zod'
46
46
 
47
47
  const schema = z.object({
48
- name: z.string(),
49
- surname: z.string()
48
+ firstName: z.string(),
49
+ lastName: z.string()
50
50
  })
51
51
 
52
52
  const app = createApp(App)
@@ -72,16 +72,16 @@ A `valid` or `invalid` event is emitted when the form status changes.
72
72
  ```vue
73
73
  <script lang="ts" setup>
74
74
  const onSubmit = (formData) => {
75
- // Do something with the form data
75
+ // ...
76
76
  }
77
77
  const onInvalid = (errors) => {
78
- // Do something with the errors
78
+ // ...
79
79
  }
80
80
  </script>
81
81
 
82
82
  <template>
83
83
  <VvForm @submit="onSubmit" @invalid="onInvalid">
84
- <!-- form fields -->
84
+ <!-- ... -->
85
85
  <button type="submit">Submit</button>
86
86
  </VvForm>
87
87
  </template>
@@ -96,7 +96,7 @@ The submit can be triggered programmatically with the `submit()` method.
96
96
 
97
97
  const formEl = ref<InstanceType<FormComponent>>(null)
98
98
  const onSubmit = (formData) => {
99
- // Do something with the form data
99
+ // ...
100
100
  }
101
101
  const submitForm = () => {
102
102
  formEl.value.submit()
@@ -105,54 +105,63 @@ The submit can be triggered programmatically with the `submit()` method.
105
105
 
106
106
  <template>
107
107
  <VvForm @submit="onSubmit" ref="formEl">
108
- <!-- form fields -->
108
+ <!-- ... -->
109
109
  </VvForm>
110
110
  <button type="button" @click.stop="submitForm">Submit</button>
111
111
  </template>
112
112
  ```
113
113
 
114
- Use the `v-model` directive (or only `:model-value` to set the initial value of form data) to bind the form data.
115
- The form data two way binding is throttled by default (500ms) to avoid performance issues.
116
- The throttle can be changed with the `updateThrottle` option.
114
+ Use the `v-model` directive (or only `:model-value` to set the initial value of form data) or bind the form data.
115
+
116
+ The form data two way binding is **throttled** by default (500ms) to avoid performance issues. The throttle can be changed with the `updateThrottle` option or prop.
117
+
118
+ By default form validation **stops** when a **valid state** is reached.
119
+ To activate **continuos validation** use the `continuosValidation` option or prop.
117
120
 
118
121
  ```vue
119
122
  <script lang="ts" setup>
120
123
  import { ref } from 'vue'
121
124
 
122
125
  const formData = ref({
123
- name: '',
124
- surname: ''
126
+ firstName: '',
127
+ lastName: ''
125
128
  })
126
129
  </script>
127
130
 
128
131
  <template>
129
- <VvForm v-model="formData">
130
- <!-- form fields -->
132
+ <VvForm v-model="formData" :update-throttle="1000" continuos-validation>
133
+ <!-- ... -->
131
134
  </VvForm>
132
135
  </template>
133
136
  ```
134
137
 
135
- The `continuosValidation` can be passed through options or with VvForm prop.
136
- With this field the validation doesn't stop and continue also after a validaton success.
138
+ ## Composable
139
+
140
+ `useForm` can be used to create a form programmatically inside a Vue 3 Component.
141
+ The **default settings** are **inherited** from the plugin (if it was defined).
137
142
 
138
143
  ```vue
139
144
  <script lang="ts" setup>
140
- import { ref } from 'vue'
145
+ import { useForm } from '@volverjs/form-vue'
146
+ import { z } from 'zod'
141
147
 
142
- const { VvForm, VvFormField } = useForm(MyZodSchema, {
143
- lazyLoad: true
144
- // continuosValidation: true
148
+ const schema = z.object({
149
+ firstName: z.string(),
150
+ lastName: z.string()
145
151
  })
146
152
 
147
- const formData = ref({
148
- name: '',
149
- surname: ''
153
+ const { VvForm, VvFormWrapper, VvFormField } = useForm(schema, {
154
+ // lazyLoad: boolean - default false
155
+ // updateThrottle: number - default 500
156
+ // continuosValidation: boolean - default false
157
+ // sideEffects?: (formData: any) => void
150
158
  })
151
159
  </script>
152
160
 
153
161
  <template>
154
- <VvForm v-model="formData" :continuosValidation="true">
155
- <!-- form fields -->
162
+ <VvForm>
163
+ <VvFormField type="text" name="firstName" label="First Name" />
164
+ <VvFormField type="text" name="lastName" label="Last Name" />
156
165
  </VvForm>
157
166
  </template>
158
167
  ```
@@ -181,7 +190,7 @@ The wrapper status is invalid if at least one of the fields inside it is invalid
181
190
  </template>
182
191
  ```
183
192
 
184
- `VvFormWrapper` can be used recursively to create a validation tree. The wrapper status is invalid if at least one of the fields inside it or one of its children is invalid.
193
+ `VvFormWrapper` can be used recursively to create a validation tree. The wrapper status is invalid if **at least one of the fields** inside it or one of its children **is invalid**.
185
194
 
186
195
  ```vue
187
196
  <template>
@@ -204,57 +213,28 @@ The wrapper status is invalid if at least one of the fields inside it is invalid
204
213
 
205
214
  ### VvFormField
206
215
 
207
- `VvFormField` allow you to render a [`@volverjs/ui-vue`](https://github.com/volverjs/ui-vue) input component inside a form. It automatically bind the form data through the `name` attribute.
216
+ `VvFormField` allow you to render a form field or a [`@volverjs/ui-vue`](https://github.com/volverjs/ui-vue) input component inside a form.
208
217
 
209
- ```vue
210
- <template>
211
- <VvForm>
212
- <VvFormField type="text" name="name" label="Name" />
213
- <VvFormField type="text" name="surname" label="Surname" />
214
- </VvForm>
215
- </template>
216
- ```
217
-
218
- For nested objects, use the `name` attribute with dot notation.
219
-
220
- ```vue
221
- <template>
222
- <VvForm>
223
- <VvFormField type="text" name="shipping.address" label="Shipping address" />
224
- </VvForm>
225
- </template>
226
- ```
227
-
228
- The type of input component is defined by the `type` attribute.
229
- All the available input types are listed in the [VvFormField documentation](/docs/VvFormField.md).
230
-
231
- You can also use the `VvFormField` component to render a default slot without a `type` (default `type` is `custom`).
218
+ It automatically bind the form data through the `name` attribute. For nested objects, use the `name` attribute with **dot notation**.
232
219
 
233
220
  ```vue
234
221
  <template>
235
222
  <VvForm>
236
223
  <VvFormField
237
- v-slot="{
238
- modelValue,
239
- invalid,
240
- invalidLabel,
241
- formData,
242
- formErrors,
243
- errors,
244
- onUpdate
245
- }"
246
- name="surname"
224
+ v-slot="{ modelValue, invalid, invalidLabel, onUpdate }"
225
+ name="lastName"
247
226
  >
248
- <label for="surname">Surname</label>
227
+ <label for="lastName">Last Name</label>
249
228
  <input
250
- id="surname"
229
+ id="lastName"
251
230
  type="text"
231
+ name="lastName"
252
232
  :value="modelValue"
253
233
  :aria-invalid="invalid"
254
- :aria-errormessage="invalid ? 'surname-alert' : undefined"
234
+ :aria-errormessage="invalid ? 'last-name-alert' : undefined"
255
235
  @input="onUpdate"
256
236
  />
257
- <small v-if="invalid" role="alert" id="surname-alert">
237
+ <small v-if="invalid" role="alert" id="last-name-alert">
258
238
  {{ invalidLabel }}
259
239
  </small>
260
240
  </VvFormField>
@@ -262,104 +242,105 @@ You can also use the `VvFormField` component to render a default slot without a
262
242
  </template>
263
243
  ```
264
244
 
265
- Or a custom component.
245
+ To render a [`@volverjs/ui-vue`](https://github.com/volverjs/ui-vue) input component, use the `type` attribute.
266
246
 
267
247
  ```vue
268
- <script lang="ts" setup>
269
- import MyInput from './MyInput.vue'
270
- </script>
271
-
272
248
  <template>
273
249
  <VvForm>
274
- <VvFormField name="surname" :is="MyInput" />
250
+ <VvFormField type="text" name="username" label="Username" />
251
+ <VvFormField type="password" name="password" label="Password" />
275
252
  </VvForm>
276
253
  </template>
277
254
  ```
278
255
 
279
- ## Nested VvFormField
280
-
281
- In some use cases can be usefull nest `VvFormField`.
282
- For example let's assume:
283
-
284
- - a shopping list that is a field of our model (ex: ToDo list)
285
- - the sum of all products of the shopping list cannot be 0
286
- - we don't know all the products a priori
256
+ Check the [`VvFormField`](./docs/VvFormField.md) documentation to learn more about form fields.
287
257
 
288
- So our ToDo model and shopping list are structured like:
258
+ ## Form Template
289
259
 
290
- ```javascript
291
- const toDo = {
292
- shoppingList: {
293
- bread: 0,
294
- milk: 0,
295
- tomato: 0,
296
- potato: 0,
297
- ...
298
- }
299
- }
300
- ```
260
+ Forms can also be created using a template. A template is an **array of objects** that describes the form fields. All properties that are **not listed** below are passed to the component **as props**.
301
261
 
302
- Our Zod schema can be:
262
+ ```vue
263
+ <script lang="ts" setup>
264
+ import { useForm } from '@volverjs/form-vue'
265
+ import { z } from 'zod'
303
266
 
304
- ```typescript
305
- const toDoSchema = z.object({
306
- shoppingList: z
307
- .object({})
308
- .default({})
309
- .superRefine((value, ctx) => {
310
- const shoppingList = value as Record<string, number>
311
- if (
312
- Object.keys(value).length &&
313
- !Object.keys(value).find((key) => shoppingList[key] > 0)
314
- ) {
315
- ctx.addIssue({
316
- code: z.ZodIssueCode.custom,
317
- message: i18n.global.t('atLeastOneProduct')
318
- })
319
- }
267
+ const schema = z.object({
268
+ firstName: z.string(),
269
+ lastName: z.string(),
270
+ address: z.object({
271
+ street: z.string(),
272
+ number: z.string(),
273
+ city: z.string(),
274
+ zip: z.string()
320
275
  })
321
- })
322
- ```
323
-
324
- And the Vue component:
325
-
326
- ```vue
327
- <script setup lang="ts">
328
- const { VvForm, VvFormField } = useForm(toDoSchema, {
329
- lazyLoad: true,
330
- continuosValidation: true
331
276
  })
332
277
 
333
- // shopping list data, const or async
334
- const shoppingList = {
335
- bread: 0,
336
- milk: 0,
337
- tomato: 0,
338
- potato: 0
339
- }
278
+ const templateSchema = [
279
+ {
280
+ vvName: 'firstName',
281
+ vvType: 'text',
282
+ label: 'First Name'
283
+ },
284
+ {
285
+ vvName: 'lastName',
286
+ vvType: 'text',
287
+ label: 'Last Name'
288
+ },
289
+ {
290
+ vvIs: 'div',
291
+ class: 'grid grid-col-3 gap-4',
292
+ vvChildren: [
293
+ {
294
+ vvName: 'address.street',
295
+ vvType: 'text',
296
+ label: 'Street',
297
+ class: 'col-span-2'
298
+ },
299
+ {
300
+ vvName: 'address.number',
301
+ vvType: 'text',
302
+ label: 'Number'
303
+ },
304
+ {
305
+ vvName: 'address.city',
306
+ vvType: 'text',
307
+ label: 'City'
308
+ class: 'col-span-2',
309
+ },
310
+ {
311
+ vvName: 'address.zip',
312
+ vvType: 'number',
313
+ label: 'Zip'
314
+ }
315
+ ]
316
+ }
317
+ ]
318
+
319
+ const { VvForm, VvFormTemplate } = useForm(schema)
340
320
  </script>
341
321
 
342
322
  <template>
343
323
  <VvForm>
344
- <VvFormField v-slot="{ invalid, invalidLabel }" name="shoppingList">
345
- <VvFormField
346
- v-for="key in Object.keys(shoppingList)"
347
- :key="key"
348
- :name="`shoppingList.${key}`"
349
- :label="$t(key)"
350
- />
351
- <small v-if="invalid" class="input-counter__hint">{{
352
- invalidLabel[0]
353
- }}</small>
354
- </VvFormField>
324
+ <VvFormTemplate :schema="templateSchema" />
355
325
  </VvForm>
356
326
  </template>
357
327
  ```
358
328
 
359
- ## Composable
329
+ Template items, by default, are rendered as a `VvFormField` component but this can be changed using the `vvIs` property. The `vvIs` property can be a string or a component.
360
330
 
361
- `useForm` can be used to create a form programmatically inside a Vue 3 Component.
362
- The default settings are inherited from the plugin (if it was defined).
331
+ `vvName` refers to the name of the field in the schema and can be a nested property using **dot notation**.
332
+
333
+ `vvType` refers to the type of the field and can be any of the supported types.
334
+
335
+ `vvDefaultValue` can be used to set default values for the form item.
336
+
337
+ `vvShowValid` can be used to show the valid state of the form item.
338
+
339
+ `vvSlots` can be used to pass a slots to the template item.
340
+
341
+ `vvChildren` is an array of template items which will be wrapped in the parent item.
342
+
343
+ Conditional rendering can be achieved using the `vvIf` and `vvElseIf` properties.
363
344
 
364
345
  ```vue
365
346
  <script lang="ts" setup>
@@ -367,26 +348,108 @@ The default settings are inherited from the plugin (if it was defined).
367
348
  import { z } from 'zod'
368
349
 
369
350
  const schema = z.object({
370
- name: z.string(),
371
- surname: z.string()
351
+ firstName: z.string(),
352
+ lastName: z.string(),
353
+ hasUsername: z.boolean(),
354
+ username: z.string().optional()
355
+ email: z.string().email().optional()
356
+ }).superRefine((value, ctx) => {
357
+ if (value.hasUsername && !value.username) {
358
+ ctx.addIssue({
359
+ code: z.ZodIssueCode.custom,
360
+ message: 'Username is required'
361
+ })
362
+ }
363
+ if (!value.hasUsername && !value.email) {
364
+ ctx.addIssue({
365
+ code: z.ZodIssueCode.custom,
366
+ message: 'Email is required'
367
+ })
368
+ }
372
369
  })
373
370
 
374
- const { VvForm, VvFormWrapper, VvFormField } = useForm(schema, {
375
- // lazyLoad: boolean - default false
376
- // updateThrottle: number - default 500
377
- // continuosValidation: true - default false
378
- // sideEffects?: (formData: any) => void
379
- })
371
+ const templateSchema = [
372
+ {
373
+ vvName: 'firstName',
374
+ vvType: 'text',
375
+ label: 'First Name'
376
+ },
377
+ {
378
+ vvName: 'lastName',
379
+ vvType: 'text',
380
+ label: 'Last Name'
381
+ },
382
+ {
383
+ vvName: 'hasUsername',
384
+ vvType: 'checkbox',
385
+ label: 'Has Username'
386
+ value: true,
387
+ uncheckedValue: false
388
+ },
389
+ {
390
+ vvIf: 'hasUsername',
391
+ vvName: 'username',
392
+ vvType: 'text',
393
+ label: 'Username'
394
+ },
395
+ {
396
+ vvElseIf: true,
397
+ vvName: 'email',
398
+ vvType: 'email',
399
+ label: 'Email'
400
+ }
401
+ ]
402
+
403
+ const { VvForm, VvFormTemplate } = useForm(schema)
380
404
  </script>
381
405
 
382
406
  <template>
383
407
  <VvForm>
384
- <VvFormField type="text" name="name" label="Name" />
385
- <VvFormField type="text" name="surname" label="Surname" />
408
+ <VvFormTemplate :schema="templateSchema" />
386
409
  </VvForm>
387
410
  </template>
388
411
  ```
389
412
 
413
+ `vvElseIf` can be used multiple times. `vvElseIf: true` is like an `else` statement and will be rendered if all previous `vvIf` and `vvElseIf` conditions are false.
414
+
415
+ `vvIf` and `vvElseIf` can be a string or a function. If it is a string it will be evaluated as a **property** of the form data. If it is a function it will be called with the **form context** as the **first argument**.
416
+
417
+ ```ts
418
+ {
419
+ vvIf: (ctx) => ctx.formData.value.hasUsername,
420
+ vvName: 'username',
421
+ vvType: 'text',
422
+ label: 'Username'
423
+ }
424
+ ```
425
+
426
+ The template schema and all template items can be a function. The function will be called with the form context as the first argument.
427
+
428
+ ```ts
429
+ const templateSchema = (ctx) => [
430
+ {
431
+ vvName: 'firstName',
432
+ vvType: 'text',
433
+ label: `Hi ${ctx.formData.value.firstName}!`
434
+ }
435
+ ]
436
+ ```
437
+
438
+ ```ts
439
+ const templateSchema = [
440
+ (ctx) => ({
441
+ vvName: 'firstName',
442
+ vvType: 'text',
443
+ label: `Hi ${ctx.formData.value.firstName}!`
444
+ }),
445
+ {
446
+ vvName: 'username',
447
+ type: 'text',
448
+ label: 'username'
449
+ }
450
+ ]
451
+ ```
452
+
390
453
  ## Outside a Vue 3 Component
391
454
 
392
455
  `formFactory` can be used to create a form outside a Vue 3 Component.
@@ -401,20 +464,36 @@ const schema = z.object({
401
464
  surname: z.string()
402
465
  })
403
466
 
404
- const { VvForm, VvFormWrapper, VvFormField } = formFactory(schema, {
467
+ const {
468
+ VvForm,
469
+ VvFormWrapper,
470
+ VvFormField,
471
+ VvFormTemplate,
472
+ formData,
473
+ status,
474
+ errors
475
+ } = formFactory(schema, {
405
476
  // lazyLoad: boolean - default false
406
477
  // updateThrottle: number - default 500
407
- // continuosValidation: true - default false
478
+ // continuosValidation: boolean - default false
408
479
  // sideEffects?: (data: any) => void
409
480
  })
410
481
 
411
- export default { VvForm, VvFormWrapper, VvFormField }
482
+ export default {
483
+ VvForm,
484
+ VvFormWrapper,
485
+ VvFormField,
486
+ VvFormTemplate,
487
+ formData,
488
+ status,
489
+ errors
490
+ }
412
491
  ```
413
492
 
414
493
  ## Default Object by Zod Object Schema
415
494
 
416
- `defaultObjectBySchema` creates an object by a Zod Object Schema.
417
- It can be useful to create a default object for a form. The default object is created by the default values of the schema and can be merged with an other object passed as parameter.
495
+ `defaultObjectBySchema` creates an object by a [Zod Object Schema](https://zod.dev/?id=objects).
496
+ It can be useful to create a **default object** for a **form**. The default object is created by the default values of the schema and can be merged with an other object passed as parameter.
418
497
 
419
498
  ```ts
420
499
  import { z } from 'zod'
package/dist/VvForm.d.ts CHANGED
@@ -18,12 +18,17 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
18
18
  $data: {};
19
19
  $props: Partial<{
20
20
  modelValue: Record<string, any>;
21
+ updateThrottle: number;
21
22
  continuosValidation: boolean;
22
23
  }> & Omit<Readonly<import("vue").ExtractPropTypes<{
23
24
  modelValue: {
24
25
  type: ObjectConstructor;
25
26
  default: () => {};
26
27
  };
28
+ updateThrottle: {
29
+ type: NumberConstructor;
30
+ default: number;
31
+ };
27
32
  continuosValidation: {
28
33
  type: BooleanConstructor;
29
34
  default: boolean;
@@ -33,7 +38,7 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
33
38
  onValid?: ((...args: any[]) => any) | undefined;
34
39
  onSubmit?: ((...args: any[]) => any) | undefined;
35
40
  "onUpdate:modelValue"?: ((...args: any[]) => any) | undefined;
36
- } & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, "modelValue" | "continuosValidation">;
41
+ } & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, "modelValue" | "updateThrottle" | "continuosValidation">;
37
42
  $attrs: {
38
43
  [x: string]: unknown;
39
44
  };
@@ -52,6 +57,10 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
52
57
  type: ObjectConstructor;
53
58
  default: () => {};
54
59
  };
60
+ updateThrottle: {
61
+ type: NumberConstructor;
62
+ default: number;
63
+ };
55
64
  continuosValidation: {
56
65
  type: BooleanConstructor;
57
66
  default: boolean;
@@ -69,6 +78,7 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
69
78
  invalid: import("vue").ComputedRef<boolean>;
70
79
  }, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, ("invalid" | "valid" | "submit" | "update:modelValue")[], string, {
71
80
  modelValue: Record<string, any>;
81
+ updateThrottle: number;
72
82
  continuosValidation: boolean;
73
83
  }, {}, string> & {
74
84
  beforeCreate?: ((() => void) | (() => void)[]) | undefined;
@@ -95,6 +105,10 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
95
105
  type: ObjectConstructor;
96
106
  default: () => {};
97
107
  };
108
+ updateThrottle: {
109
+ type: NumberConstructor;
110
+ default: number;
111
+ };
98
112
  continuosValidation: {
99
113
  type: BooleanConstructor;
100
114
  default: boolean;
@@ -119,6 +133,10 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
119
133
  type: ObjectConstructor;
120
134
  default: () => {};
121
135
  };
136
+ updateThrottle: {
137
+ type: NumberConstructor;
138
+ default: number;
139
+ };
122
140
  continuosValidation: {
123
141
  type: BooleanConstructor;
124
142
  default: boolean;
@@ -136,6 +154,7 @@ export declare const defineForm: <Schema extends FormSchema>(schema: Schema, pro
136
154
  invalid: import("vue").ComputedRef<boolean>;
137
155
  }, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, ("invalid" | "valid" | "submit" | "update:modelValue")[], "invalid" | "valid" | "submit" | "update:modelValue", {
138
156
  modelValue: Record<string, any>;
157
+ updateThrottle: number;
139
158
  continuosValidation: boolean;
140
159
  }, {}, string> & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps & (new () => {
141
160
  $slots: {
@@ -25,7 +25,7 @@ export declare const defineFormField: <Schema extends FormSchema>(formProvideKey
25
25
  default: boolean;
26
26
  };
27
27
  defaultValue: {
28
- type: (ObjectConstructor | BooleanConstructor | StringConstructor | NumberConstructor | ArrayConstructor)[];
28
+ type: (ObjectConstructor | NumberConstructor | BooleanConstructor | StringConstructor | ArrayConstructor)[];
29
29
  default: undefined;
30
30
  };
31
31
  }, {
@@ -60,7 +60,7 @@ export declare const defineFormField: <Schema extends FormSchema>(formProvideKey
60
60
  default: boolean;
61
61
  };
62
62
  defaultValue: {
63
- type: (ObjectConstructor | BooleanConstructor | StringConstructor | NumberConstructor | ArrayConstructor)[];
63
+ type: (ObjectConstructor | NumberConstructor | BooleanConstructor | StringConstructor | ArrayConstructor)[];
64
64
  default: undefined;
65
65
  };
66
66
  }>> & {