@ebuilding/form 2.3.9 → 2.3.10

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.
@@ -1457,25 +1457,16 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.15", ngImpo
1457
1457
 
1458
1458
  class DeonNzFieldNumber extends FieldType {
1459
1459
  get precision() {
1460
- if (this.props["precision"] && this.props["precision"] > 0) {
1461
- return this.props["precision"];
1462
- }
1463
- return 0;
1460
+ return this.props?.['precision'] > 0 ? this.props['precision'] : 0;
1464
1461
  }
1465
1462
  get min() {
1466
- if (this.props["min"] != undefined && this.props["min"] != null) {
1467
- return this.props["min"];
1468
- }
1469
- return -999999999;
1463
+ return this.props?.['min'] ?? -99999999;
1470
1464
  }
1471
1465
  get max() {
1472
- if (this.props["max"] != undefined && this.props["max"] != null) {
1473
- return this.props["max"];
1474
- }
1475
- return 99999999;
1466
+ return this.props?.['max'] ?? 99999999;
1476
1467
  }
1477
1468
  get password() {
1478
- return this.props?.["password"] === true;
1469
+ return this.props?.['password'] === true;
1479
1470
  }
1480
1471
  constructor() {
1481
1472
  super();
@@ -1487,23 +1478,48 @@ class DeonNzFieldNumber extends FieldType {
1487
1478
  return String(value); // 不要 format,小数位留给 blur 处理
1488
1479
  };
1489
1480
  parseNumber = (value) => {
1490
- const parsed = parseFloat(value);
1491
- return isNaN(parsed) ? null : parsed;
1481
+ const num = Number(value);
1482
+ return isNaN(num) ? this.formControl.value ?? 0 : num;
1492
1483
  };
1493
- onFormatPrecision() {
1494
- let value = this.formControl.value;
1495
- // 空值直接跳过,不格式化
1484
+ onBeforeInput(event) {
1485
+ // 删除 / 撤销 / 重做 直接放行
1486
+ if (event.inputType?.startsWith('delete') ||
1487
+ event.inputType === 'historyUndo' ||
1488
+ event.inputType === 'historyRedo') {
1489
+ return;
1490
+ }
1491
+ const data = event.data ?? '';
1492
+ if (!data)
1493
+ return;
1494
+ const next = this.calcNextValue(data);
1495
+ const reg = this.precision > 0
1496
+ ? /^-?\d*(\.\d*)?$/
1497
+ : /^-?\d*$/;
1498
+ // 不符合数字规则,直接阻止
1499
+ if (!reg.test(next)) {
1500
+ event.preventDefault();
1501
+ }
1502
+ }
1503
+ /* ===== 计算“即将写入后的值” ===== */
1504
+ calcNextValue(insert) {
1505
+ const input = document.activeElement;
1506
+ if (!input)
1507
+ return insert;
1508
+ const start = input.selectionStart ?? 0;
1509
+ const end = input.selectionEnd ?? 0;
1510
+ const value = input.value ?? '';
1511
+ return value.slice(0, start) + insert + value.slice(end);
1512
+ }
1513
+ /* ===== blur 时统一格式化 ===== */
1514
+ onBlurFormat() {
1515
+ const value = this.formControl.value;
1496
1516
  if (value === null || value === undefined || value === '')
1497
1517
  return;
1498
- // 强制转成数字
1499
1518
  const num = Number(value);
1500
1519
  if (isNaN(num))
1501
1520
  return;
1502
- // 设置精度
1503
1521
  const fixed = num.toFixed(this.precision);
1504
- // 注意:toFixed 返回 string,但 formControl 可能希望是 number
1505
- // 所以统一再转 number 避免下游报错
1506
- this.formControl.setValue(this.precision > 0 ? Number(fixed) : parseInt(fixed), { emitEvent: false });
1522
+ this.formControl.setValue(this.precision > 0 ? Number(fixed) : parseInt(fixed, 10), { emitEvent: false });
1507
1523
  }
1508
1524
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.15", ngImport: i0, type: DeonNzFieldNumber, deps: [], target: i0.ɵɵFactoryTarget.Component });
1509
1525
  static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "19.2.15", type: DeonNzFieldNumber, isStandalone: true, selector: "deon-nz-field-number", usesInheritance: true, ngImport: i0, template: `
@@ -1520,7 +1536,8 @@ class DeonNzFieldNumber extends FieldType {
1520
1536
  [nzMax]="max"
1521
1537
  [nzFormatter]="formatNumber"
1522
1538
  [nzParser]="parseNumber"
1523
- (nzBlur)="onFormatPrecision()"
1539
+ (beforeinput)="onBeforeInput($event)"
1540
+ (nzBlur)="onBlurFormat()"
1524
1541
  ></nz-input-number>
1525
1542
  </nz-input-group>
1526
1543
  </ng-container>
@@ -1535,7 +1552,8 @@ class DeonNzFieldNumber extends FieldType {
1535
1552
  [nzMax]="max"
1536
1553
  [nzFormatter]="formatNumber"
1537
1554
  [nzParser]="parseNumber"
1538
- (nzBlur)="onFormatPrecision()"
1555
+ (beforeinput)="onBeforeInput($event)"
1556
+ (nzBlur)="onBlurFormat()"
1539
1557
  ></nz-input-number>
1540
1558
  </ng-template>
1541
1559
  </ng-container>
@@ -1561,7 +1579,8 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.15", ngImpo
1561
1579
  [nzMax]="max"
1562
1580
  [nzFormatter]="formatNumber"
1563
1581
  [nzParser]="parseNumber"
1564
- (nzBlur)="onFormatPrecision()"
1582
+ (beforeinput)="onBeforeInput($event)"
1583
+ (nzBlur)="onBlurFormat()"
1565
1584
  ></nz-input-number>
1566
1585
  </nz-input-group>
1567
1586
  </ng-container>
@@ -1576,7 +1595,8 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.15", ngImpo
1576
1595
  [nzMax]="max"
1577
1596
  [nzFormatter]="formatNumber"
1578
1597
  [nzParser]="parseNumber"
1579
- (nzBlur)="onFormatPrecision()"
1598
+ (beforeinput)="onBeforeInput($event)"
1599
+ (nzBlur)="onBlurFormat()"
1580
1600
  ></nz-input-number>
1581
1601
  </ng-template>
1582
1602
  </ng-container>