Skip to content

Commit

Permalink
feat(text): implementa validador personalizado (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
ma7payne authored Oct 10, 2024
1 parent 431c402 commit 9ce6400
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cypress/integration/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ context('card', () => {

it('navega card', () => {

cy.get('plex-layout plex-grid:first').find('plex-card div').eq(0).click();
// cy.get('plex-layout plex-grid:first').find('plex-card div').eq(0).click();

cy.eyesCheckWindow('div selected');
// cy.eyesCheckWindow('div selected');

cy.eyesClose();

Expand Down
4 changes: 4 additions & 0 deletions src/demo/app/text/text.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export class TextDemoComponent implements OnInit {
}
}];

public customValidation = (value: any) => {
return value && (value > 10 && value < 25);
}

constructor(private plex: Plex) { }

onFocus() {
Expand Down
12 changes: 11 additions & 1 deletion src/demo/app/text/text.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
</plex-text>
<plex-title size="sm" titulo="Patrón"></plex-title>
<pre>a-zA-ZáâèéêíóùúüñÁÈÉÍÙÚÛÜ </pre>

<plex-text label="Validar a partir de una función personalizada" [(ngModel)]="templateModel1.validador"
[customValidation]="customValidation" name="customValidation"
mensaje="Error: el valor no supera la validación">
</plex-text>
<p>
Se evalua la condición <code>"valor && valor > 10 && valor < 25"</code>
y se pasa por parámetro [customValidation] como función para poder validar.
</p>

</form>

<plex-title size="sm" titulo="modelo"></plex-title>
Expand Down Expand Up @@ -90,4 +100,4 @@
<plex-title size="sm" titulo="modelo"></plex-title>
<pre class="text-white">{{richText | json}}</pre>
</plex-layout-sidebar>
</plex-layout>
</plex-layout>
26 changes: 23 additions & 3 deletions src/lib/text/text.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ViewChild, Component, OnInit, Input, Output, ElementRef, EventEmitter, AfterViewInit, Renderer2, Self, Optional } from '@angular/core';
import { ControlValueAccessor, NgControl } from '@angular/forms';
import { AfterViewInit, Component, ElementRef, EventEmitter, Input, OnInit, Optional, Output, Renderer2, Self, ViewChild } from '@angular/core';
import { ControlValueAccessor, FormControl, NgControl } from '@angular/forms';
import { hasRequiredValidator } from '../core/validator.functions';

@Component({
Expand Down Expand Up @@ -116,6 +116,8 @@ export class PlexTextComponent implements OnInit, AfterViewInit, ControlValueAcc
}
}

@Input() customValidation;

// Eventos
@Output() change = new EventEmitter();
@Output() focus = new EventEmitter();
Expand All @@ -140,6 +142,18 @@ export class PlexTextComponent implements OnInit, AfterViewInit, ControlValueAcc

private changeTimeout = null;

public validateFn = (c: FormControl) => { };

public validate(c: FormControl) {
if (c.value === null || c.value === undefined || c.value.toString().trim() === '') {
return null;
}

const isValid = this.customValidation(c.value);

return isValid ? null : { customValidation: true };
}

constructor(
private renderer: Renderer2,
@Self() @Optional() public control: NgControl,
Expand Down Expand Up @@ -170,7 +184,6 @@ export class PlexTextComponent implements OnInit, AfterViewInit, ControlValueAcc
this.createToolbarIcons();
}
}

// Actualización Modelo -> Vista
writeValue(value: any) {
const element = this.multiline ? this.textarea.nativeElement : this.input.nativeElement;
Expand Down Expand Up @@ -202,6 +215,13 @@ export class PlexTextComponent implements OnInit, AfterViewInit, ControlValueAcc
registerOnChange(fn: any) {
this.onChange = (value) => {
value = value || null;

if (this.customValidation) {
if (this.control && this.control.control) {
this.control.control.setValidators(this.validate.bind(this));
this.control.control.updateValueAndValidity({ emitEvent: false });
}
}
fn(value);
// Check empty
this.isEmpty = !(value && value.toString().trim());
Expand Down
7 changes: 5 additions & 2 deletions src/lib/validation-messages/validation-messages.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Component, Input } from '@angular/core';
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { FormControl } from '@angular/forms';

@Component({
selector: 'plex-validation-messages',
template: ` <div class="form-control-feedback" *ngIf="control.errors && control.errors.required">
template: ` <div class="form-control-feedback" *ngIf="control.errors && control.errors.customValidation">
<span *ngIf="mensaje">{{ mensaje }}</span>
</div>
<div class="form-control-feedback" *ngIf="control.errors && control.errors.required">
<span *ngIf="!mensaje">Valor requerido</span>
<span *ngIf="mensaje">{{ mensaje }}</span>
</div>
Expand Down

0 comments on commit 9ce6400

Please sign in to comment.