@tekus/kiosks-design-system 3.10.0 → 3.11.0

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.
@@ -2932,13 +2932,24 @@ class TkKioskTextFieldComponent {
2932
2932
  this.focusOut = new EventEmitter();
2933
2933
  this.cursorPositionChange = new EventEmitter();
2934
2934
  this._statusFocus = 'iddle';
2935
+ this.VALIDATION_PATTERNS = {
2936
+ ID_NUMBER: /^[a-zA-Z0-9]+$/,
2937
+ PHONE_NUMBER: /^[\d\s+()-]+$/,
2938
+ EMAIL: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,}(\.[a-zA-Z]{2,})?$/,
2939
+ AGE: /^\d+$/,
2940
+ };
2941
+ this.KEYBOARD_TYPE_MAP = {
2942
+ email: keyboardEmailType,
2943
+ 'only-text': keyboardTypeText,
2944
+ 'text-number': keyboardTypeTextNumbers,
2945
+ };
2935
2946
  this.numericTypes = ['number', 'tel'];
2936
2947
  this.keyboardType = keyboardType;
2937
2948
  this.cursorPosition = 0;
2938
2949
  }
2939
2950
  set statusFocus(value) {
2940
2951
  this._statusFocus = value;
2941
- const control = this.formGroup?.get(this.controlName);
2952
+ const control = this.getControl();
2942
2953
  if (control) {
2943
2954
  value === 'desactive' ? control.disable() : control.enable();
2944
2955
  }
@@ -2946,75 +2957,80 @@ class TkKioskTextFieldComponent {
2946
2957
  get statusFocus() {
2947
2958
  return this._statusFocus;
2948
2959
  }
2960
+ getControl() {
2961
+ return this.formGroup?.get(this.controlName);
2962
+ }
2949
2963
  get showError() {
2950
- const control = this.formGroup?.get(this.controlName);
2951
- return !!(control?.invalid && control?.touched);
2964
+ const control = this.getControl();
2965
+ return !!(control?.invalid && (control?.touched || control?.dirty));
2952
2966
  }
2953
2967
  get displayErrorMessage() {
2954
2968
  if (!this.showError)
2955
2969
  return '';
2956
- if (this.errorMessage)
2957
- return this.errorMessage;
2958
- const control = this.formGroup?.get(this.controlName);
2970
+ const control = this.getControl();
2959
2971
  if (!control?.errors)
2960
2972
  return '';
2961
- if (control.errors['required'])
2973
+ if (this.isEmptyAndRequired(control))
2962
2974
  return 'Este campo es requerido';
2963
- if (control.errors['email'])
2975
+ if (this.errorMessage)
2976
+ return this.errorMessage;
2977
+ return this.getDefaultErrorMessage(control);
2978
+ }
2979
+ isEmptyAndRequired(control) {
2980
+ const isEmpty = !control.value || control.value.toString().trim() === '';
2981
+ return isEmpty && !!control.errors?.['required'];
2982
+ }
2983
+ getDefaultErrorMessage(control) {
2984
+ const errors = control.errors;
2985
+ if (errors?.['email'])
2964
2986
  return 'Formato de correo inválido';
2965
- if (control.errors['pattern'])
2987
+ if (errors?.['pattern'])
2966
2988
  return 'Formato inválido';
2967
- if (control.errors['minlength'])
2968
- return `Mínimo ${control.errors['minlength'].requiredLength} caracteres`;
2969
- if (control.errors['maxlength'])
2970
- return `Máximo ${control.errors['maxlength'].requiredLength} caracteres`;
2971
- if (control.errors['min'])
2972
- return `Valor mínimo es ${control.errors['min'].min}`;
2973
- if (control.errors['max'])
2974
- return `Valor máximo es ${control.errors['max'].max}`;
2989
+ if (errors?.['minlength'])
2990
+ return `Mínimo ${errors['minlength'].requiredLength} caracteres`;
2991
+ if (errors?.['maxlength'])
2992
+ return `Máximo ${errors['maxlength'].requiredLength} caracteres`;
2993
+ if (errors?.['min'])
2994
+ return `Valor mínimo es ${errors['min'].min}`;
2995
+ if (errors?.['max'])
2996
+ return `Valor máximo es ${errors['max'].max}`;
2975
2997
  return 'Valor inválido';
2976
2998
  }
2977
2999
  ngOnInit() {
3000
+ this.initializeKeyboardType();
3001
+ this.applyValidations();
3002
+ }
3003
+ initializeKeyboardType() {
2978
3004
  this.keyboardType = this.numericTypes.includes(this.type)
2979
3005
  ? keyboardTypeNumeric
2980
- : this.type === 'email'
2981
- ? keyboardEmailType
2982
- : this.type === 'only-text'
2983
- ? keyboardTypeText
2984
- : this.type === 'text-number'
2985
- ? keyboardTypeTextNumbers
2986
- : keyboardType;
2987
- this.applyValidations();
3006
+ : this.KEYBOARD_TYPE_MAP[this.type] || keyboardType;
2988
3007
  }
2989
3008
  applyValidations() {
2990
- const control = this.formGroup?.get(this.controlName);
3009
+ const control = this.getControl();
2991
3010
  if (!control || !this.formFieldValidationType)
2992
3011
  return;
2993
- const validators = [];
3012
+ const validators = this.buildValidators(control);
3013
+ control.setValidators(validators);
3014
+ control.updateValueAndValidity();
3015
+ }
3016
+ buildValidators(control) {
3017
+ const existingValidators = control.validator ? [control.validator] : [];
3018
+ const validators = [...existingValidators];
2994
3019
  switch (this.formFieldValidationType) {
2995
3020
  case FormFieldValidationType.ID_NUMBER:
2996
- validators.push(Validators.minLength(this.minLenght));
2997
- validators.push(Validators.maxLength(this.maxLenght));
2998
- validators.push(Validators.pattern(/^[a-zA-Z0-9]+$/));
3021
+ validators.push(Validators.minLength(this.minLenght), Validators.maxLength(this.maxLenght), Validators.pattern(this.VALIDATION_PATTERNS.ID_NUMBER));
2999
3022
  break;
3000
3023
  case FormFieldValidationType.PHONE_NUMBER:
3001
- validators.push(Validators.minLength(this.minLenght));
3002
- validators.push(Validators.maxLength(this.maxLenght));
3003
- validators.push(Validators.pattern(/^[\d\s+()-]+$/));
3024
+ validators.push(Validators.minLength(this.minLenght), Validators.maxLength(this.maxLenght), Validators.pattern(this.VALIDATION_PATTERNS.PHONE_NUMBER));
3004
3025
  break;
3005
3026
  case FormFieldValidationType.EMAIL:
3006
- validators.push(Validators.pattern(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,}(\.[a-zA-Z]{2,})?$/));
3027
+ validators.push(Validators.pattern(this.VALIDATION_PATTERNS.EMAIL));
3007
3028
  break;
3008
3029
  case FormFieldValidationType.AGE:
3009
- validators.push(Validators.minLength(this.minLenght));
3010
- validators.push(Validators.maxLength(this.maxLenght));
3011
- validators.push(Validators.pattern(/^\d+$/));
3012
- validators.push(Validators.min(1));
3013
- validators.push(Validators.max(100));
3030
+ validators.push(Validators.minLength(this.minLenght), Validators.maxLength(this.maxLenght), Validators.pattern(this.VALIDATION_PATTERNS.AGE), Validators.min(1), Validators.max(100));
3014
3031
  break;
3015
3032
  }
3016
- control.setValidators(validators);
3017
- control.updateValueAndValidity();
3033
+ return validators;
3018
3034
  }
3019
3035
  handleInputClick(event) {
3020
3036
  if (this.statusFocus === 'desactive')
@@ -3028,14 +3044,14 @@ class TkKioskTextFieldComponent {
3028
3044
  }
3029
3045
  changeValue(value) {
3030
3046
  if (value.length <= this.maxLenght) {
3031
- const control = this.formGroup?.controls[this.controlName];
3047
+ const control = this.getControl();
3032
3048
  control?.patchValue(value);
3033
3049
  control?.markAsTouched();
3034
3050
  control?.markAsDirty();
3035
3051
  }
3036
3052
  }
3037
3053
  closeKeyboard() {
3038
- const control = this.formGroup?.controls[this.controlName];
3054
+ const control = this.getControl();
3039
3055
  control?.markAsTouched();
3040
3056
  control?.markAsDirty();
3041
3057
  this.focusOut.emit();