Skip to content

Commit 91c662b

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 44511f9 commit 91c662b

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/fields/abstractField.js

Lines changed: 12 additions & 0 deletions
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

Lines changed: 8 additions & 3 deletions
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: {
@@ -93,7 +94,8 @@ export default {
9394
fieldRowClasses() {
9495
let baseClasses = {
9596
[objGet(this.options, "validationErrorClass", "error")]: this.fieldHasErrors,
96-
[objGet(this.options, "validationSuccessClass", "valid")]: !this.fieldHasErrors,
97+
[objGet(this.options, "validationSuccessClass", "valid")]: !this.fieldHasErrors && this.childTouched,
98+
[objGet(this.options, "validationCleanClass", "clean")]: !this.fieldHasErrors && !this.childTouched,
9799
disabled: this.getValueFromOption(this.field, "disabled"),
98100
readonly: this.getValueFromOption(this.field, "readonly"),
99101
featured: this.getValueFromOption(this.field, "featured"),
@@ -128,6 +130,9 @@ export default {
128130
buttonClickHandler(btn, field, event) {
129131
return btn.onclick.call(this, this.model, field, event, this);
130132
},
133+
onFieldTouched() {
134+
this.childTouched = true;
135+
},
131136
onChildValidated(errors) {
132137
this.childErrors = errors;
133138
}

0 commit comments

Comments
 (0)