ng-tailwind 6.4.7 → 6.4.9

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.
@@ -102,6 +102,7 @@ export declare class NgtInputComponent extends NgtBaseNgModel implements OnInit,
102
102
  private initComponent;
103
103
  private updateValidations;
104
104
  private setupMasks;
105
+ private setupAlphanumericPasteHandler;
105
106
  private setupProperties;
106
107
  private minValueValidator;
107
108
  private multipleOfValidator;
@@ -115,10 +116,15 @@ export declare class NgtInputComponent extends NgtBaseNgModel implements OnInit,
115
116
  private validatePhone;
116
117
  private uniqueValidator;
117
118
  private validatorCNPJ;
119
+ private validatorNumericCNPJ;
120
+ private validatorAlphaNumericCNPJ;
121
+ private getCnpjVerificationDigit;
122
+ private getCnpjCharacterValue;
118
123
  private validatorCPF;
119
124
  private getNativeValue;
120
125
  private getElementTitle;
121
126
  private removeMasks;
127
+ private normalizeDocument;
122
128
  private hasEmailServiceValidation;
123
129
  private hasPasswordValidation;
124
130
  private isDisabledByParent;
@@ -2450,15 +2450,25 @@ class NgtInputComponent extends NgtBaseNgModel {
2450
2450
  removeInputMask(this.element.nativeElement);
2451
2451
  return this.clearInput();
2452
2452
  }
2453
+ const cnpjMaskPattern = 'XX.XXX.XXX/XXXX-99';
2454
+ const onBeforeCnpjPaste = (pastedValue) => pastedValue.toUpperCase().replace(/[^A-Z0-9]/g, '');
2455
+ const cnpjMaskParameters = {
2456
+ mask: [cnpjMaskPattern],
2457
+ definitions: {
2458
+ X: {
2459
+ validator: '[0-9A-Za-z]',
2460
+ casing: 'upper'
2461
+ }
2462
+ },
2463
+ showMaskOnHover: false,
2464
+ onBeforePaste: onBeforeCnpjPaste
2465
+ };
2453
2466
  let masks = {
2454
2467
  [InputMaskEnum.CPF]: {
2455
2468
  mask: ['999.999.999-99'],
2456
2469
  showMaskOnHover: false
2457
2470
  },
2458
- [InputMaskEnum.CNPJ]: {
2459
- mask: ['99.999.999/9999-99'],
2460
- showMaskOnHover: false
2461
- },
2471
+ [InputMaskEnum.CNPJ]: cnpjMaskParameters,
2462
2472
  [InputMaskEnum.CUIT]: {
2463
2473
  mask: ['99-99999999-9'],
2464
2474
  clearMaskOnLostFocus: false
@@ -2468,14 +2478,16 @@ class NgtInputComponent extends NgtBaseNgModel {
2468
2478
  clearMaskOnLostFocus: false
2469
2479
  },
2470
2480
  [InputMaskEnum.CPF_CNPJ_RUT]: {
2471
- mask: ['999.999.999-99', '999999999999', '99.999.999/9999-99'],
2472
- keepStatic: true,
2473
- showMaskOnHover: false
2481
+ mask: ['999.999.999-99', '999999999999', cnpjMaskPattern],
2482
+ definitions: cnpjMaskParameters.definitions,
2483
+ showMaskOnHover: false,
2484
+ onBeforePaste: onBeforeCnpjPaste
2474
2485
  },
2475
2486
  [InputMaskEnum.CPF_CNPJ]: {
2476
- mask: ['999.999.999-99', '99.999.999/9999-99'],
2477
- keepStatic: true,
2478
- showMaskOnHover: false
2487
+ mask: ['999.999.999-99', cnpjMaskPattern],
2488
+ definitions: cnpjMaskParameters.definitions,
2489
+ showMaskOnHover: false,
2490
+ onBeforePaste: onBeforeCnpjPaste
2479
2491
  },
2480
2492
  [InputMaskEnum.DECIMAL]: {
2481
2493
  digits: this.decimalMaskPrecision,
@@ -2527,6 +2539,39 @@ class NgtInputComponent extends NgtBaseNgModel {
2527
2539
  else {
2528
2540
  applyInputMask(this.element.nativeElement, masks[this.mask]);
2529
2541
  }
2542
+ if (this.mask == InputMaskEnum.CPF_CNPJ || this.mask == InputMaskEnum.CPF_CNPJ_RUT) {
2543
+ this.setupAlphanumericPasteHandler();
2544
+ }
2545
+ }
2546
+ setupAlphanumericPasteHandler() {
2547
+ const handler = (event) => {
2548
+ const pastedText = event.clipboardData?.getData('text/plain') ?? '';
2549
+ const normalized = pastedText.toUpperCase().replace(/[^A-Z0-9]/g, '');
2550
+ if (normalized.length !== 14 || !/[A-Z]/.test(normalized)) {
2551
+ return;
2552
+ }
2553
+ event.preventDefault();
2554
+ event.stopImmediatePropagation();
2555
+ const formatted = `${normalized.substring(0, 2)}.${normalized.substring(2, 5)}.${normalized.substring(5, 8)}/${normalized.substring(8, 12)}-${normalized.substring(12, 14)}`;
2556
+ const input = this.element.nativeElement;
2557
+ const inputmaskInstance = input.inputmask;
2558
+ const alphaMaskOptions = {
2559
+ mask: ['XX.XXX.XXX/XXXX-99'],
2560
+ definitions: { X: { validator: '[0-9A-Za-z]', casing: 'upper' } },
2561
+ showMaskOnHover: false
2562
+ };
2563
+ if (inputmaskInstance && typeof inputmaskInstance.option === 'function') {
2564
+ inputmaskInstance.option(alphaMaskOptions);
2565
+ }
2566
+ else {
2567
+ removeInputMask(input);
2568
+ Inputmask(alphaMaskOptions).mask(input);
2569
+ }
2570
+ input.value = formatted;
2571
+ input.dispatchEvent(new Event('input', { bubbles: true }));
2572
+ input.dispatchEvent(new Event('change', { bubbles: true }));
2573
+ };
2574
+ this.element.nativeElement.addEventListener('paste', handler, true);
2530
2575
  }
2531
2576
  setupProperties() {
2532
2577
  let props = {
@@ -2603,19 +2648,20 @@ class NgtInputComponent extends NgtBaseNgModel {
2603
2648
  if (!control.value) {
2604
2649
  return null;
2605
2650
  }
2606
- if (control.value && control.value.length <= 11) {
2607
- if (this.validatorCPF(control.value)) {
2651
+ const normalizedDocument = this.removeMasks(control.value);
2652
+ if (normalizedDocument && normalizedDocument.length <= 11) {
2653
+ if (this.validatorCPF(normalizedDocument)) {
2608
2654
  return null;
2609
2655
  }
2610
2656
  else {
2611
2657
  return { 'cpf': true };
2612
2658
  }
2613
2659
  }
2614
- else if (control.value && control.value.length == 12) {
2660
+ else if (normalizedDocument && normalizedDocument.length == 12) {
2615
2661
  return null;
2616
2662
  }
2617
2663
  else {
2618
- if (control.value && this.validatorCNPJ(control.value)) {
2664
+ if (normalizedDocument && this.validatorCNPJ(normalizedDocument)) {
2619
2665
  return null;
2620
2666
  }
2621
2667
  else {
@@ -2735,7 +2781,16 @@ class NgtInputComponent extends NgtBaseNgModel {
2735
2781
  };
2736
2782
  }
2737
2783
  validatorCNPJ(value) {
2738
- let b = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
2784
+ const normalizedValue = this.normalizeDocument(value);
2785
+ if (normalizedValue.length != 14) {
2786
+ return false;
2787
+ }
2788
+ return /[A-Z]/.test(normalizedValue)
2789
+ ? this.validatorAlphaNumericCNPJ(normalizedValue)
2790
+ : this.validatorNumericCNPJ(normalizedValue);
2791
+ }
2792
+ validatorNumericCNPJ(value) {
2793
+ const b = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
2739
2794
  if ((value = value.replace(/[^\d]/g, "")).length != 14) {
2740
2795
  return false;
2741
2796
  }
@@ -2743,22 +2798,46 @@ class NgtInputComponent extends NgtBaseNgModel {
2743
2798
  return false;
2744
2799
  }
2745
2800
  let n = 0;
2746
- for (let i = 0; i < 12; n += value[i] * b[++i]) {
2747
- ;
2801
+ for (let i = 0; i < 12; i++) {
2802
+ n += Number(value[i]) * b[i + 1];
2748
2803
  }
2749
- if (value[12] != (((n %= 11) < 2) ? 0 : 11 - n)) {
2804
+ if (Number(value[12]) != (((n %= 11) < 2) ? 0 : 11 - n)) {
2750
2805
  return false;
2751
2806
  }
2752
2807
  n = 0;
2753
- for (let i = 0; i <= 12; n += value[i] * b[i++]) {
2754
- ;
2808
+ for (let i = 0; i <= 12; i++) {
2809
+ n += Number(value[i]) * b[i];
2755
2810
  }
2756
- if (value[13] != (((n %= 11) < 2) ? 0 : 11 - n)) {
2811
+ if (Number(value[13]) != (((n %= 11) < 2) ? 0 : 11 - n)) {
2757
2812
  return false;
2758
2813
  }
2759
2814
  return true;
2760
2815
  }
2761
- ;
2816
+ validatorAlphaNumericCNPJ(value) {
2817
+ if (!/^[A-Z0-9]{12}[0-9]{2}$/.test(value)) {
2818
+ return false;
2819
+ }
2820
+ const firstWeights = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
2821
+ const firstDigit = this.getCnpjVerificationDigit(value.substring(0, 12), firstWeights);
2822
+ if (firstDigit !== Number(value[12])) {
2823
+ return false;
2824
+ }
2825
+ const secondWeights = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
2826
+ const secondDigit = this.getCnpjVerificationDigit(`${value.substring(0, 12)}${firstDigit}`, secondWeights);
2827
+ return secondDigit === Number(value[13]);
2828
+ }
2829
+ getCnpjVerificationDigit(value, weights) {
2830
+ const weightedTotal = value
2831
+ .split('')
2832
+ .reduce((total, currentValue, index) => total + (this.getCnpjCharacterValue(currentValue) * weights[index]), 0);
2833
+ const modulo = weightedTotal % 11;
2834
+ return modulo < 2 ? 0 : 11 - modulo;
2835
+ }
2836
+ getCnpjCharacterValue(value) {
2837
+ return /^[0-9]$/.test(value)
2838
+ ? Number(value)
2839
+ : value.charCodeAt(0) - 48;
2840
+ }
2762
2841
  validatorCPF(value) {
2763
2842
  let numeros, digitos, soma, i, resultado, digitos_iguais;
2764
2843
  digitos_iguais = 1;
@@ -2810,8 +2889,10 @@ class NgtInputComponent extends NgtBaseNgModel {
2810
2889
  .replace(',', '.');
2811
2890
  }
2812
2891
  else if (this.mask == "cnpj-cpf" || this.mask == "cpf" || this.mask == "cnpj" || this.mask == "cnpj-cpf-rut" || this.mask == "cuit") {
2813
- value = (value + "")
2814
- .replace(/[^\d]/g, '');
2892
+ value = this.normalizeDocument(value);
2893
+ if (this.mask == "cpf" || this.mask == "cuit") {
2894
+ value = value.replace(/[^\d]/g, '');
2895
+ }
2815
2896
  }
2816
2897
  else if (this.mask == InputMaskEnum.CELLPHONE
2817
2898
  || this.mask == InputMaskEnum.INTERNATIONAL_PHONE) {
@@ -2825,6 +2906,12 @@ class NgtInputComponent extends NgtBaseNgModel {
2825
2906
  }
2826
2907
  return value;
2827
2908
  }
2909
+ normalizeDocument(value) {
2910
+ return (value || '')
2911
+ .toString()
2912
+ .toUpperCase()
2913
+ .replace(/[^A-Z0-9]/g, '');
2914
+ }
2828
2915
  hasEmailServiceValidation() {
2829
2916
  return typeof this.ngtValidationService?.emailValidation === 'function';
2830
2917
  }