@codetectonics/mantle 1.0.12 → 1.0.13

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.
@@ -2165,35 +2165,63 @@ class NumberInputComponent {
2165
2165
  registerOnChange(fn) { this.onChange = fn; }
2166
2166
  registerOnTouched(fn) { this.onTouched = fn; }
2167
2167
  ngOnChanges() { }
2168
+ /**
2169
+ * Utility: safely coerce input into number or null
2170
+ */
2171
+ coerceToNumber(value) {
2172
+ if (value === null || value === undefined)
2173
+ return null;
2174
+ if (typeof value === 'number' && !isNaN(value))
2175
+ return value;
2176
+ if (typeof value === 'string') {
2177
+ const trimmed = value.trim();
2178
+ if (trimmed === '')
2179
+ return null;
2180
+ // Match valid numeric string: -12, 3.14, 0, -0.5, 1e5
2181
+ if (/^-?\d+(\.\d+)?([eE][-+]?\d+)?$/.test(trimmed)) {
2182
+ const n = Number(trimmed);
2183
+ return Number.isFinite(n) ? n : null;
2184
+ }
2185
+ return null; // treat non-numeric strings as null
2186
+ }
2187
+ return null;
2188
+ }
2168
2189
  writeValue(newValue) {
2190
+ newValue = this.coerceToNumber(newValue);
2169
2191
  this.value = newValue;
2170
2192
  this.onChange(newValue);
2171
2193
  }
2172
2194
  onValueChanged(event) {
2195
+ this.value = this.coerceToNumber(this.value);
2173
2196
  this.onChange(this.value);
2174
2197
  }
2175
2198
  validate(control) {
2176
- if (control.value === null) {
2199
+ const val = this.coerceToNumber(control.value);
2200
+ // Replace invalid values in form control with null
2201
+ if (val === null) {
2177
2202
  this.errorMessage = this.error;
2178
2203
  return null;
2179
2204
  }
2180
- let errors = {};
2181
- if (this.options.min && control.value < this.options.min)
2205
+ const errors = {};
2206
+ if (this.options.min !== undefined && val < this.options.min) {
2182
2207
  errors.min = `must be greater than or equal to ${this.options.min}`;
2183
- if (this.options.max && control.value > this.options.max)
2208
+ }
2209
+ if (this.options.max !== undefined && val > this.options.max) {
2184
2210
  errors.max = `must be lower than or equal to ${this.options.max}`;
2185
- if (this.options.decimal_places) {
2186
- if (this.options.decimal_places == 0) {
2187
- if ((control.value - Math.floor(control.value)) !== 0)
2188
- errors.decimal_places = 'cannot be a decimal';
2189
- }
2190
- else {
2191
- if ((control.value - control.value.toFixed(2)) !== 0)
2192
- errors.decimal_places = `must have a maximum of ${this.options.decimal_places} decimal places`;
2211
+ }
2212
+ if (this.options.decimal_places !== undefined) {
2213
+ const factor = Math.pow(10, this.options.decimal_places);
2214
+ if (Math.round(val * factor) / factor !== val) {
2215
+ errors.decimal_places =
2216
+ this.options.decimal_places === 0
2217
+ ? 'cannot be a decimal'
2218
+ : `must have a maximum of ${this.options.decimal_places} decimal places`;
2193
2219
  }
2194
2220
  }
2195
- this.errorMessage = [...[this.error], ...Object.values(errors)].filter(item => { return !!item; }).join(', ');
2196
- return errors ? errors : null;
2221
+ this.errorMessage = [this.error, ...Object.values(errors)]
2222
+ .filter(Boolean)
2223
+ .join(', ');
2224
+ return Object.keys(errors).length > 0 ? errors : null;
2197
2225
  }
2198
2226
  static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: NumberInputComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
2199
2227
  static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "20.1.3", type: NumberInputComponent, isStandalone: false, selector: "mantle-number-input", inputs: { label: "label", tooltip: "tooltip", options: "options", disable: "disable", required: "required", error: "error" }, providers: [