Skip to content

Commit c45c24e

Browse files
author
Lionel Bijaoui
committed
Add a "clean" CSS class for untouched fields
- Add "clean" when the value is not manually changed or validated - If changed or validated, loose the clean state and is either 'valid" or "error" - Class name can be customised with "validationCleanClass" - use internal "vfgTouched" value to track state of field
1 parent b02d866 commit c45c24e

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/fields/abstractField.js

+12
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export default {
4646
const fieldUID = uniqueId(this.fieldID + "_");
4747
return {
4848
fieldUID,
49+
touched: false,
4950
errors: [],
5051
debouncedValidateFunc: null,
5152
debouncedFormatFunction: null
@@ -75,6 +76,8 @@ export default {
7576
},
7677

7778
set(newValue) {
79+
this.touch();
80+
7881
let oldValue = this.value;
7982
newValue = this.formatValueToModel(newValue);
8083

@@ -133,6 +136,8 @@ export default {
133136
}
134137
},
135138
validate() {
139+
this.touch();
140+
136141
this.clearValidationErrors();
137142
let validateAsync = objGet(this.formOptions, "validateAsync", false);
138143

@@ -277,6 +282,13 @@ export default {
277282

278283
formatValueToModel(value) {
279284
return value;
285+
},
286+
287+
touch() {
288+
if (!this.touched) {
289+
this.touched = true;
290+
this.$emit("field-touched");
291+
}
280292
}
281293
},
282294
created() {

src/formElement.vue

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</label>
77

88
<div class="field-wrap">
9-
<component ref="child" :is="fieldType" :model="model" :schema="field" :formOptions="options" :eventBus="eventBus" :fieldID="fieldID" @errors-updated="onChildValidated"></component>
9+
<component ref="child" :is="fieldType" :model="model" :schema="field" :formOptions="options" :eventBus="eventBus" :fieldID="fieldID" @field-touched="onFieldTouched" @errors-updated="onChildValidated"></component>
1010
<div v-if="buttonsAreVisible" class="buttons">
1111
<button v-for="(btn, index) in field.buttons" @click="buttonClickHandler(btn, field, $event)" :class="btn.classes" :key="index" v-text="btn.label"></button>
1212
</div>
@@ -51,7 +51,8 @@ export default {
5151
},
5252
data() {
5353
return {
54-
childErrors: []
54+
childErrors: [],
55+
childTouched: false
5556
};
5657
},
5758
computed: {
@@ -84,7 +85,8 @@ export default {
8485
fieldRowClasses() {
8586
let baseClasses = {
8687
[objGet(this.options, "validationErrorClass", "error")]: this.fieldHasErrors,
87-
[objGet(this.options, "validationSuccessClass", "valid")]: !this.fieldHasErrors,
88+
[objGet(this.options, "validationSuccessClass", "valid")]: !this.fieldHasErrors && this.childTouched,
89+
[objGet(this.options, "validationCleanClass", "clean")]: !this.fieldHasErrors && !this.childTouched,
8890
disabled: this.getValueFromOption(this.field, "disabled"),
8991
readonly: this.getValueFromOption(this.field, "readonly"),
9092
featured: this.getValueFromOption(this.field, "featured"),
@@ -119,6 +121,9 @@ export default {
119121
buttonClickHandler(btn, field, event) {
120122
return btn.onclick.call(this, this.model, field, event, this);
121123
},
124+
onFieldTouched() {
125+
this.childTouched = true;
126+
},
122127
onChildValidated(errors) {
123128
this.childErrors = errors;
124129
}

0 commit comments

Comments
 (0)