Skip to content

Commit

Permalink
REF: Add a helper to improve how fields are set
Browse files Browse the repository at this point in the history
  • Loading branch information
royriojas committed Jun 24, 2016
1 parent 443d90d commit c208bc7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/Field.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import clsc from 'coalescy';

export default class Field {
@observable _value;
_skipValidation = false;
@observable _interacted;
@observable _valid = true;
@observable errorMessage;
Expand All @@ -15,6 +16,10 @@ export default class Field {
return this._valid;
}

@computed get interacted() {
return this._interacted;
}

get value() {
if (isObservableArray(this._value)) {
return [].slice.call(this._value);
Expand All @@ -33,6 +38,10 @@ export default class Field {

this._value = val;

if (this._skipValidation) {
return;
}

if (this.interactive) {
this._debouncedValidation();
} else {
Expand All @@ -45,6 +54,10 @@ export default class Field {
this.errorMessage = '';
}

clearInteractive() {
this._interacted = false;
}

validate(force = false) {
if (!this._validateFn) {
return;
Expand Down
52 changes: 48 additions & 4 deletions src/FormModel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { observable, computed, extendObservable } from 'mobx';
import { observable, computed, extendObservable, transaction } from 'mobx';
import Field from './Field';

class FormModel {
Expand All @@ -9,17 +9,43 @@ class FormModel {
return false; // consider the form invalid until the validation process finish
}
const keys = Object.keys(this.fields);
return keys.reduce((seq, key) => {
return keys.every((key) => {
const field = this.fields[key];
seq = seq && field.valid; // eslint-disable-line no-param-reassign
return seq;
return !!field.valid;
}, true);
}

@computed get interacted() {
const keys = this.fieldKeys();
return keys.some((key) => {
const field = this.fields[key];
return !!field.interacted;
});
}

clearValues(obj) {
transaction(() => {
Object.keys(obj).forEach((key) => this.updateField(key, obj[key], true));
});

}

fieldKeys() {
return Object.keys(this.fields);
}

getField(field) {
const theField = this.fields[field];
if (!theField) {
throw new Error(`Field ${field} not found`);
}
return theField;
}

valueOf(field) {
return this.getField(field).value;
}

@computed get summary() {
return this.fieldKeys().reduce((seq, key) => {
const field = this.fields[key];
Expand All @@ -43,6 +69,24 @@ class FormModel {
return p;
}

updateField(field, value, reset) {
transaction(() => {
const theField = this.getField(field);

if (reset) {
theField._skipValidation = true;
}

theField.value = value;

if (reset) {
theField.clearValidation();
theField.clearInteractive();
theField._skipValidation = false;
}
});
}

toJSON() {
const keys = Object.keys(this.fields);
return keys.reduce((seq, key) => {
Expand Down

0 comments on commit c208bc7

Please sign in to comment.