Skip to content

Commit

Permalink
feat: reimplemented initial/default isDirty/isPristine
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio S authored and Claudio S committed Feb 19, 2019
1 parent 00bddf7 commit c6193d7
Show file tree
Hide file tree
Showing 139 changed files with 296 additions and 140 deletions.
Empty file modified .babelrc
100644 → 100755
Empty file.
Empty file modified .eslintrc.json
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .travis.yml
100644 → 100755
Empty file.
Empty file modified CHANGELOG.md
100644 → 100755
Empty file.
Empty file modified DOCUMENTATION.md
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion package.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
}
},
"dependencies": {
"lodash": "^4.16.2"
"lodash": "^4.17.11"
},
"peerDependencies": {
"mobx": "^2.5.0 || ^3.0.0 || ^4.0.0 || ^5.0.0"
Expand Down
Empty file modified src/Base.js
100644 → 100755
Empty file.
Empty file modified src/Bindings.js
100644 → 100755
Empty file.
109 changes: 58 additions & 51 deletions src/Field.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {

const setupFieldProps = (instance, props, data) =>
Object.assign(instance, {
$value: instance.$initial,
$label: props.$label || data.label || '',
$placeholder: props.$placeholder || data.placeholder || '',
$disabled: props.$disabled || data.disabled || false,
Expand All @@ -32,13 +31,14 @@ const setupFieldProps = (instance, props, data) =>
});

const setupDefaultProp = (instance, data, props, update, {
isEmptyArray, checkArray,
isEmptyArray,
}) => parseInput(instance.$input, {
nullable: true,
isEmptyArray,
type: instance.type,
unified: update ? '' : checkArray(data.default),
separated: checkArray(props.$default),
initial: checkArray(instance.$initial),
unified: update ? '' : data.default,
separated: props.$default,
fallback: instance.$initial,
});

export default class Field extends Base {
Expand Down Expand Up @@ -162,15 +162,19 @@ export default class Field extends Base {
}

@computed get initial() {
return this.getComputedProp('initial');
return this.$initial
? toJS(this.$initial)
: this.getComputedProp('initial');
}

set initial(val) {
this.$initial = parseInput(this.$input, { separated: val });
@computed get default() {
return this.$default
? toJS(this.$default)
: this.getComputedProp('default');
}

@computed get default() {
return this.getComputedProp('default');
set initial(val) {
this.$initial = parseInput(this.$input, { separated: val });
}

set default(val) {
Expand Down Expand Up @@ -240,22 +244,27 @@ export default class Field extends Base {
&& this.check('isValid', true);
}

@computed get isDefault() {
return !_.isNil(this.default) &&
_.isEqual(this.default, this.value);
}

@computed get isDirty() {
return this.hasNestedFields
? this.check('isDirty', true)
: !_.isEqual(this.$default, this.value);
return !_.isNil(this.initial) &&
!_.isEqual(this.initial, this.value);
}

@computed get isPristine() {
return this.hasNestedFields
? this.check('isPristine', true)
: _.isEqual(this.$default, this.value);
return !_.isNil(this.initial) &&
_.isEqual(this.initial, this.value) ;
}

@computed get isDefault() {
return this.hasNestedFields
? this.check('isDefault', true)
: _.isEqual(this.$default, this.value);
@computed get isEmpty() {
if (this.hasNestedFields) return this.check('isEmpty', true);
if (_.isBoolean(this.value)) return !!this.$value;
if (_.isNumber(this.value)) return false;
if (_.isDate(this.value)) return false;
return _.isEmpty(this.value);
}

@computed get resetting() {
Expand All @@ -270,14 +279,6 @@ export default class Field extends Base {
: this.$clearing;
}

@computed get isEmpty() {
if (this.hasNestedFields) return this.check('isEmpty', true);
if (_.isBoolean(this.value)) return !!this.$value;
if (_.isNumber(this.value)) return false;
if (_.isDate(this.value)) return false;
return _.isEmpty(this.value);
}

@computed get focused() {
return this.hasNestedFields
? this.check('focused', true)
Expand Down Expand Up @@ -385,41 +386,38 @@ export const prototypes = {
this.path = $path;
this.id = this.state.options.get('uniqueId').apply(this, [this]);
const isEmptyArray = (_.has($data, 'fields') && _.isArray($data.fields));
const checkArray = val => isEmptyArray ? [] : val;

const {
$value,
$type,
$input,
$output,
} = $props;
const { $type, $input, $output } = $props;

// eslint-disable-next-line
if (_.isNil($data)) $data = '';

if (_.isPlainObject($data)) {
const {
value,
type,
input,
output,
} = $data;
const { type, input, output } = $data;

this.name = _.toString($data.name || $key);
this.$type = $type || type || 'text';
this.$input = $try($input, input, this.$input);
this.$output = $try($output, output, this.$output);

this.$value = parseInput(this.$input, {
isEmptyArray,
type: this.type,
unified: $data.value,
separated: $props.$value,
fallback: $props.$initial,
});

this.$initial = parseInput(this.$input, {
nullable: true,
isEmptyArray,
type: this.type,
unified: checkArray(value),
separated: checkArray($props.$initial),
initial: checkArray($data.initial),
unified: $data.initial,
separated: $props.$initial,
fallback: this.$value,
});

this.$default = setupDefaultProp(this, $data, $props, update, {
isEmptyArray, checkArray,
isEmptyArray,
});

setupFieldProps(this, $props, $data);
Expand All @@ -432,15 +430,24 @@ export const prototypes = {
this.$input = $try($input, this.$input);
this.$output = $try($output, this.$output);

this.$value = parseInput(this.$input, {
isEmptyArray,
type: this.type,
unified: $data,
separated: $props.$value,
});

this.$initial = parseInput(this.$input, {
nullable: true,
isEmptyArray,
type: this.type,
unified: checkArray($data),
separated: checkArray($value),
unified: $data,
separated: $props.$initial,
fallback: this.$value,
});

this.$default = setupDefaultProp(this, $data, $props, update, {
isEmptyArray, checkArray,
isEmptyArray,
});

setupFieldProps(this, $props, $data);
Expand Down Expand Up @@ -531,7 +538,7 @@ export const prototypes = {
this.$value = defaultClearValue({ value: this.$value });
this.files = undefined;

if (deep) this.each(field => field.clear(true, false));
if (deep) this.each(field => field.clear(true));

this.validate({
showErrors: this.state.options.get('showErrorsOnClear', this),
Expand All @@ -550,7 +557,7 @@ export const prototypes = {
if (!useDefaultValue) this.value = this.$initial;
this.files = undefined;

if (deep) this.each(field => field.reset(true, false));
if (deep) this.each(field => field.reset(true));

this.validate({
showErrors: this.state.options.get('showErrorsOnReset', this),
Expand Down
8 changes: 4 additions & 4 deletions src/Form.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ export default class Form extends Base {
&& this.check('isValid', true);
}

@computed get isDirty() {
return this.check('isDirty', true);
}

@computed get isPristine() {
return this.check('isPristine', true);
}

@computed get isDirty() {
return this.check('isDirty', true);
}

@computed get isDefault() {
return this.check('isDefault', true);
}
Expand Down
Empty file modified src/Options.js
100644 → 100755
Empty file.
Empty file modified src/State.js
100644 → 100755
Empty file.
Empty file modified src/Validator.js
100644 → 100755
Empty file.
Empty file modified src/index.js
100644 → 100755
Empty file.
16 changes: 12 additions & 4 deletions src/parser.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ const defaultClearValue = ({ value }) => {
return undefined;
};

const defaultValue = ({ type, isEmptyArray = false }) => {
const defaultValue = ({
type,
nullable = false,
isEmptyArray = false
}) => {
if (type === 'date') return null;
if (type === 'checkbox') return false;
if (type === 'number') return 0;
return isEmptyArray ? [] : '';
if (nullable) return null;
if (isEmptyArray) return [];
return '';
};

const parsePath = (path) => {
Expand All @@ -25,9 +31,11 @@ const parsePath = (path) => {
};

const parseInput = (input, {
type, isEmptyArray, separated, unified, initial,
type, isEmptyArray, nullable, separated, unified, fallback,
}) =>
input(utils.$try(separated, unified, initial, defaultValue({ type, isEmptyArray })));
input(utils.$try(separated, unified, fallback, defaultValue({
type, isEmptyArray, nullable
})));

const parseArrayProp = ($val, $prop) => {
const $values = _.values($val);
Expand Down
25 changes: 25 additions & 0 deletions src/props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default {
booleans: ['hasError', 'isValid', 'isDirty', 'isPristine', 'isDefault', 'isEmpty', 'focused', 'touched', 'changed', 'disabled', 'resetting', 'clearing', 'blurred', 'deleted'],
field: ['value', 'initial', 'default', 'label', 'placeholder', 'disabled', 'related', 'options', 'extra', 'bindings', 'type', 'hooks', 'handlers', 'deleted', 'error'],
separated: ['values', 'initials', 'defaults', 'labels', 'placeholders', 'disabled', 'related', 'options', 'extra', 'bindings', 'types', 'hooks', 'handlers', 'deleted', 'error'],
handlers: ['onChange', 'onToggle', 'onFocus', 'onBlur', 'onDrop', 'onSubmit', 'onReset', 'onClear', 'onAdd', 'onDel'],
function: ['observers', 'interceptors', 'input', 'output'],
validation: ['rules', 'validators', 'validateWith'],
exceptions: ['isDirty', 'isPristine'],
types: {
isDirty: 'some',
isPristine: 'every',
isDefault: 'every',
isValid: 'every',
isEmpty: 'every',
hasError: 'some',
focused: 'some',
blurred: 'some',
touched: 'some',
changed: 'some',
deleted: 'every',
disabled: 'every',
clearing: 'every',
resetting: 'every',
},
};
13 changes: 7 additions & 6 deletions src/shared/Actions.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ export default {
},

deepCheck(type, prop, fields) {
return _.transform(utils.getObservableMapValues(fields), (check, field) => {
if (field.fields.size === 0) {
const $fields = utils.getObservableMapValues(fields);
return _.transform($fields, (check, field) => {
if (!field.fields.size || utils.props.exceptions.includes(prop)) {
check.push(field[prop]);
return check;
}

const $deep = this.deepCheck(type, prop, field.fields);
check.push(utils.checkPropType({ type, data: $deep }));
return check;
Expand Down Expand Up @@ -171,9 +172,9 @@ export default {

if (_.isString(prop)) {
const removeValue = (prop === 'value') &&
((this.state.options.get('softDelete', this) && field.deleted) ||
(this.state.options.get('retrieveOnlyDirtyValues', this) && field.isPristine) ||
(this.state.options.get('retrieveOnlyEnabledFields', this) && field.disabled));
((this.state.options.get('retrieveOnlyDirtyValues', this) && field.isPristine) ||
(this.state.options.get('retrieveOnlyEnabledFields', this) && field.disabled) ||
(this.state.options.get('softDelete', this) && field.deleted));

if (field.fields.size === 0) {
delete obj[field.key]; // eslint-disable-line
Expand Down
Empty file modified src/shared/Events.js
100644 → 100755
Empty file.
Empty file modified src/shared/Helpers.js
100644 → 100755
Empty file.
Empty file modified src/shared/Initializer.js
100644 → 100755
Empty file.
Empty file modified src/shared/Utils.js
100644 → 100755
Empty file.
25 changes: 1 addition & 24 deletions src/utils.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
import _ from 'lodash';
import { values as mobxValues, keys as mobxKeys } from 'mobx';

const props = {
booleans: ['hasError', 'isValid', 'isDirty', 'isPristine', 'isDefault', 'isEmpty', 'focused', 'touched', 'changed', 'disabled', 'resetting', 'clearing', 'blurred', 'deleted'],
field: ['value', 'initial', 'default', 'label', 'placeholder', 'disabled', 'related', 'options', 'extra', 'bindings', 'type', 'hooks', 'handlers', 'error', 'deleted'],
separated: ['values', 'initials', 'defaults', 'labels', 'placeholders', 'disabled', 'related', 'options', 'extra', 'bindings', 'types', 'hooks', 'handlers'],
handlers: ['onChange', 'onToggle', 'onFocus', 'onBlur', 'onDrop', 'onSubmit', 'onReset', 'onClear', 'onAdd', 'onDel'],
function: ['observers', 'interceptors', 'input', 'output'],
validation: ['rules', 'validators', 'validateWith'],
types: {
isDirty: 'some',
isValid: 'every',
isPristine: 'every',
isDefault: 'every',
isEmpty: 'every',
hasError: 'some',
focused: 'some',
blurred: 'some',
touched: 'some',
changed: 'some',
deleted: 'every',
disabled: 'every',
clearing: 'every',
resetting: 'every',
},
};
import props from './props';

const getObservableMapValues = observableMap =>
mobxValues
Expand Down
Empty file modified src/validators/DVR.js
100644 → 100755
Empty file.
Empty file modified src/validators/SVK.js
100644 → 100755
Empty file.
Empty file modified src/validators/VJF.js
100644 → 100755
Empty file.
Empty file modified src/validators/YUP.js
100644 → 100755
Empty file.
Empty file modified tests/.eslintrc
100644 → 100755
Empty file.
Empty file modified tests/computed/_.flat.js
100644 → 100755
Empty file.
Empty file modified tests/computed/_.nested.js
100644 → 100755
Empty file.
Empty file modified tests/computed/flat.hasError.js
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion tests/computed/flat.isDirty.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default ($) => {
it('$H isDirty should be false', () => expect($.$H.isDirty).to.be.false);
it('$I isDirty should be false', () => expect($.$I.isDirty).to.be.false);
it('$L isDirty should be true', () => expect($.$L.isDirty).to.be.true);
it('$M isDirty should be false', () => expect($.$M.isDirty).to.be.false);
it('$M isDirty should be true', () => expect($.$M.isDirty).to.be.true);
it('$N isDirty should be false', () => expect($.$N.isDirty).to.be.false);
});
};
Empty file modified tests/computed/flat.isEmpty.js
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion tests/computed/flat.isPristine.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default ($) => {
it('$H isPristine should be true', () => expect($.$H.isPristine).to.be.true);
it('$I isPristine should be true', () => expect($.$I.isPristine).to.be.true);
it('$L isPristine should be false', () => expect($.$L.isPristine).to.be.false);
it('$M isPristine should be true', () => expect($.$M.isPristine).to.be.true);
it('$M isPristine should be false', () => expect($.$M.isPristine).to.be.false);
it('$N isPristine should be true', () => expect($.$N.isPristine).to.be.true);
});
};
Empty file modified tests/computed/flat.isValid.js
100644 → 100755
Empty file.
Empty file modified tests/computed/nested.isValid.js
100644 → 100755
Empty file.
3 changes: 2 additions & 1 deletion tests/data/_.fixes.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import $Q1 from './forms/fixes/form.q1';
import $Q2 from './forms/fixes/form.q2';
import $R from './forms/fixes/form.r';
import $S from './forms/fixes/form.s';
import $T from './forms/fixes/form.t';

import $425 from './forms/fixes/form.425';
import $472 from './forms/fixes/form.472';
import $481 from './forms/fixes/form.481';

export default {

$A, $B, $C, $D, $E, $F, $G, $H, $I, $L, $M, $N, $O, $P, $Q, $Q1, $Q2, $R, $S, $425, $472, $481,
$A, $B, $C, $D, $E, $F, $G, $H, $I, $L, $M, $N, $O, $P, $Q, $Q1, $Q2, $R, $S, $T, $425, $472, $481,

};

Empty file modified tests/data/_.flat.js
100644 → 100755
Empty file.
Empty file modified tests/data/_.nested.js
100644 → 100755
Empty file.
Empty file modified tests/data/extension/_.async.js
100644 → 100755
Empty file.
Empty file modified tests/data/extension/_.bindings.js
100644 → 100755
Empty file.
Empty file modified tests/data/extension/dvr.js
100644 → 100755
Empty file.
Empty file modified tests/data/extension/svk.js
100644 → 100755
Empty file.
Empty file modified tests/data/extension/vjf.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.425.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.472.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.481.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.a.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.b.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.c.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.d.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.e.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.f.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.g.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.h.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.i.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.l.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.m.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.n.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.o.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.p.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.q.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.q1.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.q2.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.r.js
100644 → 100755
Empty file.
Empty file modified tests/data/forms/fixes/form.s.js
100644 → 100755
Empty file.
Loading

0 comments on commit c6193d7

Please sign in to comment.