Skip to content

Commit 1adc7dd

Browse files
committed
feat(MdField): add required fields and error messages
1 parent 5a106cf commit 1adc7dd

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

docs/app/pages/Components/Input/Input.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<example src="./examples/TextFields.vue" />
2+
<example src="./examples/ErrorsMessages.vue" />
23

34
<template>
45
<page-container centered :title="$t('pages.input.title')">
@@ -11,6 +12,12 @@
1112

1213
<code-example title="Input and Textarea" :component="examples['text-fields']" />
1314
</div>
15+
16+
<div class="page-container-section">
17+
<h2>Errors and Messages</h2>
18+
19+
<code-example title="Validation" :component="examples['errors-messages']" />
20+
</div>
1421
</page-container>
1522
</template>
1623

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<template>
2+
<div>
3+
<md-field>
4+
<label>Required Field</label>
5+
<md-input required />
6+
</md-field>
7+
8+
<md-field class="md-invalid">
9+
<label>Error</label>
10+
<md-input required />
11+
<span class="md-error">There is an error</span>
12+
</md-field>
13+
14+
<md-field class="md-invalid">
15+
<label>Textarea</label>
16+
<md-textarea required />
17+
<span class="md-error">There is an error</span>
18+
</md-field>
19+
</div>
20+
</template>
21+
22+
<script>
23+
export default {
24+
name: 'ErrorsMessages'
25+
}
26+
</script>

src/components/MdField/MdField.vue

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
value: null,
2121
focused: false,
2222
disabled: false,
23+
required: false,
2324
placeholder: false,
2425
textarea: false,
2526
autogrow: false
@@ -188,6 +189,10 @@
188189
}
189190
190191
.md-field {
192+
+ .md-has-textarea:not(.md-autogrow) {
193+
margin-top: 36px;
194+
}
195+
191196
&.md-has-placeholder {
192197
label {
193198
pointer-events: auto;
@@ -203,8 +208,6 @@
203208
}
204209
205210
&.md-has-textarea:not(.md-autogrow) {
206-
padding-top: 0;
207-
208211
&:after {
209212
display: none;
210213
}
@@ -227,7 +230,7 @@
227230
228231
textarea {
229232
min-height: 100px;
230-
padding: 16px 16px 0;
233+
padding: 0 16px;
231234
}
232235
233236
&:hover,
@@ -244,7 +247,7 @@
244247
}
245248
246249
textarea {
247-
padding-top: 26px;
250+
padding-top: 10px;
248251
}
249252
}
250253
}
@@ -348,6 +351,12 @@
348351
}
349352
350353
&.md-invalid {
354+
&.md-has-textarea:not(.md-autogrow) {
355+
&:before {
356+
border-width: 2px;
357+
}
358+
}
359+
351360
.md-error {
352361
opacity: 1;
353362
transform: translate3d(0, 0, 0);
@@ -366,7 +375,6 @@
366375
right: 0;
367376
transform: translateX(calc(100% + 2px));
368377
content: "*";
369-
font-size: 12px;
370378
line-height: 1em;
371379
vertical-align: top;
372380
}

src/components/MdField/MdFieldMixin.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export default {
2929
},
3030
disabled () {
3131
this.setDisabled()
32+
},
33+
required () {
34+
this.setRequired()
3235
}
3336
},
3437
methods: {
@@ -41,6 +44,9 @@ export default {
4144
setDisabled () {
4245
this.state.disabled = Boolean(this.disabled)
4346
},
47+
setRequired () {
48+
this.state.required = Boolean(this.required)
49+
},
4450
onFocus () {
4551
this.state.focused = true
4652
},
@@ -55,5 +61,6 @@ export default {
5561
this.setStateValue()
5662
this.setPlaceholder()
5763
this.setDisabled()
64+
this.setRequired()
5865
}
5966
}

src/components/MdField/theme.scss

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
}
1919
}
2020

21-
.md-error {
22-
@include md-theme-property-by-hue(color, red, A400, field, error);
23-
}
24-
2521
&.md-focused,
2622
&.md-has-value {
2723
input,
@@ -91,6 +87,26 @@
9187
}
9288
}
9389

90+
&.md-invalid {
91+
&:after {
92+
@include md-theme-property-by-hue(background-color, red, A400, field, error);
93+
}
94+
95+
&.md-has-textarea:not(.md-autogrow) {
96+
&:before {
97+
@include md-theme-property-by-hue(border-color, red, A400, field, error);
98+
}
99+
}
100+
101+
label {
102+
@include md-theme-property-by-hue(color, red, A400, field, error);
103+
}
104+
105+
.md-error {
106+
@include md-theme-property-by-hue(color, red, A400, field, error);
107+
}
108+
}
109+
94110
&.md-focused {
95111
&:before {
96112
@include md-theme-property(background-color, primary);

0 commit comments

Comments
 (0)