]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-forms/input-text.component.ts
Handle input error in custom input text
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-forms / input-text.component.ts
1 import { Component, forwardRef, Input } from '@angular/core'
2 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
3 import { Notifier } from '@app/core'
4
5 @Component({
6 selector: 'my-input-text',
7 templateUrl: './input-text.component.html',
8 styleUrls: [ './input-text.component.scss' ],
9 providers: [
10 {
11 provide: NG_VALUE_ACCESSOR,
12 useExisting: forwardRef(() => InputTextComponent),
13 multi: true
14 }
15 ]
16 })
17 export class InputTextComponent implements ControlValueAccessor {
18 @Input() inputId = Math.random().toString(11).slice(2, 8) // id cannot be left empty or undefined
19 @Input() value = ''
20 @Input() autocomplete = 'off'
21 @Input() placeholder = ''
22 @Input() tabindex = 0
23 @Input() withToggle = true
24 @Input() withCopy = false
25 @Input() readonly = false
26 @Input() show = false
27 @Input() formError: string
28
29 constructor (private notifier: Notifier) { }
30
31 get inputType () {
32 return this.show
33 ? 'text'
34 : 'password'
35 }
36
37 get toggleTitle () {
38 return this.show
39 ? $localize`Hide`
40 : $localize`Show`
41 }
42
43 toggle () {
44 this.show = !this.show
45 }
46
47 activateCopiedMessage () {
48 this.notifier.success($localize`Copied`)
49 }
50
51 propagateChange = (_: any) => { /* empty */ }
52
53 writeValue (value: string) {
54 this.value = value
55 }
56
57 registerOnChange (fn: (_: any) => void) {
58 this.propagateChange = fn
59 }
60
61 registerOnTouched () {
62 // Unused
63 }
64
65 update () {
66 this.propagateChange(this.value)
67 }
68 }