Skip to content

Commit 932f580

Browse files
authored
Merge pull request #7 from denOldTimer/chapter-five
chapter five
2 parents 4645f94 + cd537c0 commit 932f580

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,28 @@ Lifecycle hooks : { [beforeCreated], [created], [beforeMount], [mounted],
150150

151151
---
152152

153+
## Basics : Chapter 5 : Forms & Handling USER input
154+
155+
1. create a form
156+
2. create a label & textarea
157+
3. create a select & dynamic options -
158+
- <option :value="option.value" v-for="(option, index) in twootTypes" :key="index">
159+
- the v-for is looping a list of options named `twootTyps`
160+
- the v-for requires a key - value pair
161+
- twootTypes doesn't have a dedicated key (ex. id) so we add the index of the array as key
162+
4. the [v-model] attribute for handeling form data
163+
- v-model creates a sync link between de form input and a data component/variable/placeholder
164+
- in our ex. textarea is v-model with data-> newTwootContent
165+
5. the submit
166+
- normal submits refresh the html page, so to stop this we use `prevent`
167+
- `@submit.prevent="createNewTwoot"`
168+
- if we prevent, we have to replace it with something else `createNewTwoot`
169+
- [Mutation Methods] :
170+
- unshift => puts the item @ the top of the list = LIFO {last in, first out}
171+
- push => pust the item @ the back of the list LILO {last in, last out}
172+
173+
---
174+
153175
[data]: https://v3.vuejs.org/api/options-data.html#data-2
154176
[props]: https://v3.vuejs.org/api/options-data.html#props
155177
[computed]: https://v3.vuejs.org/api/options-data.html#computed
@@ -172,3 +194,5 @@ Lifecycle hooks : { [beforeCreated], [created], [beforeMount], [mounted],
172194
[conditionals]: https://v3.vuejs.org/guide/conditional.html
173195
[v-if]: https://v3.vuejs.org/guide/conditional.html#v-if
174196
[v-for]: https://v3.vuejs.org/guide/list.html#mapping-an-array-to-elements-with-v-for
197+
[v-model]: https://v3.vuejs.org/api/directives.html#v-model
198+
[mutation methods]: https://v3.vuejs.org/guide/list.html#mutation-methods

src/components/UserProfile.vue

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@
66
<div class="user-profile__follower-count">
77
<strong>Followers: </strong>{{ followers }}
88
</div>
9+
<form class="user-profile__create-twoot" @submit.prevent="createNewTwoot">
10+
<label for="newTwoot"><strong>New Twoot</strong></label>
11+
<textarea id="newTwoot" rows="4" v-model="newTwootContent"></textarea>
12+
13+
<div class="user-profiel__create-twoot-type">
14+
<label for="newTwootType"><strong>Type</strong></label>
15+
<select id="newTwootType" v-model="selectedTwootType">
16+
<option
17+
:value="option.value"
18+
v-for="(option, index) in twootTypes"
19+
:key="index"
20+
>
21+
{{ option.name }}
22+
</option>
23+
</select>
24+
</div>
25+
<button>Twoot!</button>
26+
</form>
927
</div>
1028
<div class="user-profile__twoots-wrapper">
1129
<TwootItem
@@ -26,6 +44,12 @@ export default {
2644
components: { TwootItem },
2745
data(){
2846
return {
47+
newTwootContent: '',
48+
selectedTwootType: 'instant', //default => instant
49+
twootTypes: [
50+
{value: 'draft', name: 'Draft'},
51+
{value: 'instant', name: 'Instant Twoot'}
52+
],
2953
followers: 0,
3054
user: {
3155
id: 1,
@@ -62,6 +86,16 @@ export default {
6286
},
6387
toggleFavourite(id){
6488
console.log(`Favourite Twoot #${id}`)
89+
},
90+
createNewTwoot(){
91+
// If it it is a draft don't do anything
92+
if(this.newTwootContent && this.selectedTwootType !== 'draft'){
93+
this.user.twoots.unshift( {
94+
id: this.user.twoots.length + 1,
95+
content: this.newTwootContent
96+
})
97+
this.newTwootContent = ''
98+
}
6599
}
66100
},
67101
mounted(){
@@ -101,6 +135,11 @@ export default {
101135
display: grid;
102136
grid-gap: 10px;
103137
}
138+
.user-profile__create-twoot {
139+
padding-top: 20px;
140+
display: flex;
141+
flex-direction: column;
142+
}
104143
105144
h1 {
106145
margin: 0;

0 commit comments

Comments
 (0)