Skip to content

Commit c6193d7

Browse files
Claudio SClaudio S
Claudio S
authored and
Claudio S
committed
feat: reimplemented initial/default isDirty/isPristine
1 parent 00bddf7 commit c6193d7

File tree

139 files changed

+296
-140
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+296
-140
lines changed

Diff for: .babelrc

100644100755
File mode changed.

Diff for: .eslintrc.json

100644100755
File mode changed.

Diff for: .gitignore

100644100755
File mode changed.

Diff for: .travis.yml

100644100755
File mode changed.

Diff for: CHANGELOG.md

100644100755
File mode changed.

Diff for: DOCUMENTATION.md

100644100755
File mode changed.

Diff for: LICENSE

100644100755
File mode changed.

Diff for: README.md

100644100755
File mode changed.

Diff for: package.json

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
}
6767
},
6868
"dependencies": {
69-
"lodash": "^4.16.2"
69+
"lodash": "^4.17.11"
7070
},
7171
"peerDependencies": {
7272
"mobx": "^2.5.0 || ^3.0.0 || ^4.0.0 || ^5.0.0"

Diff for: src/Base.js

100644100755
File mode changed.

Diff for: src/Bindings.js

100644100755
File mode changed.

Diff for: src/Field.js

100644100755
+58-51
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414

1515
const setupFieldProps = (instance, props, data) =>
1616
Object.assign(instance, {
17-
$value: instance.$initial,
1817
$label: props.$label || data.label || '',
1918
$placeholder: props.$placeholder || data.placeholder || '',
2019
$disabled: props.$disabled || data.disabled || false,
@@ -32,13 +31,14 @@ const setupFieldProps = (instance, props, data) =>
3231
});
3332

3433
const setupDefaultProp = (instance, data, props, update, {
35-
isEmptyArray, checkArray,
34+
isEmptyArray,
3635
}) => parseInput(instance.$input, {
36+
nullable: true,
3737
isEmptyArray,
3838
type: instance.type,
39-
unified: update ? '' : checkArray(data.default),
40-
separated: checkArray(props.$default),
41-
initial: checkArray(instance.$initial),
39+
unified: update ? '' : data.default,
40+
separated: props.$default,
41+
fallback: instance.$initial,
4242
});
4343

4444
export default class Field extends Base {
@@ -162,15 +162,19 @@ export default class Field extends Base {
162162
}
163163

164164
@computed get initial() {
165-
return this.getComputedProp('initial');
165+
return this.$initial
166+
? toJS(this.$initial)
167+
: this.getComputedProp('initial');
166168
}
167169

168-
set initial(val) {
169-
this.$initial = parseInput(this.$input, { separated: val });
170+
@computed get default() {
171+
return this.$default
172+
? toJS(this.$default)
173+
: this.getComputedProp('default');
170174
}
171175

172-
@computed get default() {
173-
return this.getComputedProp('default');
176+
set initial(val) {
177+
this.$initial = parseInput(this.$input, { separated: val });
174178
}
175179

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

247+
@computed get isDefault() {
248+
return !_.isNil(this.default) &&
249+
_.isEqual(this.default, this.value);
250+
}
251+
243252
@computed get isDirty() {
244-
return this.hasNestedFields
245-
? this.check('isDirty', true)
246-
: !_.isEqual(this.$default, this.value);
253+
return !_.isNil(this.initial) &&
254+
!_.isEqual(this.initial, this.value);
247255
}
248256

249257
@computed get isPristine() {
250-
return this.hasNestedFields
251-
? this.check('isPristine', true)
252-
: _.isEqual(this.$default, this.value);
258+
return !_.isNil(this.initial) &&
259+
_.isEqual(this.initial, this.value) ;
253260
}
254261

255-
@computed get isDefault() {
256-
return this.hasNestedFields
257-
? this.check('isDefault', true)
258-
: _.isEqual(this.$default, this.value);
262+
@computed get isEmpty() {
263+
if (this.hasNestedFields) return this.check('isEmpty', true);
264+
if (_.isBoolean(this.value)) return !!this.$value;
265+
if (_.isNumber(this.value)) return false;
266+
if (_.isDate(this.value)) return false;
267+
return _.isEmpty(this.value);
259268
}
260269

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

273-
@computed get isEmpty() {
274-
if (this.hasNestedFields) return this.check('isEmpty', true);
275-
if (_.isBoolean(this.value)) return !!this.$value;
276-
if (_.isNumber(this.value)) return false;
277-
if (_.isDate(this.value)) return false;
278-
return _.isEmpty(this.value);
279-
}
280-
281282
@computed get focused() {
282283
return this.hasNestedFields
283284
? this.check('focused', true)
@@ -385,41 +386,38 @@ export const prototypes = {
385386
this.path = $path;
386387
this.id = this.state.options.get('uniqueId').apply(this, [this]);
387388
const isEmptyArray = (_.has($data, 'fields') && _.isArray($data.fields));
388-
const checkArray = val => isEmptyArray ? [] : val;
389-
390-
const {
391-
$value,
392-
$type,
393-
$input,
394-
$output,
395-
} = $props;
389+
const { $type, $input, $output } = $props;
396390

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

400394
if (_.isPlainObject($data)) {
401-
const {
402-
value,
403-
type,
404-
input,
405-
output,
406-
} = $data;
395+
const { type, input, output } = $data;
407396

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

402+
this.$value = parseInput(this.$input, {
403+
isEmptyArray,
404+
type: this.type,
405+
unified: $data.value,
406+
separated: $props.$value,
407+
fallback: $props.$initial,
408+
});
409+
413410
this.$initial = parseInput(this.$input, {
411+
nullable: true,
414412
isEmptyArray,
415413
type: this.type,
416-
unified: checkArray(value),
417-
separated: checkArray($props.$initial),
418-
initial: checkArray($data.initial),
414+
unified: $data.initial,
415+
separated: $props.$initial,
416+
fallback: this.$value,
419417
});
420418

421419
this.$default = setupDefaultProp(this, $data, $props, update, {
422-
isEmptyArray, checkArray,
420+
isEmptyArray,
423421
});
424422

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

433+
this.$value = parseInput(this.$input, {
434+
isEmptyArray,
435+
type: this.type,
436+
unified: $data,
437+
separated: $props.$value,
438+
});
439+
435440
this.$initial = parseInput(this.$input, {
441+
nullable: true,
436442
isEmptyArray,
437443
type: this.type,
438-
unified: checkArray($data),
439-
separated: checkArray($value),
444+
unified: $data,
445+
separated: $props.$initial,
446+
fallback: this.$value,
440447
});
441448

442449
this.$default = setupDefaultProp(this, $data, $props, update, {
443-
isEmptyArray, checkArray,
450+
isEmptyArray,
444451
});
445452

446453
setupFieldProps(this, $props, $data);
@@ -531,7 +538,7 @@ export const prototypes = {
531538
this.$value = defaultClearValue({ value: this.$value });
532539
this.files = undefined;
533540

534-
if (deep) this.each(field => field.clear(true, false));
541+
if (deep) this.each(field => field.clear(true));
535542

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

553-
if (deep) this.each(field => field.reset(true, false));
560+
if (deep) this.each(field => field.reset(true));
554561

555562
this.validate({
556563
showErrors: this.state.options.get('showErrorsOnReset', this),

Diff for: src/Form.js

100644100755
+4-4
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ export default class Form extends Base {
107107
&& this.check('isValid', true);
108108
}
109109

110-
@computed get isDirty() {
111-
return this.check('isDirty', true);
112-
}
113-
114110
@computed get isPristine() {
115111
return this.check('isPristine', true);
116112
}
117113

114+
@computed get isDirty() {
115+
return this.check('isDirty', true);
116+
}
117+
118118
@computed get isDefault() {
119119
return this.check('isDefault', true);
120120
}

Diff for: src/Options.js

100644100755
File mode changed.

Diff for: src/State.js

100644100755
File mode changed.

Diff for: src/Validator.js

100644100755
File mode changed.

Diff for: src/index.js

100644100755
File mode changed.

Diff for: src/parser.js

100644100755
+12-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ const defaultClearValue = ({ value }) => {
1010
return undefined;
1111
};
1212

13-
const defaultValue = ({ type, isEmptyArray = false }) => {
13+
const defaultValue = ({
14+
type,
15+
nullable = false,
16+
isEmptyArray = false
17+
}) => {
1418
if (type === 'date') return null;
1519
if (type === 'checkbox') return false;
1620
if (type === 'number') return 0;
17-
return isEmptyArray ? [] : '';
21+
if (nullable) return null;
22+
if (isEmptyArray) return [];
23+
return '';
1824
};
1925

2026
const parsePath = (path) => {
@@ -25,9 +31,11 @@ const parsePath = (path) => {
2531
};
2632

2733
const parseInput = (input, {
28-
type, isEmptyArray, separated, unified, initial,
34+
type, isEmptyArray, nullable, separated, unified, fallback,
2935
}) =>
30-
input(utils.$try(separated, unified, initial, defaultValue({ type, isEmptyArray })));
36+
input(utils.$try(separated, unified, fallback, defaultValue({
37+
type, isEmptyArray, nullable
38+
})));
3139

3240
const parseArrayProp = ($val, $prop) => {
3341
const $values = _.values($val);

Diff for: src/props.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export default {
2+
booleans: ['hasError', 'isValid', 'isDirty', 'isPristine', 'isDefault', 'isEmpty', 'focused', 'touched', 'changed', 'disabled', 'resetting', 'clearing', 'blurred', 'deleted'],
3+
field: ['value', 'initial', 'default', 'label', 'placeholder', 'disabled', 'related', 'options', 'extra', 'bindings', 'type', 'hooks', 'handlers', 'deleted', 'error'],
4+
separated: ['values', 'initials', 'defaults', 'labels', 'placeholders', 'disabled', 'related', 'options', 'extra', 'bindings', 'types', 'hooks', 'handlers', 'deleted', 'error'],
5+
handlers: ['onChange', 'onToggle', 'onFocus', 'onBlur', 'onDrop', 'onSubmit', 'onReset', 'onClear', 'onAdd', 'onDel'],
6+
function: ['observers', 'interceptors', 'input', 'output'],
7+
validation: ['rules', 'validators', 'validateWith'],
8+
exceptions: ['isDirty', 'isPristine'],
9+
types: {
10+
isDirty: 'some',
11+
isPristine: 'every',
12+
isDefault: 'every',
13+
isValid: 'every',
14+
isEmpty: 'every',
15+
hasError: 'some',
16+
focused: 'some',
17+
blurred: 'some',
18+
touched: 'some',
19+
changed: 'some',
20+
deleted: 'every',
21+
disabled: 'every',
22+
clearing: 'every',
23+
resetting: 'every',
24+
},
25+
};

Diff for: src/shared/Actions.js

100644100755
+7-6
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ export default {
6666
},
6767

6868
deepCheck(type, prop, fields) {
69-
return _.transform(utils.getObservableMapValues(fields), (check, field) => {
70-
if (field.fields.size === 0) {
69+
const $fields = utils.getObservableMapValues(fields);
70+
return _.transform($fields, (check, field) => {
71+
if (!field.fields.size || utils.props.exceptions.includes(prop)) {
7172
check.push(field[prop]);
72-
return check;
7373
}
74+
7475
const $deep = this.deepCheck(type, prop, field.fields);
7576
check.push(utils.checkPropType({ type, data: $deep }));
7677
return check;
@@ -171,9 +172,9 @@ export default {
171172

172173
if (_.isString(prop)) {
173174
const removeValue = (prop === 'value') &&
174-
((this.state.options.get('softDelete', this) && field.deleted) ||
175-
(this.state.options.get('retrieveOnlyDirtyValues', this) && field.isPristine) ||
176-
(this.state.options.get('retrieveOnlyEnabledFields', this) && field.disabled));
175+
((this.state.options.get('retrieveOnlyDirtyValues', this) && field.isPristine) ||
176+
(this.state.options.get('retrieveOnlyEnabledFields', this) && field.disabled) ||
177+
(this.state.options.get('softDelete', this) && field.deleted));
177178

178179
if (field.fields.size === 0) {
179180
delete obj[field.key]; // eslint-disable-line

Diff for: src/shared/Events.js

100644100755
File mode changed.

Diff for: src/shared/Helpers.js

100644100755
File mode changed.

Diff for: src/shared/Initializer.js

100644100755
File mode changed.

Diff for: src/shared/Utils.js

100644100755
File mode changed.

Diff for: src/utils.js

100644100755
+1-24
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,7 @@
11
import _ from 'lodash';
22
import { values as mobxValues, keys as mobxKeys } from 'mobx';
33

4-
const props = {
5-
booleans: ['hasError', 'isValid', 'isDirty', 'isPristine', 'isDefault', 'isEmpty', 'focused', 'touched', 'changed', 'disabled', 'resetting', 'clearing', 'blurred', 'deleted'],
6-
field: ['value', 'initial', 'default', 'label', 'placeholder', 'disabled', 'related', 'options', 'extra', 'bindings', 'type', 'hooks', 'handlers', 'error', 'deleted'],
7-
separated: ['values', 'initials', 'defaults', 'labels', 'placeholders', 'disabled', 'related', 'options', 'extra', 'bindings', 'types', 'hooks', 'handlers'],
8-
handlers: ['onChange', 'onToggle', 'onFocus', 'onBlur', 'onDrop', 'onSubmit', 'onReset', 'onClear', 'onAdd', 'onDel'],
9-
function: ['observers', 'interceptors', 'input', 'output'],
10-
validation: ['rules', 'validators', 'validateWith'],
11-
types: {
12-
isDirty: 'some',
13-
isValid: 'every',
14-
isPristine: 'every',
15-
isDefault: 'every',
16-
isEmpty: 'every',
17-
hasError: 'some',
18-
focused: 'some',
19-
blurred: 'some',
20-
touched: 'some',
21-
changed: 'some',
22-
deleted: 'every',
23-
disabled: 'every',
24-
clearing: 'every',
25-
resetting: 'every',
26-
},
27-
};
4+
import props from './props';
285

296
const getObservableMapValues = observableMap =>
307
mobxValues

Diff for: src/validators/DVR.js

100644100755
File mode changed.

Diff for: src/validators/SVK.js

100644100755
File mode changed.

Diff for: src/validators/VJF.js

100644100755
File mode changed.

Diff for: src/validators/YUP.js

100644100755
File mode changed.

Diff for: tests/.eslintrc

100644100755
File mode changed.

Diff for: tests/computed/_.flat.js

100644100755
File mode changed.

Diff for: tests/computed/_.nested.js

100644100755
File mode changed.

Diff for: tests/computed/flat.hasError.js

100644100755
File mode changed.

Diff for: tests/computed/flat.isDirty.js

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default ($) => {
1212
it('$H isDirty should be false', () => expect($.$H.isDirty).to.be.false);
1313
it('$I isDirty should be false', () => expect($.$I.isDirty).to.be.false);
1414
it('$L isDirty should be true', () => expect($.$L.isDirty).to.be.true);
15-
it('$M isDirty should be false', () => expect($.$M.isDirty).to.be.false);
15+
it('$M isDirty should be true', () => expect($.$M.isDirty).to.be.true);
1616
it('$N isDirty should be false', () => expect($.$N.isDirty).to.be.false);
1717
});
1818
};

Diff for: tests/computed/flat.isEmpty.js

100644100755
File mode changed.

Diff for: tests/computed/flat.isPristine.js

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default ($) => {
1212
it('$H isPristine should be true', () => expect($.$H.isPristine).to.be.true);
1313
it('$I isPristine should be true', () => expect($.$I.isPristine).to.be.true);
1414
it('$L isPristine should be false', () => expect($.$L.isPristine).to.be.false);
15-
it('$M isPristine should be true', () => expect($.$M.isPristine).to.be.true);
15+
it('$M isPristine should be false', () => expect($.$M.isPristine).to.be.false);
1616
it('$N isPristine should be true', () => expect($.$N.isPristine).to.be.true);
1717
});
1818
};

Diff for: tests/computed/flat.isValid.js

100644100755
File mode changed.

Diff for: tests/computed/nested.isValid.js

100644100755
File mode changed.

Diff for: tests/data/_.fixes.js

100644100755
+2-1
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ import $Q1 from './forms/fixes/form.q1';
2020
import $Q2 from './forms/fixes/form.q2';
2121
import $R from './forms/fixes/form.r';
2222
import $S from './forms/fixes/form.s';
23+
import $T from './forms/fixes/form.t';
2324

2425
import $425 from './forms/fixes/form.425';
2526
import $472 from './forms/fixes/form.472';
2627
import $481 from './forms/fixes/form.481';
2728

2829
export default {
2930

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

3233
};
3334

Diff for: tests/data/_.flat.js

100644100755
File mode changed.

Diff for: tests/data/_.nested.js

100644100755
File mode changed.

Diff for: tests/data/extension/_.async.js

100644100755
File mode changed.

Diff for: tests/data/extension/_.bindings.js

100644100755
File mode changed.

Diff for: tests/data/extension/dvr.js

100644100755
File mode changed.

Diff for: tests/data/extension/svk.js

100644100755
File mode changed.

Diff for: tests/data/extension/vjf.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.425.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.472.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.481.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.a.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.b.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.c.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.d.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.e.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.f.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.g.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.h.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.i.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.l.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.m.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.n.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.o.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.p.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.q.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.q1.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.q2.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.r.js

100644100755
File mode changed.

Diff for: tests/data/forms/fixes/form.s.js

100644100755
File mode changed.

0 commit comments

Comments
 (0)