@zajno/common 1.3.15 → 1.4.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.
Files changed (94) hide show
  1. package/coverage/clover.xml +105 -91
  2. package/coverage/coverage-final.json +6 -6
  3. package/coverage/lcov-report/index.html +19 -19
  4. package/coverage/lcov-report/src/__tests__/helpers/index.html +1 -1
  5. package/coverage/lcov-report/src/__tests__/helpers/main.ts.html +1 -1
  6. package/coverage/lcov-report/src/async/arrays.ts.html +1 -1
  7. package/coverage/lcov-report/src/async/index.html +1 -1
  8. package/coverage/lcov-report/src/dates/calc.ts.html +1 -1
  9. package/coverage/lcov-report/src/dates/convert.ts.html +1 -1
  10. package/coverage/lcov-report/src/dates/datex.ts.html +1 -1
  11. package/coverage/lcov-report/src/dates/format.ts.html +1 -1
  12. package/coverage/lcov-report/src/dates/index.html +1 -1
  13. package/coverage/lcov-report/src/dates/index.ts.html +1 -1
  14. package/coverage/lcov-report/src/dates/parse.ts.html +1 -1
  15. package/coverage/lcov-report/src/dates/period.ts.html +1 -1
  16. package/coverage/lcov-report/src/dates/shift.ts.html +1 -1
  17. package/coverage/lcov-report/src/dates/types.ts.html +1 -1
  18. package/coverage/lcov-report/src/dates/yearDate.ts.html +1 -1
  19. package/coverage/lcov-report/src/enumHelper.ts.html +1 -1
  20. package/coverage/lcov-report/src/event.ts.html +1 -1
  21. package/coverage/lcov-report/src/fields/index.html +1 -1
  22. package/coverage/lcov-report/src/fields/update.ts.html +1 -1
  23. package/coverage/lcov-report/src/functions.ts.html +1 -1
  24. package/coverage/lcov-report/src/index.html +1 -1
  25. package/coverage/lcov-report/src/lazy.light.ts.html +4 -4
  26. package/coverage/lcov-report/src/logger/batch.ts.html +1 -1
  27. package/coverage/lcov-report/src/logger/console.ts.html +1 -1
  28. package/coverage/lcov-report/src/logger/index.html +1 -1
  29. package/coverage/lcov-report/src/logger/index.ts.html +1 -1
  30. package/coverage/lcov-report/src/logger/named.ts.html +1 -1
  31. package/coverage/lcov-report/src/logger/proxy.ts.html +1 -1
  32. package/coverage/lcov-report/src/math/arrays.ts.html +1 -1
  33. package/coverage/lcov-report/src/math/calc.ts.html +1 -1
  34. package/coverage/lcov-report/src/math/distribution.ts.html +1 -1
  35. package/coverage/lcov-report/src/math/index.html +1 -1
  36. package/coverage/lcov-report/src/math/index.ts.html +1 -1
  37. package/coverage/lcov-report/src/transitionObserver.ts.html +1 -1
  38. package/coverage/lcov-report/src/types.ts.html +1 -1
  39. package/coverage/lcov-report/src/validation/ValidationErrors.ts.html +1 -1
  40. package/coverage/lcov-report/src/validation/creditCard.ts.html +1 -1
  41. package/coverage/lcov-report/src/validation/helpers.ts.html +1 -1
  42. package/coverage/lcov-report/src/validation/index.html +1 -1
  43. package/coverage/lcov-report/src/validation/index.ts.html +1 -1
  44. package/coverage/lcov-report/src/validation/types.ts.html +1 -1
  45. package/coverage/lcov-report/src/validation/validators.ts.html +1 -1
  46. package/coverage/lcov-report/src/validation/wrappers.ts.html +1 -1
  47. package/coverage/lcov-report/src/viewModels/FlagModel.ts.html +25 -10
  48. package/coverage/lcov-report/src/viewModels/MultiSelectModel.ts.html +20 -8
  49. package/coverage/lcov-report/src/viewModels/NumberModel.ts.html +24 -9
  50. package/coverage/lcov-report/src/viewModels/SelectModel.ts.html +48 -21
  51. package/coverage/lcov-report/src/viewModels/Validatable.ts.html +42 -18
  52. package/coverage/lcov-report/src/viewModels/index.html +52 -52
  53. package/coverage/lcov-report/src/viewModels/wrappers.ts.html +1 -1
  54. package/coverage/lcov.info +266 -237
  55. package/lib/viewModels/CommonModel.d.ts +13 -0
  56. package/lib/viewModels/CommonModel.d.ts.map +1 -0
  57. package/lib/viewModels/CommonModel.js +37 -0
  58. package/lib/viewModels/CommonModel.js.map +1 -0
  59. package/lib/viewModels/FlagModel.d.ts +1 -0
  60. package/lib/viewModels/FlagModel.d.ts.map +1 -1
  61. package/lib/viewModels/FlagModel.js +8 -1
  62. package/lib/viewModels/FlagModel.js.map +1 -1
  63. package/lib/viewModels/MultiSelectModel.d.ts.map +1 -1
  64. package/lib/viewModels/MultiSelectModel.js +3 -0
  65. package/lib/viewModels/MultiSelectModel.js.map +1 -1
  66. package/lib/viewModels/NumberModel.d.ts +1 -0
  67. package/lib/viewModels/NumberModel.d.ts.map +1 -1
  68. package/lib/viewModels/NumberModel.js +8 -1
  69. package/lib/viewModels/NumberModel.js.map +1 -1
  70. package/lib/viewModels/SelectModel.d.ts +1 -0
  71. package/lib/viewModels/SelectModel.d.ts.map +1 -1
  72. package/lib/viewModels/SelectModel.js +24 -14
  73. package/lib/viewModels/SelectModel.js.map +1 -1
  74. package/lib/viewModels/TextModel.d.ts +2 -0
  75. package/lib/viewModels/TextModel.d.ts.map +1 -1
  76. package/lib/viewModels/TextModel.js +27 -23
  77. package/lib/viewModels/TextModel.js.map +1 -1
  78. package/lib/viewModels/Validatable.d.ts +3 -1
  79. package/lib/viewModels/Validatable.d.ts.map +1 -1
  80. package/lib/viewModels/Validatable.js +14 -7
  81. package/lib/viewModels/Validatable.js.map +1 -1
  82. package/lib/viewModels/index.d.ts +1 -0
  83. package/lib/viewModels/index.d.ts.map +1 -1
  84. package/lib/viewModels/index.js +1 -0
  85. package/lib/viewModels/index.js.map +1 -1
  86. package/package.json +1 -1
  87. package/src/viewModels/CommonModel.ts +43 -0
  88. package/src/viewModels/FlagModel.ts +6 -1
  89. package/src/viewModels/MultiSelectModel.ts +4 -0
  90. package/src/viewModels/NumberModel.ts +6 -1
  91. package/src/viewModels/SelectModel.ts +11 -2
  92. package/src/viewModels/TextModel.ts +21 -11
  93. package/src/viewModels/Validatable.ts +16 -8
  94. package/src/viewModels/index.ts +1 -0
@@ -1,5 +1,5 @@
1
1
  import { createLazy } from '../lazy.light';
2
- import { observable, computed, makeObservable, reaction } from 'mobx';
2
+ import { observable, computed, makeObservable, reaction, action } from 'mobx';
3
3
  import { FlagModel, ILabeledFlagModel } from './FlagModel';
4
4
  import { ValidatableModel } from './Validatable';
5
5
  import { IValueModel } from './types';
@@ -75,12 +75,21 @@ export class Select<T = any> extends ValidatableModel<T> implements IValueModel<
75
75
  }
76
76
 
77
77
  set index(val: number) {
78
+ this.setIndex(val);
79
+ }
80
+
81
+ @action
82
+ public setIndex = (val: number) => {
78
83
  if (this._indexLocked) {
79
84
  return;
80
85
  }
81
86
 
82
87
  this._index = val;
83
88
 
89
+ if (this._validateOnChange) {
90
+ this.validate();
91
+ }
92
+
84
93
  // update all flags to be properly selected
85
94
  try {
86
95
  this._indexLocked = true;
@@ -91,7 +100,7 @@ export class Select<T = any> extends ValidatableModel<T> implements IValueModel<
91
100
  } finally {
92
101
  this._indexLocked = false;
93
102
  }
94
- }
103
+ };
95
104
 
96
105
  reset = () => {
97
106
  super.reset();
@@ -1,4 +1,4 @@
1
- import { observable, computed, makeObservable, reaction } from 'mobx';
1
+ import { observable, makeObservable, reaction, action } from 'mobx';
2
2
  import { Getter } from '../types';
3
3
  import { IValueModel } from './types';
4
4
  import logger from '../logger';
@@ -46,10 +46,7 @@ export class TextInputVM extends ValidatableModel<string> implements IValueModel
46
46
  @observable
47
47
  private _focused = false;
48
48
 
49
- @observable
50
49
  private _name: string = null;
51
-
52
- @observable
53
50
  private _title: string = null;
54
51
 
55
52
  private readonly _valueObserving: () => void = null;
@@ -71,14 +68,22 @@ export class TextInputVM extends ValidatableModel<string> implements IValueModel
71
68
  get title() { return this._title; }
72
69
 
73
70
  set value(val) {
71
+ this.setValue(val);
72
+ }
73
+
74
+ @action
75
+ public readonly setValue = (value: string) => {
74
76
  if (!this._valueObserving) {
75
- this._value = val;
77
+ this._value = value;
78
+
79
+ if (this._validateOnChange) {
80
+ this.validate();
81
+ }
76
82
  } else {
77
83
  logger.warn('[TextInputViewModel] Setting value is not allowed when value is observing');
78
84
  }
79
- }
85
+ };
80
86
 
81
- @computed
82
87
  get isEmpty() {
83
88
  return !this._value;
84
89
  }
@@ -87,15 +92,20 @@ export class TextInputVM extends ValidatableModel<string> implements IValueModel
87
92
  return this._focused;
88
93
  }
89
94
 
90
- set focused(val) {
91
- this._focused = val;
92
- if (!val) {
95
+ set focused(val: boolean) {
96
+ this.setFocused(val);
97
+ }
98
+
99
+ @action
100
+ public readonly setFocused = (value = true) => {
101
+ this._focused = value;
102
+ if (!value) {
93
103
  this.onBlur();
94
104
  return;
95
105
  }
96
106
 
97
107
  super.reset();
98
- }
108
+ };
99
109
 
100
110
  protected get valueToValidate() { return (this.value ?? '').trim(); }
101
111
 
@@ -1,9 +1,9 @@
1
- import { observable, makeObservable, action } from 'mobx';
1
+ import { observable, makeObservable, action, runInAction } from 'mobx';
2
2
  import { ValidatorFunction, ValidatorFunctionAsync, ValidationErrors, ValidationError } from '../validation';
3
3
  import { someAsync } from '../async/arrays';
4
4
 
5
5
  export type ValueValidator<T, TErrors = ValidationErrors> = ValidatorFunction<T, TErrors> | ValidatorFunctionAsync<T, TErrors>;
6
- export type ValidationErrorsStrings<TErrors extends string | number = number> = Record<TErrors, string>;
6
+ export type ValidationErrorsStrings<TErrors extends string | number = number> = Partial<Omit<Record<TErrors, string>, 0 | null>>;
7
7
 
8
8
  export type ValidationConfig<T, TErrors extends string | number = ValidationErrors> = {
9
9
  validator: ValueValidator<Readonly<T>, TErrors>,
@@ -21,6 +21,7 @@ export abstract class ValidatableModel<T = string> {
21
21
  private _error: string = null;
22
22
 
23
23
  private _validationError: ValidationError = null;
24
+ protected _validateOnChange = false;
24
25
 
25
26
  constructor() {
26
27
  makeObservable(this);
@@ -38,6 +39,11 @@ export abstract class ValidatableModel<T = string> {
38
39
  return this;
39
40
  }
40
41
 
42
+ public validateOnChange(enable = true) {
43
+ this._validateOnChange = enable;
44
+ return this;
45
+ }
46
+
41
47
  public async testValidate(value: T) {
42
48
  if (this._validator) {
43
49
  const res = await this._validator(value);
@@ -60,12 +66,14 @@ export abstract class ValidatableModel<T = string> {
60
66
  this._validationError = err as ValidationError;
61
67
  }
62
68
 
63
- if (!this._validationError) {
64
- this._error = null;
65
- } else {
66
- const code = this._validationError.code;
67
- this._error = this._strings && this._strings[code];
68
- }
69
+ runInAction(() => {
70
+ if (!this._validationError) {
71
+ this._error = null;
72
+ } else {
73
+ const code = this._validationError.code;
74
+ this._error = this._strings && this._strings[code];
75
+ }
76
+ });
69
77
  return this._validationError == null;
70
78
  }
71
79
 
@@ -1,5 +1,6 @@
1
1
  export * from './types';
2
2
  export * from './Validatable';
3
+ export * from './CommonModel';
3
4
  export * from './TextModel';
4
5
  export * from './FlagModel';
5
6
  export * from './NumberModel';