Skip to content

Commit fd37785

Browse files
NataliaTepluhinayyx990803
authored andcommitted
feat: started lesson 3
1 parent dd7a825 commit fd37785

File tree

6 files changed

+132
-2
lines changed

6 files changed

+132
-2
lines changed

src/tutorial/src/step-2/description.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ Now we created a _two-way binding_: we update `input` field on reactive property
3535
Now, we can choose how many colors we'll have in our guessing game! As a last touch, let's add a label to our input field and set up minimum and maximum for it:
3636

3737
```html
38-
<label for="squares">
38+
<label for="colors">
3939
Enter number of colors and press Enter:
40-
<input id="squares" type="number" v-model="squaresNumber" min="2" max="10" />
40+
<input id="colors" type="number" v-model="colorsNumber" min="2" max="10" />
4141
</label>
4242
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ref } from 'vue'
2+
3+
export default {
4+
name: 'App',
5+
setup() {
6+
const colorsNumber = ref(3)
7+
8+
return { colorsNumber }
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
name: 'App',
3+
data() {
4+
return {
5+
colorsNumber: 3
6+
}
7+
}
8+
}

src/tutorial/src/step-3/App/style.css

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
* {
2+
margin: 0;
3+
padding: 0;
4+
}
5+
6+
#app {
7+
font-family: Avenir, Helvetica, Arial, sans-serif;
8+
-webkit-font-smoothing: antialiased;
9+
-moz-osx-font-smoothing: grayscale;
10+
text-align: center;
11+
color: #2c3e50;
12+
}
13+
14+
label {
15+
font-size: 18px;
16+
}
17+
18+
input {
19+
font-size: 18px;
20+
padding-left: 5px;
21+
width: 35px;
22+
}
23+
24+
.panel {
25+
color: white;
26+
background-color: teal;
27+
transition: background 0.5s;
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<main>
2+
<div class="panel">
3+
<h1>Guess the Color</h1>
4+
<h2>Number of colors: {{ colorsNumber }}</h2>
5+
<label for="colors">
6+
Enter number of colors and press Enter:
7+
<input id="colors" type="number" v-model="colorsNumber" min="2" max="10" />
8+
</label>
9+
</div>
10+
</main>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Conditional Rendering
2+
3+
Now, when we have defined how many colors we will show in the game, it's time to actually make our game start. For now, we want to have two states:
4+
5+
- game is not started: we are displaying our input field so user can define the number of colors to guess;
6+
- game is started: for now, we want to hide the input field and we want to display a text "Game is started".
7+
8+
Let's start with creating one more reactive property - `gameStarted`:
9+
10+
<div class="composition-api">
11+
12+
```js{2}
13+
const colorsNumber = ref(3)
14+
const gameStarted = ref(false)
15+
```
16+
17+
</div>
18+
19+
<div class="options-api">
20+
21+
```js{4}
22+
data() {
23+
return {
24+
colorsNumber: 3,
25+
gameStarted: false
26+
}
27+
}
28+
```
29+
30+
</div>
31+
32+
Now, we can add a new section to our component template:
33+
34+
```html{4}
35+
<div class="panel">
36+
<!-- header content -->
37+
</div>
38+
<section class="flex" v-if="gameStarted">Game is started</section>
39+
```
40+
41+
Try to change `gameStarted` to `true` and you will see a text rendered below the header.
42+
43+
Let's start our game as soon as user submits the form with pressing <kbd>Enter</kbd> on the input field. To make this more semantic, let's wrap our input in `<form>` tag:
44+
45+
```html
46+
<form>
47+
<label for="colors">
48+
Enter number of colors and press Enter:
49+
<input id="colors" type="number" v-model="colorsNumber" />
50+
</label>
51+
</form>
52+
```
53+
54+
Form with the single input will be submitted on <kbd>Enter</kbd> press automatically. We only need to listen to the form's submit event:
55+
56+
```html
57+
<form @submit="gameStarted = true">
58+
<label for="colors">
59+
Enter number of colors and press Enter:
60+
<input id="colors" type="number" v-model="colorsNumber" />
61+
</label>
62+
</form>
63+
```
64+
65+
However, if we leave it like this, page will refresh and no text will be shown. The reason is we need to _prevent a default form submit event_ and make our change instead. In JavaScript, it's done with `event.preventDefault()` method; in Vue.js we have an event modifier `.prevent` that does the same under the hood.
66+
67+
```html
68+
<form @submit.prevent="gameStarted = true">
69+
<label for="colors">
70+
Enter number of colors and press Enter:
71+
<input id="colors" type="number" v-model="colorsNumber" />
72+
</label>
73+
</form>
74+
```

0 commit comments

Comments
 (0)