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
}
export interface Placeholder {
label: string,
static?: boolean
}
declare var UIkit;
/**
@ -34,71 +39,79 @@ declare var UIkit;
@Component({
selector: '[dashboard-input], [input]',
template: `
<div #inputBox class="input-wrapper" [class.focused]="(focused && type !== 'select') || formAsControl?.value || formAsArray?.length > 0"
[ngClass]="inputClass" [class.disabled]="formControl.disabled" [class.select]="type === 'select'"
<div class="input-border" [class.disabled]="formControl.disabled"
[class.danger]="(formControl.invalid && (formControl.touched || searchControl?.touched)) || (searchControl?.invalid && searchControl?.touched)">
<div *ngIf="!hasPlaceholder" class="placeholder">
<label>{{placeholder}} <sup *ngIf="required">*</sup></label>
</div>
<div class="uk-flex" [class.uk-flex-middle]="type !== 'textarea'">
<ng-template [ngIf]="type === 'text' || type === 'URL' || type === 'logoURL'">
<input #input class="input" [attr.placeholder]="hasPlaceholder?placeholder:null" [formControl]="formAsControl">
</ng-template>
<ng-template [ngIf]="type === 'textarea'">
<textarea #textArea class="input" [attr.placeholder]="hasPlaceholder?placeholder:null" [rows]="rows" [formControl]="formAsControl"></textarea>
</ng-template>
<ng-template [ngIf]="type === 'select'">
<div class="input">{{getLabel(formControl.value)}}</div>
</ng-template>
<ng-template [ngIf]="type === 'autocomplete'">
<div class="uk-grid uk-width-expand" uk-grid>
<div *ngIf="formControl.value" class="chip">
<div class="uk-label uk-flex uk-flex-middle">
<span>{{getLabel(formControl.value)}}</span>
<icon (click)="resetSearch($event)" class="uk-link-text uk-margin-small-left clickable" [flex]="true"
name="close" ratio="0.7"></icon>
<div #inputBox class="input-wrapper" [class.static]="placeholderInfo.static"
[class.focused]="focused" [class.select]="type === 'select'"
[class.active]="(focused && type !== 'select') || formAsControl?.value || formAsArray?.length > 0 || hint"
[ngClass]="inputClass">
<div *ngIf="!placeholderInfo.static && placeholderInfo.label" class="placeholder">
<label>{{placeholderInfo.label}} <sup *ngIf="required">*</sup></label>
</div>
<div class="uk-flex" [class.uk-flex-middle]="type !== 'textarea'">
<ng-template [ngIf]="type === 'text' || type === 'URL' || type === 'logoURL'">
<input #input class="input" [attr.placeholder]="placeholderInfo.static?placeholderInfo.label:hint"
[formControl]="formAsControl">
</ng-template>
<ng-template [ngIf]="type === 'textarea'">
<textarea #textArea class="input" [attr.placeholder]="placeholderInfo.static?placeholderInfo.label:hint"
[rows]="rows" [formControl]="formAsControl"></textarea>
</ng-template>
<ng-template [ngIf]="type === 'select'">
<div class="input">{{getLabel(formControl.value)}}</div>
</ng-template>
<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 class="uk-width-expand">
<input [class.uk-hidden]="formControl.value" [attr.placeholder]="hasPlaceholder?placeholder:null" #searchInput class="input search" [formControl]="searchControl">
</div>
</div>
</ng-template>
<ng-template [ngIf]="type === 'chips'">
<div class="uk-grid uk-grid-small uk-grid-row-collapse uk-width-expand" uk-grid>
<div *ngFor="let chip of formAsArray.controls; let i=index" class="chip">
<div class="uk-label uk-flex uk-flex-middle">
<span class="uk-text-truncate uk-width-expand">{{getLabel(chip.value)}}</span>
<icon (click)="remove(i)" class="uk-link-text uk-margin-small-left clickable" [flex]="true"
name="close" ratio="0.7"></icon>
</ng-template>
<ng-template [ngIf]="type === 'chips'">
<div class="uk-grid uk-grid-small uk-grid-row-collapse uk-width-expand" uk-grid>
<div *ngFor="let chip of formAsArray.controls; let i=index" class="chip">
<div class="uk-label uk-flex uk-flex-middle">
<span class="uk-text-truncate uk-width-expand">{{getLabel(chip.value)}}</span>
<icon (click)="remove(i)" class="uk-link-text uk-margin-small-left clickable" [flex]="true"
name="close" ratio="0.7"></icon>
</div>
</div>
<div class="uk-width-expand uk-flex uk-flex-column uk-flex-center">
<input #searchInput class="input" [class.search]="searchControl.value"
[attr.placeholder]="placeholderInfo.static?placeholderInfo.label:hint" [formControl]="searchControl">
</div>
</div>
<div class="uk-width-expand uk-flex uk-flex-column uk-flex-center">
<input #searchInput class="input search" [attr.placeholder]="hasPlaceholder?placeholder:null" [formControl]="searchControl">
</div>
</ng-template>
<div *ngIf="formControl.disabled || icon || type === 'select' || type === 'autocomplete'"
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>
</ng-template>
<div *ngIf="formControl.disabled || icon || type === 'select'" class="uk-margin-small-left uk-margin-right icon">
<icon *ngIf="formControl.disabled" [name]="'lock'" [flex]="true"></icon>
<icon *ngIf="formControl.enabled && icon" [name]="icon" [flex]="true"></icon>
<icon *ngIf="formControl.enabled && !icon" name="arrow_drop_down" [flex]="true"></icon>
</div>
<div class="tools">
<ng-content select="[tools]"></ng-content>
</div>
</div>
<div class="tools">
<ng-content select="[tools]"></ng-content>
</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">
<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>
</li>
</ul>
</div>
<span *ngIf="formControl.invalid && formControl.touched" class="uk-text-danger">
<span *ngIf="formControl.errors.error">{{formControl.errors.error}}</span>
<span
*ngIf="type === 'URL' || type === 'logoURL'">Please provide a valid URL (e.g. https://example.com)</span>
<span *ngIf="type === 'URL' || type === 'logoURL'">Please provide a valid URL (e.g. https://example.com)</span>
</span>
<span class="uk-text-danger uk-text-small">
<ng-content select="[error]"></ng-content>
@ -114,30 +127,12 @@ declare var UIkit;
<span class="uk-text-small">
<ng-content select="[note]"></ng-content>
</span>
`,
styleUrls: ['input.component.css']
`
})
export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChanges {
/** 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[]>();
/** Deprecated options*/
/** @deprecated */
@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 */
@Input() extraLeft: boolean = true;
/** @deprecated */
@ -146,16 +141,39 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
@Input() hideControl: boolean = false;
/** @deprecated */
@Input() flex: 'middle' | 'top' | 'bottom' = 'middle';
/** Icon on the input */
@Input() icon: string = null;
/** @deprecated */
@Input() iconLeft: boolean = false;
/** Chip options */
/** @deprecated */
@Input() removable: boolean = true;
@Input() addExtraChips: boolean = false;
/** @deprecated */
@Input() smallChip: boolean = false;
/** @deprecated */
@Input() panelWidth: number = 300;
/** @deprecated */
@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() validators: ValidatorFn[] | ValidatorFn;
@Input() disabled: boolean = false;
@ -163,20 +181,30 @@ export class InputComponent implements OnInit, OnDestroy, AfterViewInit, OnChang
/** LogoUrl information */
public secure: boolean = true;
/** Internal basic information */
public placeholderInfo: Placeholder = {label: '', static: true};
public required: boolean = false;
public focused: boolean = false;
public opened: boolean = false;
public properties: EnvProperties = properties;
private initValue: any;
private subscriptions: any[] = [];
/** Chips && Autocomplete*/
public filteredOptions: Option[] = [];
public searchControl: FormControl;
@Input() tooltip: boolean = true;
@ViewChild('inputBox') inputBox: ElementRef;
@ViewChild('optionBox') optionBox: 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) {
if (elementRef.nativeElement.hasAttribute('dashboard-input') && this.properties.environment === "development") {
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() {
this.secure = true;
this.unsubscribe();