This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07ce265
commit ae090c4
Showing
5 changed files
with
105 additions
and
21 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
14 changes: 9 additions & 5 deletions
14
src/core/components/inputs/text-input/text-input.component.html
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,8 +1,12 @@ | ||
<label for="inputElement">{{label}}</label> | ||
<input id="inputElement" type="text" | ||
[placeholder]="placeholder" | ||
(keydown.enter)="onEnter.emit()" | ||
[class]="flavor" | ||
[disabled]="disabled"> | ||
[placeholder]="placeholder" | ||
(keydown.enter)="onEnter.emit()" | ||
[class]="flavor" | ||
[disabled]="disabled" | ||
(input)="onInput($event.target)" | ||
(blur)="onTouched?.()" | ||
(compositionstart)="compositionStart()" | ||
(compositionend)="compositionEnd($event.target)"> | ||
<small [className]="getLabelColor()">{{hintText}}</small> | ||
|
||
<ng-content></ng-content> |
76 changes: 71 additions & 5 deletions
76
src/core/components/inputs/text-input/text-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 |
---|---|---|
@@ -1,30 +1,96 @@ | ||
import {Component, EventEmitter, Input, Output} from '@angular/core'; | ||
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core'; | ||
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from "@angular/forms"; | ||
import {ɵgetDOM as getDOM} from '@angular/common'; | ||
|
||
export type Flavor = 'primary' | 'success' | 'warning' | 'danger' | 'info'; | ||
|
||
/** | ||
* We must check whether the agent is Android because composition events | ||
* behave differently between iOS and Android. | ||
*/ | ||
function isAndroid(): boolean { | ||
const userAgent = getDOM() ? getDOM().getUserAgent() : ''; | ||
return /android (\d+)/.test(userAgent.toLowerCase()); | ||
} | ||
|
||
@Component({ | ||
selector: 'design-text-input', | ||
templateUrl: './text-input.component.html', | ||
styleUrls: ['./text-input.component.scss'] | ||
styleUrls: ['./text-input.component.scss'], | ||
providers: [ | ||
{ | ||
provide: NG_VALUE_ACCESSOR, | ||
multi: true, | ||
useExisting: TextInputComponent | ||
} | ||
] | ||
}) | ||
export class TextInputComponent { | ||
export class TextInputComponent implements ControlValueAccessor { | ||
|
||
@Input() public disabled = false; | ||
public disabled = false; | ||
@Input() placeholder: string = ""; | ||
@Input() hintText: string = ""; | ||
@Input() label: string = ""; | ||
@Input() hintColored: boolean = false; | ||
@Input() public flavor: Flavor = 'primary'; | ||
@ViewChild(HTMLInputElement) private input?: HTMLInputElement; | ||
|
||
// eslint-disable-next-line @angular-eslint/no-output-on-prefix | ||
@Output() public onEnter = new EventEmitter<InputEvent>(); | ||
onChanged: ((_: any) => void) | null = null; | ||
onTouched: (() => void) | null = null; | ||
composing: boolean = false; | ||
|
||
private readonly compositionMode: boolean | null; | ||
|
||
constructor() { | ||
if (this.compositionMode === null) { | ||
this.compositionMode = !isAndroid(); | ||
} | ||
} | ||
|
||
writeValue(obj: any): void { | ||
if(this.input) { | ||
this.input.value = obj | ||
} | ||
} | ||
|
||
registerOnChange(fn: (_: any) => void): void { | ||
this.onChanged = fn; | ||
} | ||
|
||
registerOnTouched(fn: () => void): void { | ||
this.onTouched = fn; | ||
} | ||
|
||
setDisabledState(isDisabled: boolean) { | ||
this.disabled = isDisabled; | ||
} | ||
|
||
getLabelColor() { | ||
if(this.hintColored && !this.disabled && this.flavor != "primary"){ | ||
if (this.hintColored && !this.disabled && this.flavor != "primary") { | ||
return this.flavor; | ||
} else { | ||
return ""; | ||
} | ||
} | ||
|
||
onInput(target: EventTarget | null) { | ||
if (!this.compositionMode || (this.compositionMode && !this.composing)) { | ||
// @ts-ignore | ||
this.onChanged?.(target.value) | ||
} | ||
} | ||
|
||
compositionStart() { | ||
this.composing = true; | ||
} | ||
|
||
compositionEnd(target: EventTarget | null) { | ||
this.composing = false; | ||
if (this.compositionMode) { | ||
// @ts-ignore | ||
this.onChanged?.(target.value); | ||
} | ||
} | ||
} |