Skip to content
Snippets Groups Projects
Commit 99e04439 authored by Tormod Nygård's avatar Tormod Nygård
Browse files

Issue: Custom input components (#3)

parent 71195b3f
No related branches found
No related tags found
1 merge request!3Resolve "Frontend: Annonser form"
Showing
with 247 additions and 19 deletions
<h3>Lag annonse</h3> <h3>Lag annonse</h3>
<label for="title">Tittel</label> <app-text-input [(inputModel)]="title" label="Tittel" (blur)="checkForm()"></app-text-input>
<input id="title" [(ngModel)]="title" (blur)="checkForm()">
<label for="description">Beskrivelse</label> <app-text-input [(inputModel)]="description" label="Beskrivelse" (blur)="checkForm()"></app-text-input>
<input id="description" [(ngModel)]="description" (blur)="checkForm()">
<label for="price">Pris</label> <app-number-input [(inputModel)]="price" label="Pris" (blur)="checkForm()"></app-number-input>
<input type="number" id="price" [(ngModel)]="price" (blur)="checkForm()">
<label for="category">Kategori</label> <app-select [(inputModel)]="categoryid" (change)="checkForm()" label="Kategori">
<select id="category" [(ngModel)]="categoryid" (change)="checkForm()" >
<option value="0" selected>Velg kategori</option> <option value="0" selected>Velg kategori</option>
<option value="1">Antikviteter og Kunst</option> <option value="1">Antikviteter og Kunst</option>
<option value="2">Dyr og Utstyr</option> <option value="2">Dyr og Utstyr</option>
<option value="3">Elektronikk og Hvitevarer</option> <option value="3">Elektronikk og Hvitevarer</option>
</select> </app-select>
<p>{{statusMessage}}</p> <p>{{statusMessage}}</p>
<button (click)="publishPost()">Publiser</button> <app-button (click)="publishPost()" text="Publiser"></app-button>
\ No newline at end of file \ No newline at end of file
<button>{{text}}</button>
\ No newline at end of file
button{
padding: 5px;
}
\ No newline at end of file
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ButtonComponent } from './button.component';
describe('ButtonComponent', () => {
let component: ButtonComponent;
let fixture: ComponentFixture<ButtonComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ ButtonComponent ]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(ButtonComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'app-button',
templateUrl: './button.component.html',
styleUrls: ['./button.component.scss']
})
export class ButtonComponent {
@Input()
text: string;
constructor() { }
}
<label>
{{label}}
<input
type="number"
[placeholder]="placeholder"
[(ngModel)]="inputModel"
(ngModelChange)="inputModelChange.emit(inputModel)"
(change)="change.emit($event)"
(focus)="focus.emit($event)"
(blur)="blur.emit($event)">
</label>
\ No newline at end of file
input{
padding: 5px;
}
\ No newline at end of file
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { NumberInputComponent } from './number-input.component';
describe('NumberInputComponent', () => {
let component: NumberInputComponent;
let fixture: ComponentFixture<NumberInputComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ NumberInputComponent ]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(NumberInputComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'app-number-input',
templateUrl: './number-input.component.html',
styleUrls: ['./number-input.component.scss']
})
export class NumberInputComponent {
@Input()
label: string = "";
@Input()
inputModel: number;
@Input()
placeholder: string = "";
@Output()
inputModelChange = new EventEmitter<number>();
@Output()
change = new EventEmitter();
@Output()
focus = new EventEmitter();
@Output()
blur = new EventEmitter();
constructor() { }
}
<label>
{{label}}
<select
[(ngModel)]="inputModel"
(ngModelChange)="inputModelChange.emit($event)"
(change)="change.emit($event)"
(focus)="focus.emit($event)"
(blur)="blur.emit($event)">
<ng-content></ng-content>
</select>
</label>
\ No newline at end of file
select{
padding: 5px;
}
\ No newline at end of file
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { SelectComponent } from './select.component';
describe('SelectComponent', () => {
let component: SelectComponent;
let fixture: ComponentFixture<SelectComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ SelectComponent ]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(SelectComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'app-select',
templateUrl: './select.component.html',
styleUrls: ['./select.component.scss']
})
export class SelectComponent {
@Input()
label: string = "";
@Input()
inputModel: any;
@Input()
placeholder: string = "";
@Output()
inputModelChange = new EventEmitter<any>();
@Output()
change = new EventEmitter();
@Output()
focus = new EventEmitter();
@Output()
blur = new EventEmitter();
constructor() { }
}
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { TextInputComponent } from './text-input/text-input.component'; import { TextInputComponent } from './text-input/text-input.component';
import { FormsModule } from '@angular/forms';
import { NumberInputComponent } from './number-input/number-input.component';
import { ButtonComponent } from './button/button.component';
import { SelectComponent } from './select/select.component';
@NgModule({ @NgModule({
declarations: [TextInputComponent], declarations: [
exports: [TextInputComponent], TextInputComponent,
NumberInputComponent,
ButtonComponent,
SelectComponent
],
exports: [
TextInputComponent,
NumberInputComponent,
ButtonComponent,
SelectComponent
],
imports: [ imports: [
CommonModule CommonModule,
FormsModule
] ]
}) })
export class SharedModule { } export class SharedModule { }
<label for="textField">{{label}}</label> <label>
<input type="text" id="textField"> {{label}}
\ No newline at end of file
<input
type="text"
[placeholder]="placeholder"
[(ngModel)]="inputModel"
(ngModelChange)="inputModelChange.emit(inputModel)"
(change)="change.emit($event)"
(focus)="focus.emit($event)"
(blur)="blur.emit($event)">
</label>
\ No newline at end of file
#textField{ input{
padding: 5px; padding: 5px;
} }
\ No newline at end of file
import { Component, Input, OnInit, Output } from '@angular/core'; import { Component, EventEmitter, Input, Output } from '@angular/core';
import { ControlValueAccessor } from '@angular/forms';
@Component({ @Component({
selector: 'app-text-input', selector: 'app-text-input',
...@@ -11,6 +10,24 @@ export class TextInputComponent { ...@@ -11,6 +10,24 @@ export class TextInputComponent {
@Input() @Input()
label: string = ""; label: string = "";
@Input()
inputModel: string;
@Input()
placeholder: string = "";
@Output()
inputModelChange = new EventEmitter<string>();
@Output()
change = new EventEmitter();
@Output()
focus = new EventEmitter();
@Output()
blur = new EventEmitter();
constructor() { } constructor() { }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment