mobx-form 13.3.0 → 13.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -268,13 +268,19 @@ class Field {
268
268
 
269
269
 
270
270
  restoreInitialValue({
271
- resetInteractedFlag = true
271
+ resetInteractedFlag = true,
272
+ commit = true
272
273
  } = {}) {
273
274
  this.setValue(this._initialValue, {
274
- resetInteractedFlag
275
+ resetInteractedFlag,
276
+ commit
275
277
  });
276
278
  }
277
279
 
280
+ get dirty() {
281
+ return this._initialValue !== this.value;
282
+ }
283
+
278
284
  commit() {
279
285
  this._initialValue = this.value;
280
286
  }
@@ -536,8 +542,10 @@ class Field {
536
542
  hasValue: mobx.computed,
537
543
  _autoValidate: mobx.observable,
538
544
  _value: mobx.observable,
545
+ _initialValue: mobx.observable,
539
546
  _interacted: mobx.observable,
540
547
  _blurredOnce: mobx.observable,
548
+ dirty: mobx.computed,
541
549
  blurred: mobx.computed,
542
550
  errorMessage: mobx.computed,
543
551
  rawError: mobx.observable.ref,
@@ -675,6 +683,18 @@ class FormModel {
675
683
  restoreInitialValues(opts) {
676
684
  this._eachField(field => field.restoreInitialValue(opts));
677
685
  }
686
+
687
+ commit() {
688
+ this._eachField(field => field.commit());
689
+ }
690
+
691
+ get dirty() {
692
+ return this._fieldKeys().some(key => {
693
+ const f = this._getField(key);
694
+
695
+ return f.dirty;
696
+ });
697
+ }
678
698
  /**
679
699
  * Set multiple values to more than one field a time using an object
680
700
  * where each key is the name of a field. The value will be set to each
@@ -852,11 +872,14 @@ class FormModel {
852
872
  resetInteractedFlag: mobx.action,
853
873
  disableFields: mobx.action,
854
874
  addFields: mobx.action,
855
- enableFields: mobx.action
875
+ enableFields: mobx.action,
876
+ commit: mobx.action,
877
+ dirty: mobx.computed
856
878
  });
857
879
  this.addFields(descriptors);
858
880
  initialState && this.updateFrom(initialState, {
859
- throwIfMissingField: options.throwIfMissingField
881
+ throwIfMissingField: options.throwIfMissingField,
882
+ commit: true
860
883
  });
861
884
  }
862
885
 
@@ -262,13 +262,19 @@ class Field {
262
262
 
263
263
 
264
264
  restoreInitialValue({
265
- resetInteractedFlag = true
265
+ resetInteractedFlag = true,
266
+ commit = true
266
267
  } = {}) {
267
268
  this.setValue(this._initialValue, {
268
- resetInteractedFlag
269
+ resetInteractedFlag,
270
+ commit
269
271
  });
270
272
  }
271
273
 
274
+ get dirty() {
275
+ return this._initialValue !== this.value;
276
+ }
277
+
272
278
  commit() {
273
279
  this._initialValue = this.value;
274
280
  }
@@ -530,8 +536,10 @@ class Field {
530
536
  hasValue: computed,
531
537
  _autoValidate: observable,
532
538
  _value: observable,
539
+ _initialValue: observable,
533
540
  _interacted: observable,
534
541
  _blurredOnce: observable,
542
+ dirty: computed,
535
543
  blurred: computed,
536
544
  errorMessage: computed,
537
545
  rawError: observable.ref,
@@ -669,6 +677,18 @@ class FormModel {
669
677
  restoreInitialValues(opts) {
670
678
  this._eachField(field => field.restoreInitialValue(opts));
671
679
  }
680
+
681
+ commit() {
682
+ this._eachField(field => field.commit());
683
+ }
684
+
685
+ get dirty() {
686
+ return this._fieldKeys().some(key => {
687
+ const f = this._getField(key);
688
+
689
+ return f.dirty;
690
+ });
691
+ }
672
692
  /**
673
693
  * Set multiple values to more than one field a time using an object
674
694
  * where each key is the name of a field. The value will be set to each
@@ -846,11 +866,14 @@ class FormModel {
846
866
  resetInteractedFlag: action,
847
867
  disableFields: action,
848
868
  addFields: action,
849
- enableFields: action
869
+ enableFields: action,
870
+ commit: action,
871
+ dirty: computed
850
872
  });
851
873
  this.addFields(descriptors);
852
874
  initialState && this.updateFrom(initialState, {
853
- throwIfMissingField: options.throwIfMissingField
875
+ throwIfMissingField: options.throwIfMissingField,
876
+ commit: true
854
877
  });
855
878
  }
856
879
 
@@ -277,10 +277,13 @@ var Field = /*#__PURE__*/function () {
277
277
  value: function restoreInitialValue() {
278
278
  var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
279
279
  _ref2$resetInteracted = _ref2.resetInteractedFlag,
280
- resetInteractedFlag = _ref2$resetInteracted === void 0 ? true : _ref2$resetInteracted;
280
+ resetInteractedFlag = _ref2$resetInteracted === void 0 ? true : _ref2$resetInteracted,
281
+ _ref2$commit = _ref2.commit,
282
+ commit = _ref2$commit === void 0 ? true : _ref2$commit;
281
283
 
282
284
  this.setValue(this._initialValue, {
283
- resetInteractedFlag: resetInteractedFlag
285
+ resetInteractedFlag: resetInteractedFlag,
286
+ commit: commit
284
287
  });
285
288
  }
286
289
  }, {
@@ -668,6 +671,11 @@ var Field = /*#__PURE__*/function () {
668
671
  set: function set(val) {
669
672
  this._setValue(val);
670
673
  }
674
+ }, {
675
+ key: "dirty",
676
+ get: function get() {
677
+ return this._initialValue !== this.value;
678
+ }
671
679
  }, {
672
680
  key: "originalErrorMessage",
673
681
  get: function get() {
@@ -741,8 +749,10 @@ var Field = /*#__PURE__*/function () {
741
749
  hasValue: mobx.computed,
742
750
  _autoValidate: mobx.observable,
743
751
  _value: mobx.observable,
752
+ _initialValue: mobx.observable,
744
753
  _interacted: mobx.observable,
745
754
  _blurredOnce: mobx.observable,
755
+ dirty: mobx.computed,
746
756
  blurred: mobx.computed,
747
757
  errorMessage: mobx.computed,
748
758
  rawError: mobx.observable.ref,
@@ -830,15 +840,22 @@ var FormModel = /*#__PURE__*/function () {
830
840
  return field.restoreInitialValue(opts);
831
841
  });
832
842
  }
843
+ }, {
844
+ key: "commit",
845
+ value: function commit() {
846
+ this._eachField(function (field) {
847
+ return field.commit();
848
+ });
849
+ }
850
+ }, {
851
+ key: "updateFrom",
852
+
833
853
  /**
834
854
  * Set multiple values to more than one field a time using an object
835
855
  * where each key is the name of a field. The value will be set to each
836
856
  * field and from that point on the values set are considered the new
837
857
  * initial values. Validation and interacted flags are also reset if the second argument is true
838
858
  * */
839
-
840
- }, {
841
- key: "updateFrom",
842
859
  value: function updateFrom(obj) {
843
860
  var _this = this;
844
861
 
@@ -981,12 +998,23 @@ var FormModel = /*#__PURE__*/function () {
981
998
  });
982
999
  }
983
1000
  }, {
984
- key: "summary",
1001
+ key: "dirty",
985
1002
  get: function get() {
986
1003
  var _this8 = this;
987
1004
 
1005
+ return this._fieldKeys().some(function (key) {
1006
+ var f = _this8._getField(key);
1007
+
1008
+ return f.dirty;
1009
+ });
1010
+ }
1011
+ }, {
1012
+ key: "summary",
1013
+ get: function get() {
1014
+ var _this9 = this;
1015
+
988
1016
  return this._fieldKeys().reduce(function (seq, key) {
989
- var field = _this8.fields[key];
1017
+ var field = _this9.fields[key];
990
1018
 
991
1019
  if (field.errorMessage) {
992
1020
  seq.push(field.errorMessage);
@@ -998,10 +1026,10 @@ var FormModel = /*#__PURE__*/function () {
998
1026
  }, {
999
1027
  key: "validating",
1000
1028
  get: function get() {
1001
- var _this9 = this;
1029
+ var _this10 = this;
1002
1030
 
1003
1031
  return this._validating || this._fieldKeys().some(function (key) {
1004
- var f = _this9._getField(key);
1032
+ var f = _this10._getField(key);
1005
1033
 
1006
1034
  return f.validating;
1007
1035
  });
@@ -1009,11 +1037,11 @@ var FormModel = /*#__PURE__*/function () {
1009
1037
  }, {
1010
1038
  key: "serializedData",
1011
1039
  get: function get() {
1012
- var _this10 = this;
1040
+ var _this11 = this;
1013
1041
 
1014
1042
  var keys = Object.keys(this.fields);
1015
1043
  return mobx.toJS(keys.reduce(function (seq, key) {
1016
- var field = _this10.fields[key];
1044
+ var field = _this11.fields[key];
1017
1045
  var value = mobx.toJS(field.value); // this is required to make sure forms that use the serializedData object
1018
1046
  // have the values without leading or trailing spaces
1019
1047
 
@@ -1034,7 +1062,7 @@ var FormModel = /*#__PURE__*/function () {
1034
1062
  }]);
1035
1063
 
1036
1064
  function FormModel() {
1037
- var _this11 = this;
1065
+ var _this12 = this;
1038
1066
 
1039
1067
  var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
1040
1068
  _ref2$descriptors = _ref2.descriptors,
@@ -1049,7 +1077,7 @@ var FormModel = /*#__PURE__*/function () {
1049
1077
  this._validating = false;
1050
1078
 
1051
1079
  this.setValidating = function (validating) {
1052
- _this11._validating = validating;
1080
+ _this12._validating = validating;
1053
1081
  };
1054
1082
 
1055
1083
  this.addFields = function (fieldsDescriptor) {
@@ -1063,7 +1091,7 @@ var FormModel = /*#__PURE__*/function () {
1063
1091
  name = field.name,
1064
1092
  descriptor = _objectWithoutProperties(field, ["value", "name"]);
1065
1093
 
1066
- _this11._createField({
1094
+ _this12._createField({
1067
1095
  value: value,
1068
1096
  name: name,
1069
1097
  descriptor: descriptor
@@ -1078,7 +1106,7 @@ var FormModel = /*#__PURE__*/function () {
1078
1106
  value = _fieldsDescriptor$key.value,
1079
1107
  descriptor = _objectWithoutProperties(_fieldsDescriptor$key, ["value"]);
1080
1108
 
1081
- _this11._createField({
1109
+ _this12._createField({
1082
1110
  value: value,
1083
1111
  name: key,
1084
1112
  descriptor: descriptor
@@ -1107,11 +1135,14 @@ var FormModel = /*#__PURE__*/function () {
1107
1135
  resetInteractedFlag: mobx.action,
1108
1136
  disableFields: mobx.action,
1109
1137
  addFields: mobx.action,
1110
- enableFields: mobx.action
1138
+ enableFields: mobx.action,
1139
+ commit: mobx.action,
1140
+ dirty: mobx.computed
1111
1141
  });
1112
1142
  this.addFields(descriptors);
1113
1143
  initialState && this.updateFrom(initialState, {
1114
- throwIfMissingField: options.throwIfMissingField
1144
+ throwIfMissingField: options.throwIfMissingField,
1145
+ commit: true
1115
1146
  });
1116
1147
  }
1117
1148
 
@@ -1133,10 +1164,10 @@ var FormModel = /*#__PURE__*/function () {
1133
1164
  }, {
1134
1165
  key: "_eachField",
1135
1166
  value: function _eachField(cb) {
1136
- var _this12 = this;
1167
+ var _this13 = this;
1137
1168
 
1138
1169
  Object.keys(this.fields).forEach(function (key) {
1139
- return cb(_this12.fields[key]);
1170
+ return cb(_this13.fields[key]);
1140
1171
  });
1141
1172
  }
1142
1173
  }, {
@@ -1154,11 +1185,11 @@ var FormModel = /*#__PURE__*/function () {
1154
1185
  }, {
1155
1186
  key: "disableFields",
1156
1187
  value: function disableFields(fieldKeys) {
1157
- var _this13 = this;
1188
+ var _this14 = this;
1158
1189
 
1159
1190
  if (!Array.isArray(fieldKeys)) throw new TypeError('fieldKeys should be an array with the names of the fields to disable');
1160
1191
  fieldKeys.forEach(function (key) {
1161
- var field = _this13._getField(key);
1192
+ var field = _this14._getField(key);
1162
1193
 
1163
1194
  field.setDisabled(true);
1164
1195
  });
@@ -1174,11 +1205,11 @@ var FormModel = /*#__PURE__*/function () {
1174
1205
  }, {
1175
1206
  key: "enableFields",
1176
1207
  value: function enableFields(fieldKeys) {
1177
- var _this14 = this;
1208
+ var _this15 = this;
1178
1209
 
1179
1210
  if (!Array.isArray(fieldKeys)) throw new TypeError('fieldKeys should be an array with the names of the fields to disable');
1180
1211
  fieldKeys.forEach(function (key) {
1181
- var field = _this14._getField(key);
1212
+ var field = _this15._getField(key);
1182
1213
 
1183
1214
  field.setDisabled(false);
1184
1215
  });
@@ -1186,10 +1217,10 @@ var FormModel = /*#__PURE__*/function () {
1186
1217
  }, {
1187
1218
  key: "resetValidatedOnce",
1188
1219
  value: function resetValidatedOnce() {
1189
- var _this15 = this;
1220
+ var _this16 = this;
1190
1221
 
1191
1222
  this._fieldKeys().forEach(function (key) {
1192
- _this15.fields[key].resetValidatedOnce();
1223
+ _this16.fields[key].resetValidatedOnce();
1193
1224
  });
1194
1225
  }
1195
1226
  }]);
@@ -337,10 +337,13 @@
337
337
  value: function restoreInitialValue() {
338
338
  var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
339
339
  _ref2$resetInteracted = _ref2.resetInteractedFlag,
340
- resetInteractedFlag = _ref2$resetInteracted === void 0 ? true : _ref2$resetInteracted;
340
+ resetInteractedFlag = _ref2$resetInteracted === void 0 ? true : _ref2$resetInteracted,
341
+ _ref2$commit = _ref2.commit,
342
+ commit = _ref2$commit === void 0 ? true : _ref2$commit;
341
343
 
342
344
  this.setValue(this._initialValue, {
343
- resetInteractedFlag: resetInteractedFlag
345
+ resetInteractedFlag: resetInteractedFlag,
346
+ commit: commit
344
347
  });
345
348
  }
346
349
  }, {
@@ -728,6 +731,11 @@
728
731
  set: function set(val) {
729
732
  this._setValue(val);
730
733
  }
734
+ }, {
735
+ key: "dirty",
736
+ get: function get() {
737
+ return this._initialValue !== this.value;
738
+ }
731
739
  }, {
732
740
  key: "originalErrorMessage",
733
741
  get: function get() {
@@ -801,8 +809,10 @@
801
809
  hasValue: mobx.computed,
802
810
  _autoValidate: mobx.observable,
803
811
  _value: mobx.observable,
812
+ _initialValue: mobx.observable,
804
813
  _interacted: mobx.observable,
805
814
  _blurredOnce: mobx.observable,
815
+ dirty: mobx.computed,
806
816
  blurred: mobx.computed,
807
817
  errorMessage: mobx.computed,
808
818
  rawError: mobx.observable.ref,
@@ -890,15 +900,22 @@
890
900
  return field.restoreInitialValue(opts);
891
901
  });
892
902
  }
903
+ }, {
904
+ key: "commit",
905
+ value: function commit() {
906
+ this._eachField(function (field) {
907
+ return field.commit();
908
+ });
909
+ }
910
+ }, {
911
+ key: "updateFrom",
912
+
893
913
  /**
894
914
  * Set multiple values to more than one field a time using an object
895
915
  * where each key is the name of a field. The value will be set to each
896
916
  * field and from that point on the values set are considered the new
897
917
  * initial values. Validation and interacted flags are also reset if the second argument is true
898
918
  * */
899
-
900
- }, {
901
- key: "updateFrom",
902
919
  value: function updateFrom(obj) {
903
920
  var _this = this;
904
921
 
@@ -1041,12 +1058,23 @@
1041
1058
  });
1042
1059
  }
1043
1060
  }, {
1044
- key: "summary",
1061
+ key: "dirty",
1045
1062
  get: function get() {
1046
1063
  var _this8 = this;
1047
1064
 
1065
+ return this._fieldKeys().some(function (key) {
1066
+ var f = _this8._getField(key);
1067
+
1068
+ return f.dirty;
1069
+ });
1070
+ }
1071
+ }, {
1072
+ key: "summary",
1073
+ get: function get() {
1074
+ var _this9 = this;
1075
+
1048
1076
  return this._fieldKeys().reduce(function (seq, key) {
1049
- var field = _this8.fields[key];
1077
+ var field = _this9.fields[key];
1050
1078
 
1051
1079
  if (field.errorMessage) {
1052
1080
  seq.push(field.errorMessage);
@@ -1058,10 +1086,10 @@
1058
1086
  }, {
1059
1087
  key: "validating",
1060
1088
  get: function get() {
1061
- var _this9 = this;
1089
+ var _this10 = this;
1062
1090
 
1063
1091
  return this._validating || this._fieldKeys().some(function (key) {
1064
- var f = _this9._getField(key);
1092
+ var f = _this10._getField(key);
1065
1093
 
1066
1094
  return f.validating;
1067
1095
  });
@@ -1069,11 +1097,11 @@
1069
1097
  }, {
1070
1098
  key: "serializedData",
1071
1099
  get: function get() {
1072
- var _this10 = this;
1100
+ var _this11 = this;
1073
1101
 
1074
1102
  var keys = Object.keys(this.fields);
1075
1103
  return mobx.toJS(keys.reduce(function (seq, key) {
1076
- var field = _this10.fields[key];
1104
+ var field = _this11.fields[key];
1077
1105
  var value = mobx.toJS(field.value); // this is required to make sure forms that use the serializedData object
1078
1106
  // have the values without leading or trailing spaces
1079
1107
 
@@ -1094,7 +1122,7 @@
1094
1122
  }]);
1095
1123
 
1096
1124
  function FormModel() {
1097
- var _this11 = this;
1125
+ var _this12 = this;
1098
1126
 
1099
1127
  var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
1100
1128
  _ref2$descriptors = _ref2.descriptors,
@@ -1109,7 +1137,7 @@
1109
1137
  this._validating = false;
1110
1138
 
1111
1139
  this.setValidating = function (validating) {
1112
- _this11._validating = validating;
1140
+ _this12._validating = validating;
1113
1141
  };
1114
1142
 
1115
1143
  this.addFields = function (fieldsDescriptor) {
@@ -1123,7 +1151,7 @@
1123
1151
  name = field.name,
1124
1152
  descriptor = _objectWithoutProperties(field, ["value", "name"]);
1125
1153
 
1126
- _this11._createField({
1154
+ _this12._createField({
1127
1155
  value: value,
1128
1156
  name: name,
1129
1157
  descriptor: descriptor
@@ -1138,7 +1166,7 @@
1138
1166
  value = _fieldsDescriptor$key.value,
1139
1167
  descriptor = _objectWithoutProperties(_fieldsDescriptor$key, ["value"]);
1140
1168
 
1141
- _this11._createField({
1169
+ _this12._createField({
1142
1170
  value: value,
1143
1171
  name: key,
1144
1172
  descriptor: descriptor
@@ -1167,11 +1195,14 @@
1167
1195
  resetInteractedFlag: mobx.action,
1168
1196
  disableFields: mobx.action,
1169
1197
  addFields: mobx.action,
1170
- enableFields: mobx.action
1198
+ enableFields: mobx.action,
1199
+ commit: mobx.action,
1200
+ dirty: mobx.computed
1171
1201
  });
1172
1202
  this.addFields(descriptors);
1173
1203
  initialState && this.updateFrom(initialState, {
1174
- throwIfMissingField: options.throwIfMissingField
1204
+ throwIfMissingField: options.throwIfMissingField,
1205
+ commit: true
1175
1206
  });
1176
1207
  }
1177
1208
 
@@ -1193,10 +1224,10 @@
1193
1224
  }, {
1194
1225
  key: "_eachField",
1195
1226
  value: function _eachField(cb) {
1196
- var _this12 = this;
1227
+ var _this13 = this;
1197
1228
 
1198
1229
  Object.keys(this.fields).forEach(function (key) {
1199
- return cb(_this12.fields[key]);
1230
+ return cb(_this13.fields[key]);
1200
1231
  });
1201
1232
  }
1202
1233
  }, {
@@ -1214,11 +1245,11 @@
1214
1245
  }, {
1215
1246
  key: "disableFields",
1216
1247
  value: function disableFields(fieldKeys) {
1217
- var _this13 = this;
1248
+ var _this14 = this;
1218
1249
 
1219
1250
  if (!Array.isArray(fieldKeys)) throw new TypeError('fieldKeys should be an array with the names of the fields to disable');
1220
1251
  fieldKeys.forEach(function (key) {
1221
- var field = _this13._getField(key);
1252
+ var field = _this14._getField(key);
1222
1253
 
1223
1254
  field.setDisabled(true);
1224
1255
  });
@@ -1234,11 +1265,11 @@
1234
1265
  }, {
1235
1266
  key: "enableFields",
1236
1267
  value: function enableFields(fieldKeys) {
1237
- var _this14 = this;
1268
+ var _this15 = this;
1238
1269
 
1239
1270
  if (!Array.isArray(fieldKeys)) throw new TypeError('fieldKeys should be an array with the names of the fields to disable');
1240
1271
  fieldKeys.forEach(function (key) {
1241
- var field = _this14._getField(key);
1272
+ var field = _this15._getField(key);
1242
1273
 
1243
1274
  field.setDisabled(false);
1244
1275
  });
@@ -1246,10 +1277,10 @@
1246
1277
  }, {
1247
1278
  key: "resetValidatedOnce",
1248
1279
  value: function resetValidatedOnce() {
1249
- var _this15 = this;
1280
+ var _this16 = this;
1250
1281
 
1251
1282
  this._fieldKeys().forEach(function (key) {
1252
- _this15.fields[key].resetValidatedOnce();
1283
+ _this16.fields[key].resetValidatedOnce();
1253
1284
  });
1254
1285
  }
1255
1286
  }]);
package/mobx-form.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  declare module 'mobx-form' {
2
+
3
+ export type Ret = boolean | { error: string } | void;
2
4
  export interface IValidatorFn<FieldType, ModelType> {
3
- (field: IField<FieldType, ModelType>, fields: { [P in keyof ModelType]: IField<ModelType[P], ModelType> }, formModel: IFormModel<ModelType>): Promise<void>;
5
+ (field: IField<FieldType, ModelType>, fields: { [P in keyof ModelType]: IField<ModelType[P], ModelType> }, formModel: IFormModel<ModelType>): Promise<Ret> | Ret;
4
6
  }
5
7
 
6
8
  export interface IHasValueFn<T> {
@@ -137,11 +139,15 @@ declare module 'mobx-form' {
137
139
 
138
140
  interacted: boolean;
139
141
 
140
- restoreInitialValues(options?: ResetInteractedFlagType): void;
142
+ dirty: boolean;
143
+
144
+ commit(): void;
145
+
146
+ restoreInitialValues(options?: SetValueFnArgs): void;
141
147
 
142
148
  resetInteractedFlag(options?: ResetInteractedFlagType): void;
143
149
 
144
- updateFrom(obj: Partial<T>, options?: ResetInteractedFlagType): void;
150
+ updateFrom(obj: Partial<T>, options?: SetValueFnArgs): void;
145
151
 
146
152
  enableFields(fieldNames: string[]): void;
147
153
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobx-form",
3
- "version": "13.3.0",
3
+ "version": "13.3.2",
4
4
  "description": "A simple form helper for mobx",
5
5
  "main": "dist/mobx-form.cjs.js",
6
6
  "_browser": "dist/mobx-form.umd.js",