Guest User

Untitled

a guest
Jul 17th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. <div fxLayout='row' fxLayoutAlign="end center" class="app-password">
  2. <input fxFlex kendoTextBox
  3. [ngClass]="{ 'ng-dirty': isDirty }"
  4. [(value)]="currentValue"
  5. [attr.type]="type"
  6. [disabled]="isDisabled"
  7. (touched) ="onTouched($event)"
  8. (input)="onChange($event)"
  9. (blur)="onBlur()"
  10. (focus)="onFocus()"
  11. /><span [ngClass]="{'k-icon': true, 'k-i-unlock': isShowPassword , 'k-i-lock': !isShowPassword }"
  12. (mouseup)="onMouseUp()"
  13. (mousedown)="onMouseDown()"
  14. ></span>
  15. </div>
  16.  
  17. import { Component, forwardRef, OnInit, Input } from '@angular/core';
  18. import { ControlValueAccessor, FormControl, NG_VALUE_ACCESSOR } from '@angular/forms';
  19.  
  20. @Component({
  21. selector: 'app-custom-control',
  22. templateUrl: './CustomControl.component.html',
  23. styleUrls: ['./CustomControl.component.scss'],
  24. providers: [
  25. {
  26. provide: NG_VALUE_ACCESSOR,
  27. useExisting: forwardRef(() => PasswordComponent),
  28. useValue: (c: FormControl) => PasswordComponent,
  29. multi: true
  30. }
  31. ]
  32. })
  33. export class CustomControl implements ControlValueAccessor, OnInit {
  34. public isDisabled: boolean;
  35. public isDirty = false;
  36. public type = 'password';
  37. public isShowPassword = false;
  38. public currentValue: string = null;
  39. // Events change and touched
  40. private propagateChange = (_: any) => { };
  41. private propagateTouched = () => { };
  42.  
  43. constructor() { }
  44.  
  45. public ngOnInit() { }
  46.  
  47. public writeValue(password: string): void {
  48. if (!password || typeof password !== 'string') {
  49. return;
  50. }
  51. this.currentValue = password;
  52. this.propagateTouched();
  53. this.propagateChange(this.currentValue);
  54. }
  55.  
  56. public registerOnChange(fn: any): void {
  57. console.log(fn);
  58. this.propagateChange = fn;
  59. }
  60.  
  61. public registerOnTouched(fn: any): void {
  62. this.propagateTouched = fn;
  63. }
  64.  
  65. public setDisabledState?(isDisabled: boolean): void {
  66. this.isDisabled = isDisabled;
  67. }
  68.  
  69. public get value(): string {
  70. return this.currentValue;
  71. }
  72.  
  73. public onTouched(event) {
  74. this.propagateTouched();
  75. }
  76.  
  77. public onShow() {
  78. if (this.type === 'password') {
  79. this.type = 'text';
  80. } else {
  81. this.type = 'password';
  82. }
  83. }
  84.  
  85. public onMouseUp() {
  86. this.type = 'password';
  87. this.isShowPassword = false;
  88. }
  89.  
  90. public onMouseDown() {
  91. this.type = 'text';
  92. this.isShowPassword = true;
  93. }
  94.  
  95. public onChange(event) {
  96. this.writeValue(event.target.value);
  97. }
  98.  
  99.  
  100. public onBlur() {
  101.  
  102. }
  103.  
  104. public onFocus() {
  105. this.isDirty = true;
  106. this.propagateTouched();
  107. }
  108.  
  109. }
  110.  
  111. import { FormControl, FormGroup, Validators } from '@angular/forms';
  112. .....
  113. const passwordControl = new FormControl(null, [Validators.requirement, Validators.max(3)])
  114.  
  115. <app-custom-control [formControlName]="'password'" ></app-custom-control>
Add Comment
Please, Sign In to add comment