@telcomdev/ui 0.1.19 → 0.1.20

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.
@@ -3562,6 +3562,7 @@ class TdInput {
3562
3562
  legacyMaxLength = signal(null, /* @ts-ignore */
3563
3563
  ...(ngDevMode ? [{ debugName: "legacyMaxLength" }] : /* istanbul ignore next */ []));
3564
3564
  receivedNullValue = false;
3565
+ decimalMaskDigits = '';
3565
3566
  nativeInput;
3566
3567
  id = '';
3567
3568
  label = 'Campo';
@@ -3698,15 +3699,23 @@ class TdInput {
3698
3699
  writeValue(value) {
3699
3700
  if (value === null || value === undefined) {
3700
3701
  this.receivedNullValue = true;
3702
+ this.decimalMaskDigits = '';
3701
3703
  this.value.set(null);
3702
3704
  }
3703
3705
  else if (this.type === 'number' &&
3704
3706
  this.formBridge.usesSignalForms &&
3705
3707
  typeof value === 'number') {
3708
+ this.decimalMaskDigits = this.isDecimalMask()
3709
+ ? this.decimalDigitsFromProgrammaticValue(String(value))
3710
+ : '';
3706
3711
  this.value.set(value);
3707
3712
  }
3708
3713
  else {
3709
- this.value.set(this.transformTextValue(String(value)));
3714
+ const textValue = String(value);
3715
+ if (this.isDecimalMask()) {
3716
+ this.decimalMaskDigits = this.decimalDigitsFromProgrammaticValue(textValue);
3717
+ }
3718
+ this.value.set(this.transformTextValue(textValue));
3710
3719
  }
3711
3720
  this.cdr.markForCheck();
3712
3721
  }
@@ -3724,6 +3733,10 @@ class TdInput {
3724
3733
  }
3725
3734
  handleInput(event) {
3726
3735
  const inputElement = event.target;
3736
+ if (this.isDecimalMask()) {
3737
+ this.handleDecimalMaskInput(inputElement, event);
3738
+ return;
3739
+ }
3727
3740
  const transformedValue = this.transformTextValue(inputElement.value);
3728
3741
  if (inputElement.value !== transformedValue) {
3729
3742
  inputElement.value = transformedValue;
@@ -3739,6 +3752,7 @@ class TdInput {
3739
3752
  this.formBridge.markTouched(this.touched, this.touch);
3740
3753
  }
3741
3754
  clear() {
3755
+ this.decimalMaskDigits = '';
3742
3756
  this.setValue(this.resolveEmptyValue());
3743
3757
  }
3744
3758
  parseInputValue(value, inputElement) {
@@ -3820,13 +3834,32 @@ class TdInput {
3820
3834
  return value;
3821
3835
  }
3822
3836
  maskDecimal(value) {
3837
+ return this.formatDecimalMaskDigits(value.replace(/\D/g, ''));
3838
+ }
3839
+ handleDecimalMaskInput(inputElement, event) {
3840
+ const inputEvent = event;
3841
+ const inputType = inputEvent.inputType || '';
3842
+ const dataDigits = (inputEvent.data ?? '').replace(/\D/g, '');
3843
+ const maxDigits = this.maxDecimalMaskDigits();
3844
+ if (inputType.startsWith('delete')) {
3845
+ this.decimalMaskDigits = this.decimalMaskDigits.slice(0, -1);
3846
+ }
3847
+ else if (dataDigits) {
3848
+ this.decimalMaskDigits = this.limitDigits(this.decimalMaskDigits + dataDigits, maxDigits);
3849
+ }
3850
+ else {
3851
+ this.decimalMaskDigits = this.limitDigits(this.decimalDigitsFromEditedValue(inputElement.value), maxDigits);
3852
+ }
3853
+ const transformedValue = this.formatDecimalMaskDigits(this.decimalMaskDigits);
3854
+ inputElement.value = transformedValue;
3855
+ this.moveCaretToEnd(inputElement);
3856
+ this.setValue(this.parseInputValue(transformedValue, inputElement));
3857
+ }
3858
+ formatDecimalMaskDigits(digitsValue) {
3823
3859
  const config = this.resolveDecimalMaskConfig();
3824
3860
  const scale = config.decimalDigits;
3825
3861
  const integerLimit = config.integerDigits;
3826
- const maxDigits = integerLimit && integerLimit > 0
3827
- ? integerLimit + scale
3828
- : null;
3829
- const digits = this.limitDigits(value.replace(/\D/g, ''), maxDigits);
3862
+ const digits = this.limitDigits(digitsValue.replace(/\D/g, ''), this.maxDecimalMaskDigits());
3830
3863
  if (!digits)
3831
3864
  return '';
3832
3865
  if (scale === 0)
@@ -3837,6 +3870,29 @@ class TdInput {
3837
3870
  const decimalPart = padded.slice(splitIndex);
3838
3871
  return `${integerPart || '0'}.${decimalPart}`;
3839
3872
  }
3873
+ maxDecimalMaskDigits() {
3874
+ const config = this.resolveDecimalMaskConfig();
3875
+ const scale = config.decimalDigits;
3876
+ const integerLimit = config.integerDigits;
3877
+ return integerLimit && integerLimit > 0 ? integerLimit + scale : null;
3878
+ }
3879
+ decimalDigitsFromEditedValue(value) {
3880
+ return value.replace(/\D/g, '').replace(/^0+(?=\d)/, '');
3881
+ }
3882
+ decimalDigitsFromProgrammaticValue(value) {
3883
+ return this.decimalDigitsFromEditedValue(value);
3884
+ }
3885
+ moveCaretToEnd(inputElement) {
3886
+ queueMicrotask(() => {
3887
+ const end = inputElement.value.length;
3888
+ try {
3889
+ inputElement.setSelectionRange(end, end);
3890
+ }
3891
+ catch {
3892
+ // type="number" no soporta setSelectionRange en algunos navegadores.
3893
+ }
3894
+ });
3895
+ }
3840
3896
  isDecimalMask() {
3841
3897
  return this.mask === 'decimal' || this.mask.startsWith('decimal(');
3842
3898
  }