Skip to content

Commit 4faf02d

Browse files
committed
Whitespace
1 parent 7aabae7 commit 4faf02d

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

src/guide/forms.md

+39-25
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Form Handling
22

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.
55

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()`.
77

88
## Interacting with form elements
99

@@ -21,13 +21,13 @@ Let's take a look at a very basic form:
2121
<script>
2222
export default {
2323
data() {
24-
return {
24+
return {
2525
email: ''
2626
}
2727
},
2828
methods: {
2929
submit() {
30-
this.$emit('submit', this.email)
30+
this.$emit('submit', this.email)
3131
}
3232
}
3333
}
@@ -62,7 +62,7 @@ Triggering events is the second most important action when working with forms an
6262

6363
```html
6464
<button @click="submit">Submit</button>
65-
```
65+
```
6666

6767
To trigger a click event, we can use the `trigger` event.
6868

@@ -80,9 +80,11 @@ test('trigger', async () => {
8080

8181
> 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).
8282
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.
8486

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.
8688

8789
```js
8890
test('emits the input to its parent', async () => {
@@ -104,7 +106,7 @@ test('emits the input to its parent', async () => {
104106
Now that we know the basics, let's dive into more complex examples.
105107

106108
### Working with various form elements
107-
109+
108110
We saw `setValue` works with input elements, but is much more versatile, as it can set the value on various types of input elements.
109111

110112
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.
125127
126128
<input type="radio" value="weekly" v-model="form.interval"/>
127129
<input type="radio" value="monthly" v-model="form.interval"/>
128-
130+
129131
<button type="submit">Submit</button>
130132
</form>
131133
</template>
132134
133135
<script>
134136
export default {
135137
data() {
136-
return {
137-
form: {
138+
return {
139+
form: {
138140
email: '',
139141
description: '',
140142
city: '',
@@ -145,7 +147,7 @@ export default {
145147
},
146148
methods: {
147149
async submit() {
148-
this.$emit('submitted')
150+
this.$emit('submit', this.form)
149151
}
150152
}
151153
}
@@ -162,6 +164,7 @@ import FormComponent from './FormComponent.vue'
162164

163165
test('submits a form', async () => {
164166
const wrapper = mount(FormComponent)
167+
165168
await wrapper.find('input[type=email]').setValue('[email protected]')
166169
await wrapper.find('textarea').setValue('Lorem ipsum dolor sit amet')
167170
await wrapper.find('select').setValue('moscow')
@@ -170,14 +173,13 @@ test('submits a form', async () => {
170173
})
171174
```
172175

173-
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.
174177

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.
177179

178180

179181
::: 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`.
181183
:::
182184

183185
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
232234
<input @keydown.meta.c.exact.prevent="captureCopy" v-model="input" />
233235
```
234236

235-
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.
236238

237239
```js
238240
test('handles complex events', async () => {
239241
const wrapper = mount(Component)
242+
240243
await wrapper.find(input).trigger('keydown.meta.c.exact.prevent')
241-
// assert something
244+
245+
// run your assertions
242246
})
243247
```
244248

245-
VTU will read the even and apply the appropriate properties to the event object. In this case it will match something like this:
249+
Vue Test Utils reads the event and applies the appropriate properties to the event object. In this case it will match something like this:
246250

247251
```js
248252
{
@@ -255,13 +259,15 @@ VTU will read the even and apply the appropriate properties to the event object.
255259
#### Adding extra data to an event
256260

257261
Let's say your code needs something from inside the `event` object. You can test such scenarios by passing extra data as a second parameter.
262+
258263
```vue
259264
<template>
260265
<form>
261266
<input type="text" v-model="value" @blur="handleBlur">
262267
<button>Submit</button>
263268
</form>
264269
</template>
270+
265271
<script>
266272
export default {
267273
data() {
@@ -271,35 +277,40 @@ export default {
271277
},
272278
methods: {
273279
handleBlur(event) {
274-
if(event.relatedTarget.tagName === 'BUTTON'){
280+
if (event.relatedTarget.tagName === 'BUTTON') {
275281
this.$emit('focus-lost')
276282
}
277283
}
278284
}
279285
}
280286
</script>
281287
```
288+
282289
```js
283290
import Form from './Form.vue'
291+
284292
test('emits an event only if you lose focus to a button', () => {
285293
const wrapper = mount(Form)
294+
286295
const componentToGetFocus = wrapper.find('button')
296+
287297
wrapper.find('input').trigger('blur', {
288298
relatedTarget: componentToGetFocus
289299
})
300+
290301
expect(wrapper.emitted('focus-lost')).toBeTruthy()
291302
})
292303
```
293304

294-
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`.
295306

296307
## Interacting with Vue Component inputs
297308

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.
299310

300311
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.
301312

302-
Take this simple input for example.
313+
Following is a Component that wraps a `label` and an `input` element:
303314

304315
```vue
305316
<template>
@@ -312,14 +323,17 @@ Take this simple input for example.
312323
>
313324
</label>
314325
</template>
326+
315327
<script>
316328
export default {
329+
name: 'CustomInput',
330+
317331
props: ['modelValue', 'label']
318332
}
319333
</script>
320334
```
321335

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:
323337

324338
```html
325339
<custom-input v-model="input" label="Text Input" class="text-input"/>

0 commit comments

Comments
 (0)