Skip to content

Commit c39603d

Browse files
committed
Forms
1 parent b738a21 commit c39603d

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
<p>Cart ({{ cart.length }})</p>
1616
</div>
1717

18-
<product :premium="premium" :details="details" @add-to-cart="updateCart" @remove-from-cart="removeFromCart"></product>
18+
<product
19+
:premium="premium"
20+
:details="details"
21+
@add-to-cart="updateCart"
22+
@remove-from-cart="removeFromCart"
23+
></product>
1924
</div>
2025

2126
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.7/dist/vue.js"></script>

index.js

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ Vue.component('product', {
5050
</button>
5151
<button @click="removeFromCart">Remove from cart</button>
5252
</div>
53+
54+
<div>
55+
<h2>Reviews</h2>
56+
57+
<p v-if="!reviews.length">There are no reviews yet.</p>
58+
59+
<ul>
60+
<li v-for="review in reviews">
61+
<p>{{ review.name }}</p>
62+
63+
<p>Rating: {{ review.rating }}</p>
64+
65+
<p>Recommended: {{ review.recommended }}</p>
66+
67+
<p>{{ review.review }}</p>
68+
</li>
69+
</ul>
70+
</div>
71+
72+
<productReview @review-submitted="addReview"></productReview>
5373
</div>
5474
`,
5575
data() {
@@ -73,7 +93,8 @@ Vue.component('product', {
7393
image: '/img/vmSocks-green-onWhite.jpg',
7494
quantity: 2
7595
}
76-
]
96+
],
97+
reviews: []
7798
};
7899
},
79100
methods: {
@@ -88,6 +109,9 @@ Vue.component('product', {
88109
},
89110
updateProduct(index) {
90111
this.selectedVariant = index;
112+
},
113+
addReview(productReview) {
114+
this.reviews.push(productReview);
91115
}
92116
},
93117
computed: {
@@ -120,6 +144,86 @@ Vue.component('productDetails', {
120144
`
121145
});
122146

147+
Vue.component('productReview', {
148+
template: `
149+
<form class="review-form" @submit.prevent="onSubmit">
150+
<p v-if="errors.length">
151+
<b>Please correct the following error(s):</b>
152+
153+
<ul>
154+
<li v-for="error in errors">{{ error }}</li>
155+
</ul>
156+
</p>
157+
158+
<p>
159+
<label for="name">Name:</label>
160+
<input id="name" v-model="name" placeholder="name" />
161+
</p>
162+
163+
<p>
164+
<label for="recommended">Will you recommend this product?</label>
165+
<div>yes: <input id="recommended" v-model="recommended" type="radio" value="yes" /></div>
166+
<div>no: <input id="recommended" v-model="recommended" type="radio" value="no" /></div>
167+
</p>
168+
169+
<p>
170+
<label for="review">Review:</label>
171+
<textarea id="review" v-model="review"></textarea>
172+
</p>
173+
174+
<p>
175+
<label for="rating">Rating:</label>
176+
<select id="rating" v-model.number="rating">
177+
<option>5</option>
178+
<option>4</option>
179+
<option>3</option>
180+
<option>2</option>
181+
<option>1</option>
182+
</select>
183+
</p>
184+
185+
<p>
186+
<input type="submit" value="Submit" />
187+
</p>
188+
</form>
189+
`,
190+
data() {
191+
return {
192+
name: null,
193+
rating: null,
194+
review: null,
195+
recommended: null,
196+
errors: []
197+
};
198+
},
199+
methods: {
200+
onSubmit() {
201+
if (this.name && this.review && this.rating) {
202+
let productReview = {
203+
name: this.name,
204+
review: this.review,
205+
rating: this.rating,
206+
recommended: this.recommended
207+
};
208+
209+
this.$emit('review-submitted', productReview);
210+
211+
this.name = null;
212+
this.review = null;
213+
this.rating = null;
214+
this.recommended = null;
215+
} else {
216+
this.errors = [];
217+
218+
if (!this.name) this.errors.push('Name required.');
219+
if (!this.review) this.errors.push('Review required.');
220+
if (!this.rating) this.errors.push('Rating required.');
221+
if (!this.recommended) this.errors.push('Recomendation required.');
222+
}
223+
}
224+
}
225+
});
226+
123227
var app = new Vue({
124228
el: '#app',
125229
data: {

0 commit comments

Comments
 (0)