-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove CVA and inject NgControl instead
- Loading branch information
Showing
6 changed files
with
86 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
projects/ngx-english-only-app/src/app/my-input.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Component, forwardRef, OnInit } from '@angular/core'; | ||
import { NG_VALUE_ACCESSOR, ControlValueAccessor, FormGroup, FormControl, NG_VALIDATORS, Validator, AbstractControl, ValidationErrors } from '@angular/forms'; | ||
|
||
@Component({ | ||
selector: 'my-input', | ||
template:` | ||
<ng-container [formGroup]="basicInfoForm"> | ||
<label for="Full Name"> Full Name </label> | ||
<div> | ||
<textarea rows="3" formControlName="fname" englishOnly></textarea> | ||
</div> | ||
<label for="Email"> Email </label> | ||
<div> | ||
<input type="email" formControlName="email"> | ||
</div> | ||
</ng-container>`, | ||
providers: [ | ||
{ | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: forwardRef(() => MyInputComponent ), | ||
multi: true | ||
}, | ||
{ | ||
provide: NG_VALIDATORS, | ||
useExisting: forwardRef(() => MyInputComponent), | ||
multi: true | ||
} | ||
] | ||
}) | ||
|
||
export class MyInputComponent implements OnInit, ControlValueAccessor, Validator { | ||
public basicInfoForm: FormGroup = new FormGroup( | ||
{ | ||
fname: new FormControl(""), | ||
email: new FormControl("") | ||
}); | ||
constructor() { } | ||
|
||
ngOnInit() { | ||
} | ||
|
||
public onTouched: () => void = () => {}; | ||
|
||
writeValue(val: any): void { | ||
val && this.basicInfoForm.setValue(val, { emitEvent: false }); | ||
} | ||
registerOnChange(fn: any): void { | ||
this.basicInfoForm.valueChanges.subscribe(fn); | ||
} | ||
registerOnTouched(fn: any): void { | ||
this.onTouched = fn; | ||
} | ||
setDisabledState?(isDisabled: boolean): void { | ||
isDisabled ? this.basicInfoForm.disable() : this.basicInfoForm.enable(); | ||
} | ||
|
||
validate(c: AbstractControl): ValidationErrors | null{ | ||
return this.basicInfoForm.valid ? null : { invalidForm: {valid: false, message: "basicInfoForm fields are invalid"}}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 9 additions & 56 deletions
65
projects/ngx-english-only/src/lib/directives/ngx-english-only/ngx-english-only.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,32 @@ | ||
import { Directive, ElementRef, forwardRef, HostListener, Renderer2 } from '@angular/core'; | ||
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; | ||
import { Directive, HostListener } from '@angular/core'; | ||
import { NgControl } from '@angular/forms'; | ||
|
||
@Directive({ | ||
selector: '[englishOnly]', | ||
providers: [ | ||
{ | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: forwardRef(() => NgxEnglishOnlyDirective), | ||
multi: true | ||
} | ||
], | ||
selector: '[englishOnly]' | ||
}) | ||
export class NgxEnglishOnlyDirective implements ControlValueAccessor { | ||
private isDisabled = false; | ||
export class NgxEnglishOnlyDirective { | ||
private readonly allowSpecialKeys: Array<string> = ['Backspace', 'Tab', 'End', 'Home', '-', 'ArrowRight', 'ArrowLeft']; | ||
|
||
private readonly REGEX_EN: RegExp = /^[a-zA-Z0-9.\s?><;:",{}()[\]\-_+=!@#$%\^’‘&*|'/\\]*$/g; | ||
|
||
onChangeFn: (value: any) => void = () => { | ||
}; | ||
|
||
onTouchedFn = () => { | ||
}; | ||
|
||
constructor(private elm: ElementRef, private renderer: Renderer2) { } | ||
constructor(private control: NgControl) { } | ||
|
||
@HostListener('keydown', ['$event']) | ||
onKeyDown(event: KeyboardEvent) { | ||
if (this.allowSpecialKeys.indexOf(event.key) !== -1) { | ||
return; | ||
} | ||
|
||
const current: string = this.elm.nativeElement.value; | ||
const current: string = this.control.control?.value; | ||
const next: string = current.concat(event.key); | ||
|
||
if (next && !String(next).match(this.REGEX_EN)) { | ||
event.preventDefault(); | ||
} | ||
} | ||
|
||
@HostListener('blur', ['$event.target.value']) | ||
onBlur(value: string) { | ||
const replaced = Array.from(value).filter(v => v.match(this.REGEX_EN)).join(''); | ||
this.elm.nativeElement.value = replaced; | ||
this.onChangeFn(replaced); | ||
this.onTouchedFn(); | ||
} | ||
|
||
@HostListener('paste', ['$event']) | ||
onPaste(event: ClipboardEvent) { | ||
const clipboardData: DataTransfer | null = event.clipboardData; | ||
const pastedDate: string = clipboardData?.getData('Text') || ''; | ||
const current: string = this.elm.nativeElement.value; | ||
const replaced = Array.from(current?.concat(pastedDate)).filter(v => v.match(this.REGEX_EN)).join(''); | ||
this.onChangeFn(replaced); | ||
} | ||
|
||
writeValue(value: string): void { | ||
this.elm.nativeElement.value = value; | ||
} | ||
|
||
registerOnChange(fn: any): void { | ||
this.onChangeFn = fn; | ||
} | ||
|
||
registerOnTouched(fn: () => void): void { | ||
this.onTouchedFn = fn; | ||
} | ||
|
||
setDisabledState(isDisabled: boolean): void { | ||
this.isDisabled = isDisabled; | ||
if (this.isDisabled) { | ||
this.renderer.setProperty(this.elm.nativeElement, 'disabled', 'disabled'); | ||
} else { | ||
this.renderer.removeAttribute(this.elm.nativeElement, 'disabled'); | ||
} | ||
@HostListener('input', ['$event']) | ||
onInputChange() { | ||
this.control.control?.setValue(Array.from(this.control.control?.value as string || '').filter(v => v.match(this.REGEX_EN)).join('')); | ||
} | ||
} |