feat: add input component and integrate FontAwesome icons
This commit is contained in:
56
src/app/shared/components/input/input.component.html
Normal file
56
src/app/shared/components/input/input.component.html
Normal file
@@ -0,0 +1,56 @@
|
||||
<label [for]="id" class="input-label">
|
||||
<ng-content />
|
||||
</label>
|
||||
|
||||
<div
|
||||
class="input-control"
|
||||
[ngClass]="{
|
||||
'input-control-currency': type() === 'currency',
|
||||
'input-control-percent': type() === 'percent',
|
||||
'input-control-file': type() === 'file',
|
||||
'input-control-editable': type() === 'editable',
|
||||
'input-control-disabled': isInputDisabled(),
|
||||
'input-control-editable-active': isEditableActive()
|
||||
}"
|
||||
>
|
||||
@if (type() === 'currency') {
|
||||
<span class="input-prefix">$</span>
|
||||
}
|
||||
|
||||
<input
|
||||
#inputElement
|
||||
[id]="id"
|
||||
[name]="id"
|
||||
[type]="inputType()"
|
||||
[ngClass]="inputClasses()"
|
||||
[disabled]="isInputDisabled()"
|
||||
[placeholder]="placeholder()"
|
||||
[value]="type() !== 'file' ? value() : null"
|
||||
(input)="type() !== 'file' ? onValueChange($event) : null"
|
||||
(change)="type() === 'file' ? onFileChange($event) : null"
|
||||
[attr.maxlength]="type() !== 'file' ? maxlength() : null"
|
||||
[attr.accept]="type() === 'file' ? accept() : null"
|
||||
/>
|
||||
|
||||
@if (type() === 'file') {
|
||||
<span class="input-file-name" [class.input-file-name-empty]="!resolvedFileName()">
|
||||
{{ resolvedFileName() || 'Sin archivo...' }}
|
||||
</span>
|
||||
}
|
||||
|
||||
@if (type() === 'percent') {
|
||||
<span class="input-suffix">%</span>
|
||||
}
|
||||
|
||||
@if (type() === 'editable') {
|
||||
<button
|
||||
type="button"
|
||||
class="input-icon"
|
||||
[class.input-icon-active]="isEditableActive()"
|
||||
[attr.aria-label]="isEditableActive() ? 'Deshabilitar edición' : 'Habilitar edición'"
|
||||
(click)="toggleEditableState()"
|
||||
>
|
||||
<i class="fa-solid fa-pen" aria-hidden="true"></i>
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
140
src/app/shared/components/input/input.component.scss
Normal file
140
src/app/shared/components/input/input.component.scss
Normal file
@@ -0,0 +1,140 @@
|
||||
:host {
|
||||
display: block;
|
||||
margin-bottom: 0 !important;
|
||||
padding-bottom: 0 !important;
|
||||
}
|
||||
|
||||
.input-label {
|
||||
display: block;
|
||||
margin-bottom: 0.375rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.input-control {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.form-control {
|
||||
min-height: 40px;
|
||||
padding: 0.625rem 1rem;
|
||||
border-radius: 0.5rem;
|
||||
color: #212529;
|
||||
|
||||
&:focus {
|
||||
box-shadow: none;
|
||||
}
|
||||
|
||||
&:disabled {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
input[type='file'].form-control {
|
||||
padding-right: 1rem;
|
||||
color: transparent;
|
||||
|
||||
&::file-selector-button {
|
||||
height: 2.25rem;
|
||||
margin-right: 1rem;
|
||||
padding: 0 1rem;
|
||||
border: 0;
|
||||
border-radius: 0.375rem;
|
||||
background-color: var(--bs-secondary-bg);
|
||||
color: #212529;
|
||||
cursor: pointer;
|
||||
}
|
||||
}
|
||||
|
||||
.input-control-file {
|
||||
.form-control {
|
||||
position: relative;
|
||||
}
|
||||
}
|
||||
|
||||
.input-file-name {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: 11.5rem;
|
||||
right: 1rem;
|
||||
color: #495057;
|
||||
font-size: 0.875rem;
|
||||
line-height: 1.4;
|
||||
transform: translateY(-50%);
|
||||
pointer-events: none;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.input-file-name-empty {
|
||||
color: #6c757d;
|
||||
}
|
||||
|
||||
.input-control-currency {
|
||||
.input-prefix {
|
||||
position: absolute;
|
||||
left: 1rem;
|
||||
top: 50%;
|
||||
z-index: 1;
|
||||
color: #495057;
|
||||
transform: translateY(-50%);
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.input-with-prefix {
|
||||
padding-left: 2rem;
|
||||
}
|
||||
}
|
||||
|
||||
.input-control-editable {
|
||||
.input-icon {
|
||||
position: absolute;
|
||||
right: 0.875rem;
|
||||
top: 50%;
|
||||
z-index: 1;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 1.5rem;
|
||||
height: 1.5rem;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
background: transparent;
|
||||
color: #495057;
|
||||
transform: translateY(-50%);
|
||||
cursor: pointer;
|
||||
transition: color 0.2s ease;
|
||||
|
||||
&:focus {
|
||||
outline: none;
|
||||
}
|
||||
}
|
||||
|
||||
.input-icon-active {
|
||||
color: #adb5bd;
|
||||
|
||||
&:hover {
|
||||
color: #495057;
|
||||
}
|
||||
}
|
||||
|
||||
.input-with-icon {
|
||||
padding-right: 2.5rem;
|
||||
}
|
||||
}
|
||||
|
||||
.input-control-percent {
|
||||
.input-suffix {
|
||||
position: absolute;
|
||||
right: 1rem;
|
||||
top: 50%;
|
||||
z-index: 1;
|
||||
color: #495057;
|
||||
transform: translateY(-50%);
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.input-with-suffix {
|
||||
padding-right: 2rem;
|
||||
}
|
||||
}
|
||||
167
src/app/shared/components/input/input.component.ts
Normal file
167
src/app/shared/components/input/input.component.ts
Normal file
@@ -0,0 +1,167 @@
|
||||
import {
|
||||
ChangeDetectionStrategy,
|
||||
Component,
|
||||
ElementRef,
|
||||
computed,
|
||||
effect,
|
||||
input,
|
||||
output,
|
||||
signal,
|
||||
untracked,
|
||||
viewChild
|
||||
} from '@angular/core';
|
||||
import { NgClass } from '@angular/common';
|
||||
|
||||
export type AppInputType =
|
||||
| 'text'
|
||||
| 'number'
|
||||
| 'email'
|
||||
| 'password'
|
||||
| 'currency'
|
||||
| 'percent'
|
||||
| 'date'
|
||||
| 'period'
|
||||
| 'editable'
|
||||
| 'file';
|
||||
|
||||
const DEFAULT_TYPE: AppInputType = 'text';
|
||||
const INPUT_TYPES = new Set<AppInputType>([
|
||||
'text',
|
||||
'number',
|
||||
'email',
|
||||
'password',
|
||||
'currency',
|
||||
'percent',
|
||||
'date',
|
||||
'period',
|
||||
'editable',
|
||||
'file'
|
||||
]);
|
||||
|
||||
function normalizeType(value: AppInputType | string | undefined): AppInputType {
|
||||
return value && INPUT_TYPES.has(value as AppInputType)
|
||||
? (value as AppInputType)
|
||||
: DEFAULT_TYPE;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-input',
|
||||
imports: [NgClass],
|
||||
templateUrl: './input.component.html',
|
||||
styleUrl: './input.component.scss',
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
host: {
|
||||
class: 'form-group'
|
||||
}
|
||||
})
|
||||
export class InputComponent {
|
||||
private readonly inputElement = viewChild<ElementRef<HTMLInputElement>>('inputElement');
|
||||
private readonly selectedFileName = signal('');
|
||||
private readonly editableDisabled = signal(false);
|
||||
private lastClearTrigger = 0;
|
||||
|
||||
readonly invalid = input(false);
|
||||
readonly placeholder = input('');
|
||||
readonly value = input<string | number>('');
|
||||
readonly maxlength = input<number | null>(null);
|
||||
readonly accept = input('');
|
||||
readonly fileName = input('');
|
||||
readonly clearTrigger = input(0);
|
||||
readonly disabled = input(false);
|
||||
readonly type = input<AppInputType, AppInputType | string>(DEFAULT_TYPE, {
|
||||
transform: normalizeType
|
||||
});
|
||||
|
||||
readonly disabledChange = output<boolean>();
|
||||
readonly valueChange = output<string>();
|
||||
readonly fileChange = output<File | null>();
|
||||
|
||||
protected readonly id = `app-input-${globalThis.crypto.randomUUID()}`;
|
||||
|
||||
protected readonly inputType = computed(() => {
|
||||
const type = this.type();
|
||||
|
||||
if (type === 'currency' || type === 'percent' || type === 'editable') {
|
||||
return 'text';
|
||||
}
|
||||
|
||||
if (type === 'period') {
|
||||
return 'month';
|
||||
}
|
||||
|
||||
return type;
|
||||
});
|
||||
|
||||
protected readonly inputClasses = computed(() => ({
|
||||
'form-control': true,
|
||||
'is-invalid': this.invalid(),
|
||||
'input-with-prefix': this.type() === 'currency',
|
||||
'input-with-suffix': this.type() === 'percent',
|
||||
'input-with-icon': this.type() === 'editable'
|
||||
}));
|
||||
|
||||
protected readonly isInputDisabled = computed(() =>
|
||||
this.type() === 'editable' ? this.editableDisabled() : this.disabled()
|
||||
);
|
||||
|
||||
protected readonly isEditableActive = computed(
|
||||
() => this.type() === 'editable' && !this.editableDisabled()
|
||||
);
|
||||
|
||||
protected readonly resolvedFileName = computed(
|
||||
() => this.selectedFileName() || this.fileName() || ''
|
||||
);
|
||||
|
||||
constructor() {
|
||||
effect(() => {
|
||||
this.editableDisabled.set(this.disabled());
|
||||
});
|
||||
|
||||
effect(() => {
|
||||
if (this.type() !== 'file') {
|
||||
return;
|
||||
}
|
||||
|
||||
const clearTrigger = this.clearTrigger();
|
||||
const value = this.value();
|
||||
const fileName = this.fileName();
|
||||
|
||||
untracked(() => {
|
||||
if (clearTrigger !== this.lastClearTrigger) {
|
||||
this.lastClearTrigger = clearTrigger;
|
||||
this.clearFileInput();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!value && !fileName) {
|
||||
this.clearFileInput();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
protected onValueChange(event: Event): void {
|
||||
this.valueChange.emit((event.target as HTMLInputElement).value);
|
||||
}
|
||||
|
||||
protected onFileChange(event: Event): void {
|
||||
const file = (event.target as HTMLInputElement).files?.[0] ?? null;
|
||||
this.selectedFileName.set(file?.name ?? '');
|
||||
this.fileChange.emit(file);
|
||||
}
|
||||
|
||||
protected toggleEditableState(): void {
|
||||
if (this.type() !== 'editable') {
|
||||
return;
|
||||
}
|
||||
|
||||
const nextState = !this.editableDisabled();
|
||||
this.editableDisabled.set(nextState);
|
||||
this.disabledChange.emit(nextState);
|
||||
}
|
||||
|
||||
private clearFileInput(): void {
|
||||
this.inputElement()?.nativeElement && (this.inputElement()!.nativeElement.value = '');
|
||||
this.selectedFileName.set('');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user