-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpassword-toggle.ts
More file actions
175 lines (143 loc) · 4.78 KB
/
password-toggle.ts
File metadata and controls
175 lines (143 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { FocusMonitor } from '@angular/cdk/a11y';
import {
AfterContentInit,
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
inject,
Input,
numberAttribute,
OnDestroy,
TemplateRef,
ViewChild,
ViewEncapsulation
} from '@angular/core';
import { KBQ_FORM_FIELD_REF, kbqInjectNativeElement, PopUpTriggers } from '@koobiq/components/core';
import { KbqIconButton, KbqIconModule } from '@koobiq/components/icon';
import { KbqToolTipModule, KbqTooltipTrigger } from '@koobiq/components/tooltip';
import { KbqFormField } from './form-field';
import { KbqFormFieldControl } from './form-field-control';
// @TODO Temporary solution to resolve circular dependency (#DS-3893)
type KbqInputPassword = KbqFormFieldControl<unknown> & {
elementType: string;
toggleType: () => void;
};
// @TODO Temporary solution to resolve circular dependency (#DS-3893)
const isInputPassword = (control: KbqFormFieldControl<unknown>): control is KbqInputPassword => {
return 'elementType' in control;
};
const getKbqPasswordToggleMissingControlError = (): Error => {
return Error('kbq-password-toggle should use with kbqInputPassword');
};
/** Component which changes password visibility. */
@Component({
selector: `kbq-password-toggle`,
imports: [KbqIconModule, KbqToolTipModule],
template: `
<ng-content />
`,
styleUrls: ['password-toggle.scss', '../icon/icon-button.scss', '../icon/icon-button-tokens.scss'],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
exportAs: 'kbqPasswordToggle',
host: {
class: 'kbq-password-toggle kbq kbq-icon-button kbq-contrast-fade',
'[class.kbq-error]': 'hasError',
'[class.kbq-eye_16]': 'hidden',
'[class.kbq-eye-slash_16]': '!hidden',
// legacy style for backward compatibility
'[style.visibility]': 'visibility',
'[class.cdk-visually-hidden]': 'visibility === "hidden"',
'[attr.aria-hidden]': 'visibility === "hidden"',
'[attr.tabindex]': 'tabindex',
'(click)': 'toggle($event)',
'(keydown.ENTER)': 'toggle($event)',
'(keydown.SPACE)': 'toggle($event)'
}
})
export class KbqPasswordToggle extends KbqTooltipTrigger implements AfterViewInit, OnDestroy, AfterContentInit {
protected readonly nativeElement = kbqInjectNativeElement();
protected readonly focusMonitor = inject(FocusMonitor);
protected readonly changeDetectorRef = inject(ChangeDetectorRef);
// @TODO fix types (#DS-2915)
private readonly formField = inject(KBQ_FORM_FIELD_REF, { optional: true }) as unknown as KbqFormField | undefined;
@Input({ transform: numberAttribute }) tabindex: number = 0;
/**
* @docs-private
*/
@ViewChild(KbqIconButton) readonly icon: KbqIconButton;
@Input('kbqTooltipNotHidden')
get content(): string | TemplateRef<any> {
return this.control.elementType === 'password' ? this.kbqTooltipHidden : this._content;
}
set content(content: string | TemplateRef<any>) {
this._content = content;
this.updateData();
}
@Input() kbqTooltipHidden: string | TemplateRef<any>;
protected hasError: boolean = false;
/** Form field password control. */
private get control(): KbqInputPassword {
const control = this.formField?.control;
if (!control || !isInputPassword(control)) {
throw getKbqPasswordToggleMissingControlError();
}
return control;
}
/**
* @docs-private
*/
get hidden(): boolean {
return this.control.elementType === 'password';
}
/**
* @docs-private
*/
get iconClass(): string {
return this.hidden ? 'kbq-eye_16' : 'kbq-eye-slash_16';
}
/**
* @docs-private
*/
get visibility(): 'hidden' | 'visible' {
return this.control.disabled ? 'hidden' : 'visible';
}
constructor() {
super();
this.trigger = `${PopUpTriggers.Hover}`;
}
/**
* @docs-private
*/
ngAfterContentInit(): void {
this.formField?.control?.stateChanges.subscribe(this.updateState);
this.updateState();
}
/**
* @docs-private
*/
ngAfterViewInit(): void {
this.focusMonitor.monitor(this.nativeElement, true);
}
/**
* @docs-private
*/
ngOnDestroy() {
this.focusMonitor.stopMonitoring(this.nativeElement);
}
/**
* @docs-private
*/
toggle(event: KeyboardEvent) {
this.hide();
const input = this.control;
input.toggleType();
this.updateData();
event.preventDefault();
}
private updateState = () => {
this.hasError = !!this.formField?.control?.errorState;
this.changeDetectorRef.markForCheck();
};
}