Add static placeholder and hint in input

This commit is contained in:
Konstantinos Triantafyllou 2022-03-31 16:39:17 +03:00
parent da6f220ae0
commit 1951ae8cdd
2 changed files with 105 additions and 229 deletions

View File

@ -1,148 +0,0 @@
:host {
--input-placeholder-color: var(--placeholder-color);
--input-background: transparent;
--input-shadow: none;
--input-padding: 20px;
--input-border: none;
--input-color: var(--grey-color);
--input-color-disabled: var(--disable-color);
--input-background-disabled: var(--muted-color);
--input-danger-color: #BB121A;
}
/** Input wrapper*/
.input-wrapper {
box-shadow: var(--input-shadow);
border: var(--input-border);
background: var(--input-background);
border-radius: 6px;
position: relative;
padding: var(--input-padding) 0 var(--input-padding) var(--input-padding);
cursor: text;
color: var(--input-color);
}
.input-wrapper.danger {
border: 1px solid var(--input-danger-color);
}
.input-wrapper.select {
cursor: pointer;
}
.input-wrapper.disabled {
cursor: not-allowed !important;
user-select: none;
background-color: var(--input-background-disabled);
color: var(--input-color-disabled);
}
/** Placeholder */
.input-wrapper > .placeholder {
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
overflow: hidden;
pointer-events: none;
color: var(--input-placeholder-color);
padding: var(--input-padding);
}
.input-wrapper.danger > .placeholder {
color: var(--input-danger-color);
}
.input-wrapper.disabled > .placeholder {
color: var(--input-color-disabled);
}
.input-wrapper > .placeholder > label {
position: absolute;
font-size: 16px;
line-height: 24px;
top: calc(var(--input-padding) + 15px);
transform: translateY(-50%);
transition: all 0.5s ease 0s;
}
.input-wrapper.focused > .placeholder > label {
top: 15px;
font-size: 12px;
line-height: 18px;
}
/* Icon */
.input-wrapper.danger .icon {
color: var(--input-danger-color);
}
/** Input */
.input-wrapper .input {
outline: 0 none !important;
box-shadow: none;
border-radius: 0;
border: 0 none;
padding: 0 var(--input-padding) 0 0;
background: transparent !important;
color: inherit;
width: 100%;
min-height: 32px;
font-size: inherit;
line-height: inherit;
resize: none;
cursor: inherit;
display: flex;
flex-direction: column;
justify-content: center;
}
.input-wrapper .uk-grid .input {
min-height: 27px;
margin-top: 5px;
}
.input-wrapper .input:disabled {
background: transparent;
}
.input-wrapper:not(.focused) .input.search {
opacity: 0;
}
/* Tools */
.input-wrapper > .tools {
padding-right: var(--input-padding);
}
/* Options */
.uk-dropdown.options {
max-height: 200px;
overflow: auto;
}
/* Chips */
.input-wrapper .chip {
max-width: calc(100% - var(--input-padding));
margin-top: 5px;
}
.input-wrapper .chip .uk-label {
padding: 5px 20px;
}
/** Modifiers */
.inner {
--input-shadow: var(--shadow-inset);
--input-background: var(--default-color);
--input-options-background: transparent;
--input-options-border: none;
}
.flat {
--input-background: var(--light-color);
--input-border: 1px solid var(--muted-color);
--input-options-background: transparent;
--input-options-border: none;
}

View File

@ -25,6 +25,11 @@ export interface Option {
label: string label: string
} }
export interface Placeholder {
label: string,
static?: boolean
}
declare var UIkit; declare var UIkit;
/** /**
@ -34,71 +39,79 @@ declare var UIkit;
@Component({ @Component({
selector: '[dashboard-input], [input]', selector: '[dashboard-input], [input]',
template: ` template: `
<div #inputBox class="input-wrapper" [class.focused]="(focused && type !== 'select') || formAsControl?.value || formAsArray?.length > 0" <div class="input-border" [class.disabled]="formControl.disabled"
[ngClass]="inputClass" [class.disabled]="formControl.disabled" [class.select]="type === 'select'"
[class.danger]="(formControl.invalid && (formControl.touched || searchControl?.touched)) || (searchControl?.invalid && searchControl?.touched)"> [class.danger]="(formControl.invalid && (formControl.touched || searchControl?.touched)) || (searchControl?.invalid && searchControl?.touched)">
<div *ngIf="!hasPlaceholder" class="placeholder"> <div #inputBox class="input-wrapper" [class.static]="placeholderInfo.static"
<label>{{placeholder}} <sup *ngIf="required">*</sup></label> [class.focused]="focused" [class.select]="type === 'select'"
</div> [class.active]="(focused && type !== 'select') || formAsControl?.value || formAsArray?.length > 0 || hint"
<div class="uk-flex" [class.uk-flex-middle]="type !== 'textarea'"> [ngClass]="inputClass">
<ng-template [ngIf]="type === 'text' || type === 'URL' || type === 'logoURL'"> <div *ngIf="!placeholderInfo.static && placeholderInfo.label" class="placeholder">
<input #input class="input" [attr.placeholder]="hasPlaceholder?placeholder:null" [formControl]="formAsControl"> <label>{{placeholderInfo.label}} <sup *ngIf="required">*</sup></label>
</ng-template> </div>
<ng-template [ngIf]="type === 'textarea'"> <div class="uk-flex" [class.uk-flex-middle]="type !== 'textarea'">
<textarea #textArea class="input" [attr.placeholder]="hasPlaceholder?placeholder:null" [rows]="rows" [formControl]="formAsControl"></textarea> <ng-template [ngIf]="type === 'text' || type === 'URL' || type === 'logoURL'">
</ng-template> <input #input class="input" [attr.placeholder]="placeholderInfo.static?placeholderInfo.label:hint"
<ng-template [ngIf]="type === 'select'"> [formControl]="formAsControl">
<div class="input">{{getLabel(formControl.value)}}</div> </ng-template>
</ng-template> <ng-template [ngIf]="type === 'textarea'">
<ng-template [ngIf]="type === 'autocomplete'"> <textarea #textArea class="input" [attr.placeholder]="placeholderInfo.static?placeholderInfo.label:hint"
<div class="uk-grid uk-width-expand" uk-grid> [rows]="rows" [formControl]="formAsControl"></textarea>
<div *ngIf="formControl.value" class="chip"> </ng-template>
<div class="uk-label uk-flex uk-flex-middle"> <ng-template [ngIf]="type === 'select'">
<span>{{getLabel(formControl.value)}}</span> <div class="input">{{getLabel(formControl.value)}}</div>
<icon (click)="resetSearch($event)" class="uk-link-text uk-margin-small-left clickable" [flex]="true" </ng-template>
name="close" ratio="0.7"></icon> <ng-template [ngIf]="type === 'autocomplete'">
<div class="uk-grid uk-width-expand" uk-grid>
<div *ngIf="formControl.value">
<div class="input">{{getLabel(formControl.value)}}</div>
</div>
<div class="uk-width-expand">
<input [class.uk-hidden]="formControl.value" [attr.placeholder]="placeholderInfo.static?placeholderInfo.label:hint"
#searchInput class="input" [class.search]="searchControl.value" [formControl]="searchControl">
</div> </div>
</div> </div>
<div class="uk-width-expand"> </ng-template>
<input [class.uk-hidden]="formControl.value" [attr.placeholder]="hasPlaceholder?placeholder:null" #searchInput class="input search" [formControl]="searchControl"> <ng-template [ngIf]="type === 'chips'">
</div> <div class="uk-grid uk-grid-small uk-grid-row-collapse uk-width-expand" uk-grid>
</div> <div *ngFor="let chip of formAsArray.controls; let i=index" class="chip">
</ng-template> <div class="uk-label uk-flex uk-flex-middle">
<ng-template [ngIf]="type === 'chips'"> <span class="uk-text-truncate uk-width-expand">{{getLabel(chip.value)}}</span>
<div class="uk-grid uk-grid-small uk-grid-row-collapse uk-width-expand" uk-grid> <icon (click)="remove(i)" class="uk-link-text uk-margin-small-left clickable" [flex]="true"
<div *ngFor="let chip of formAsArray.controls; let i=index" class="chip"> name="close" ratio="0.7"></icon>
<div class="uk-label uk-flex uk-flex-middle"> </div>
<span class="uk-text-truncate uk-width-expand">{{getLabel(chip.value)}}</span> </div>
<icon (click)="remove(i)" class="uk-link-text uk-margin-small-left clickable" [flex]="true" <div class="uk-width-expand uk-flex uk-flex-column uk-flex-center">
name="close" ratio="0.7"></icon> <input #searchInput class="input" [class.search]="searchControl.value"
[attr.placeholder]="placeholderInfo.static?placeholderInfo.label:hint" [formControl]="searchControl">
</div> </div>
</div> </div>
<div class="uk-width-expand uk-flex uk-flex-column uk-flex-center"> </ng-template>
<input #searchInput class="input search" [attr.placeholder]="hasPlaceholder?placeholder:null" [formControl]="searchControl"> <div *ngIf="formControl.disabled || icon || type === 'select' || type === 'autocomplete'"
</div> class="uk-margin-small-left uk-margin-right icon">
<icon *ngIf="formControl.disabled" [name]="'lock'" [flex]="true"></icon>
<ng-template [ngIf]="formControl.enabled">
<icon *ngIf="!formControl.value && icon" [name]="icon" [flex]="true"></icon>
<icon *ngIf="!icon && type === 'select'" name="arrow_drop_down" [flex]="true"></icon>
<icon *ngIf="formControl.value && type === 'autocomplete'" class="clickable" (click)="resetSearch($event)" [flex]="true" name="close"></icon>
</ng-template>
</div> </div>
</ng-template> </div>
<div *ngIf="formControl.disabled || icon || type === 'select'" class="uk-margin-small-left uk-margin-right icon"> <div class="tools">
<icon *ngIf="formControl.disabled" [name]="'lock'" [flex]="true"></icon> <ng-content select="[tools]"></ng-content>
<icon *ngIf="formControl.enabled && icon" [name]="icon" [flex]="true"></icon>
<icon *ngIf="formControl.enabled && !icon" name="arrow_drop_down" [flex]="true"></icon>
</div> </div>
</div> </div>
<div class="tools">
<ng-content select="[tools]"></ng-content>
</div>
</div> </div>
<div class="options uk-dropdown" *ngIf="filteredOptions && filteredOptions.length > 1 && opened" #optionBox uk-dropdown="pos: bottom-justify; mode: hover; offset: 15;"> <div class="options uk-dropdown" *ngIf="filteredOptions && filteredOptions.length > 1 && opened" #optionBox
uk-dropdown="pos: bottom-justify; mode: hover; offset: 15;">
<ul class="uk-nav uk-dropdown-nav"> <ul class="uk-nav uk-dropdown-nav">
<li *ngFor="let option of filteredOptions" [class.uk-active]="formControl.value === option.value"> <li *ngFor="let option of filteredOptions" [class.uk-active]="formControl.value === option.value" [attr.uk-tooltip]="(tooltip)?('title: ' + option.label + ';'):null">
<a (click)="selectOption(option, $event)">{{option.label}}</a> <a (click)="selectOption(option, $event)">{{option.label}}</a>
</li> </li>
</ul> </ul>
</div> </div>
<span *ngIf="formControl.invalid && formControl.touched" class="uk-text-danger"> <span *ngIf="formControl.invalid && formControl.touched" class="uk-text-danger">
<span *ngIf="formControl.errors.error">{{formControl.errors.error}}</span> <span *ngIf="formControl.errors.error">{{formControl.errors.error}}</span>
<span <span *ngIf="type === 'URL' || type === 'logoURL'">Please provide a valid URL (e.g. https://example.com)</span>
*ngIf="type === 'URL' || type === 'logoURL'">Please provide a valid URL (e.g. https://example.com)</span>
</span> </span>
<span class="uk-text-danger uk-text-small"> <span class="uk-text-danger uk-text-small">
<ng-content select="[error]"></ng-content> <ng-content select="[error]"></ng-content>
@ -114,30 +127,12 @@ declare var UIkit;
<span class="uk-text-small"> <span class="uk-text-small">
<ng-content select="[note]"></ng-content> <ng-content select="[note]"></ng-content>
</span> </span>
`, `
styleUrls: ['input.component.css']
}) })
export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChanges { export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChanges {
/** Basic information */ /** Deprecated options*/
@Input('formInput') formControl: AbstractControl;
@Input('type') type: 'text' | 'URL' | 'logoURL' | 'autocomplete' | 'textarea' | 'select' | 'chips' = 'text';
@Input() value: any | any[];
@Output() valueChange = new EventEmitter<any | any[]>();
/** @deprecated */ /** @deprecated */
@Input('label') label: string; @Input('label') label: string;
/** Text */
@ViewChild('input') input: ElementRef;
/** Textarea options */
@ViewChild('textArea') textArea: ElementRef;
@Input('rows') rows: number = 3;
/** Select | chips available options */
@Input('options') options: Option[] = [];
/** @deprecated */
@Input('hint') hint = null;
@Input('placeholder') placeholder = '';
@Input('staticPlaceholder') staticPlaceholder: boolean = false;
@Input() inputClass: string = 'inner';
/** Extra element Right or Left of the input */
/** @deprecated */ /** @deprecated */
@Input() extraLeft: boolean = true; @Input() extraLeft: boolean = true;
/** @deprecated */ /** @deprecated */
@ -146,16 +141,39 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
@Input() hideControl: boolean = false; @Input() hideControl: boolean = false;
/** @deprecated */ /** @deprecated */
@Input() flex: 'middle' | 'top' | 'bottom' = 'middle'; @Input() flex: 'middle' | 'top' | 'bottom' = 'middle';
/** Icon on the input */
@Input() icon: string = null;
/** @deprecated */ /** @deprecated */
@Input() iconLeft: boolean = false; @Input() iconLeft: boolean = false;
/** Chip options */ /** @deprecated */
@Input() removable: boolean = true; @Input() removable: boolean = true;
@Input() addExtraChips: boolean = false; /** @deprecated */
@Input() smallChip: boolean = false; @Input() smallChip: boolean = false;
/** @deprecated */
@Input() panelWidth: number = 300; @Input() panelWidth: number = 300;
/** @deprecated */
@Input() panelClass: string = null; @Input() panelClass: string = null;
/** Basic information */
@Input('formInput') formControl: AbstractControl;
@Input('type') type: 'text' | 'URL' | 'logoURL' | 'autocomplete' | 'textarea' | 'select' | 'chips' = 'text';
@Input() value: any | any[];
@Output() valueChange = new EventEmitter<any | any[]>();
@Input('hint') hint: string;
/** Text */
@ViewChild('input') input: ElementRef;
/** Textarea options */
@ViewChild('textArea') textArea: ElementRef;
@Input('rows') rows: number = 3;
/** Select | Autocomplete | chips available options */
@Input('options') options: Option[] = [];
@Input() tooltip: boolean = false;
/** Chips && Autocomplete*/
public filteredOptions: Option[] = [];
public searchControl: FormControl;
/** Use modifier's class(es) to change view of your Input */
@Input() inputClass: string = 'inner';
/** Icon on the input */
@Input() icon: string = null;
/** Chip options */
@Input() addExtraChips: boolean = false;
@Input() showOptionsOnEmpty: boolean = true; @Input() showOptionsOnEmpty: boolean = true;
@Input() validators: ValidatorFn[] | ValidatorFn; @Input() validators: ValidatorFn[] | ValidatorFn;
@Input() disabled: boolean = false; @Input() disabled: boolean = false;
@ -163,20 +181,30 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
/** LogoUrl information */ /** LogoUrl information */
public secure: boolean = true; public secure: boolean = true;
/** Internal basic information */ /** Internal basic information */
public placeholderInfo: Placeholder = {label: '', static: true};
public required: boolean = false; public required: boolean = false;
public focused: boolean = false; public focused: boolean = false;
public opened: boolean = false; public opened: boolean = false;
public properties: EnvProperties = properties; public properties: EnvProperties = properties;
private initValue: any; private initValue: any;
private subscriptions: any[] = []; private subscriptions: any[] = [];
/** Chips && Autocomplete*/
public filteredOptions: Option[] = [];
public searchControl: FormControl;
@Input() tooltip: boolean = true;
@ViewChild('inputBox') inputBox: ElementRef; @ViewChild('inputBox') inputBox: ElementRef;
@ViewChild('optionBox') optionBox: ElementRef; @ViewChild('optionBox') optionBox: ElementRef;
@ViewChild('searchInput') searchInput: ElementRef; @ViewChild('searchInput') searchInput: ElementRef;
@Input()
set placeholder(placeholder: string | Placeholder) {
if(typeof placeholder === 'string') {
this.placeholderInfo = {label: placeholder, static: false};
} else {
if(placeholder.static && (this.type === 'select' || this.type === 'autocomplete' || this.type === 'chips' || this.hint)) {
placeholder.static = false;
console.debug('Static placeholder is not available in this type of input and if hint is available.');
}
this.placeholderInfo = placeholder;
}
}
constructor(private elementRef: ElementRef) { constructor(private elementRef: ElementRef) {
if (elementRef.nativeElement.hasAttribute('dashboard-input') && this.properties.environment === "development") { if (elementRef.nativeElement.hasAttribute('dashboard-input') && this.properties.environment === "development") {
console.warn("'dashboard-input' selector is deprecated; use 'input' instead."); console.warn("'dashboard-input' selector is deprecated; use 'input' instead.");
@ -235,10 +263,6 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
} }
} }
get hasPlaceholder() {
return this.staticPlaceholder && this.type !== 'select';
}
reset() { reset() {
this.secure = true; this.secure = true;
this.unsubscribe(); this.unsubscribe();