You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/forms.md
+39-25
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
# Form Handling
2
2
3
-
Forms in Vue can be as simple as plain HTML forms to complicated nested trees of custom Vue component form elements.
4
-
We will gradually go through the ways of interacting with form elements, setting values and triggering events.
3
+
Forms in Vue can be as simple as plain HTML forms to complicated nested trees of custom Vue component form elements.
4
+
We will gradually go through the ways of interacting with form elements, setting values and triggering events.
5
5
6
-
The methods we will be using the most are `setValue()` and `trigger()`.
6
+
The methods we will be using the most are `setValue()` and `trigger()`.
7
7
8
8
## Interacting with form elements
9
9
@@ -21,13 +21,13 @@ Let's take a look at a very basic form:
21
21
<script>
22
22
export default {
23
23
data() {
24
-
return {
24
+
return {
25
25
email: ''
26
26
}
27
27
},
28
28
methods: {
29
29
submit() {
30
-
this.$emit('submit', this.email)
30
+
this.$emit('submit', this.email)
31
31
}
32
32
}
33
33
}
@@ -62,7 +62,7 @@ Triggering events is the second most important action when working with forms an
62
62
63
63
```html
64
64
<button@click="submit">Submit</button>
65
-
```
65
+
```
66
66
67
67
To trigger a click event, we can use the `trigger` event.
68
68
@@ -80,9 +80,11 @@ test('trigger', async () => {
80
80
81
81
> If you haven't seen `emitted()` before, don't worry. It's used to assert the emitted events of a Component. You can learn more in [Event Handling](/guide/event-handling).
82
82
83
-
We can then assert that some action has been performed, like if the emitted event has been called.
83
+
We trigger the `click` event listener, so that the Component executes the `submit` method. As we did with `setValue`, we use `await` to make sure the action is being reflected by Vue.
84
+
85
+
We can then assert that some action has happened. In this case, that we emitted the right event.
84
86
85
-
Let's combine these two to test whether our simple form is emitting the email the user inputs.
87
+
Let's combine these two to test whether our simple form is emitting the user inputs.
86
88
87
89
```js
88
90
test('emits the input to its parent', async () => {
@@ -104,7 +106,7 @@ test('emits the input to its parent', async () => {
104
106
Now that we know the basics, let's dive into more complex examples.
105
107
106
108
### Working with various form elements
107
-
109
+
108
110
We saw `setValue` works with input elements, but is much more versatile, as it can set the value on various types of input elements.
109
111
110
112
Let's take a look at a more complicated form, which has more types of inputs.
@@ -125,16 +127,16 @@ Let's take a look at a more complicated form, which has more types of inputs.
As you can see, `setValue` is a very versatile method, it can work with all types of form elements.
176
+
As you can see, `setValue` is a very versatile method. It can work with all types of form elements.
174
177
175
-
We are using `await` everywhere, to make sure that each change has been applied before we trigger the next. This is a good practice to follow,
176
-
that can save you time later on, if you do assertions but changes are not applied to the DOM yet.
178
+
We are using `await` everywhere, to make sure that each change has been applied before we trigger the next. This is recommended to make sure you do assertions when the DOM has updated.
177
179
178
180
179
181
::: tip
180
-
If you dont pass a parameter to `setValue` for `OPTION`, `CHECKBOX` or `RADIO` inputs, they will set as `checked`.
182
+
If you don't pass a parameter to `setValue` for `OPTION`, `CHECKBOX` or `RADIO` inputs, they will set as `checked`.
181
183
:::
182
184
183
185
We have set values in our form, now it's time to submit the form and do some assertions.
@@ -232,17 +234,19 @@ Let's assume you have a very detailed and complex form, with special interaction
Assume we have an input that handles when the user clicks `cmd` + `c`, and we want to intercept and stop him from copying. Testing this is as easy as copy pasting the event.
237
+
Assume we have an input that handles when the user clicks `cmd` + `c`, and we want to intercept and stop him from copying. Testing this is as easy as copy & pasting the event from the Component to the `trigger()` method.
Here we assume our code checks inside the `event` object, whether the `relatedTarget` is a button or not. We can simply pass a reference to such an element,mimicking what would happen if the user clicks on a `button` after typing something in the `input`.
305
+
Here we assume our code checks inside the `event` object, whether the `relatedTarget` is a button or not. We can simply pass a reference to such an element,mimicking what would happen if the user clicks on a `button` after typing something in the `input`.
295
306
296
307
## Interacting with Vue Component inputs
297
308
298
-
Inputs are not only plain elements. We often use Vue components that function like inputs. They can add markup, styling and alot of functionality, in an easy to use format.
309
+
Inputs are not only plain elements. We often use Vue components that behave like inputs. They can add markup, styling and lots of functionalities in an easy to use format.
299
310
300
311
Testing forms that use such inputs can be daunting at first, but with a few simple rules, it quickly becomes a walk in the park.
301
312
302
-
Take this simple input for example.
313
+
Following is a Component that wraps a `label` and an `input` element:
303
314
304
315
```vue
305
316
<template>
@@ -312,14 +323,17 @@ Take this simple input for example.
312
323
>
313
324
</label>
314
325
</template>
326
+
315
327
<script>
316
328
export default {
329
+
name: 'CustomInput',
330
+
317
331
props: ['modelValue', 'label']
318
332
}
319
333
</script>
320
334
```
321
335
322
-
This Vue component adds a label and emits back whatever you type. To use it you just do:
336
+
This Vue component also emits back whatever you type. To use it you do:
0 commit comments