-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Move ng-xform buttons to a separate component
- Loading branch information
Showing
13 changed files
with
275 additions
and
52 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<div class="pull-right btn-toolbar"> | ||
<button type="submit" class="btn btn-primary" id="formEditBtn" (click)="edit.emit()" *ngIf="editing === false"> | ||
<span class="fa fa-pencil"></span> Edit | ||
</button> | ||
<ng-template [ngIf]="editing !== false"> | ||
<button type="submit" class="btn btn-primary" id="formSubmitBtn" (click)="save.emit()">Save</button> | ||
<button type="button" class="btn btn-default" id="formCancelBtn" (click)="cancel.emit()">Cancel</button> | ||
</ng-template> | ||
</div> |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Component, EventEmitter, Input, Output } from '@angular/core'; | ||
|
||
|
||
@Component({ | ||
selector: 'ng-xform-button-bar', | ||
templateUrl: './button-bar.component.html', | ||
}) | ||
export class ButtonBarComponent { | ||
|
||
@Input() editing = false; | ||
@Output() save = new EventEmitter(); | ||
@Output() cancel = new EventEmitter(); | ||
@Output() edit = new EventEmitter(); | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/ng-xform/ng-xform-save-edit/ng-xform-save-edit.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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<div class="row"> | ||
<ng-xform [horizontalForm]="horizontalForm" [labelWidth]="labelWidth" [fields]='fields' [editing]='editing'></ng-xform> | ||
</div> | ||
<div class="row"> | ||
<ng-xform-button-bar | ||
[editing]='editing' | ||
(save)="onSubmit()" | ||
(cancel)="onCancel()" | ||
(edit)="setEditing(true)" | ||
></ng-xform-button-bar> | ||
</div> |
142 changes: 142 additions & 0 deletions
142
src/ng-xform/ng-xform-save-edit/ng-xform-save-edit.component.spec.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 |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { Validators } from '@angular/forms'; | ||
import { By } from '@angular/platform-browser'; | ||
import { DatePipe } from '@angular/common'; | ||
import { BsLocaleService } from 'ngx-bootstrap/datepicker'; | ||
|
||
import { NgXformSaveEditComponent } from './ng-xform-save-edit.component'; | ||
import { DateField, MeasureField, TextField } from '../fields'; | ||
import { CheckboxField } from '../fields/checkbox-field'; | ||
import { MultilineField } from '../fields/multiline-field'; | ||
import { NestedFormGroup } from '../fields/nested-form-group'; | ||
import { NgXformModule } from '../ng-xform.module'; | ||
|
||
describe('NgXformSaveEdit', () => { | ||
let component: NgXformSaveEditComponent; | ||
let fixture: ComponentFixture<NgXformSaveEditComponent>; | ||
let model: any; | ||
let dateTest: Date; | ||
let datePipe: DatePipe; | ||
let bsLocaleService: BsLocaleService; | ||
|
||
beforeEach( | ||
async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
NgXformModule | ||
] | ||
}).compileComponents(); | ||
bsLocaleService = TestBed.get(BsLocaleService); | ||
bsLocaleService.use('en'); | ||
}) | ||
); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(NgXformSaveEditComponent); | ||
component = fixture.componentInstance; | ||
dateTest = new Date(); | ||
datePipe = new DatePipe('en'); | ||
model = { | ||
text1: 'value1', | ||
required: 'value2', | ||
comments: 'comments here...', | ||
address: { | ||
street: 'St wall', | ||
city: 'Ny', | ||
extra_field: 'Extra', | ||
}, | ||
nested2: null, | ||
date: dateTest, | ||
}; | ||
const colors: any[] = [ | ||
{ id: 1, name: 'blue' }, | ||
{ id: 2, name: 'yellow' }, | ||
{ id: 3, name: 'white' } | ||
]; | ||
|
||
component.fields = [ | ||
new TextField({ key: 'text1', label: 'Text 1' }), | ||
new TextField({ key: 'required', label: 'Required 1', validators: [Validators.required] }), | ||
new MeasureField({ key: 'measure1', label: 'Measure 1', modelUnit: 'degC' }), | ||
new MultilineField({ key: 'comments', label: 'Comments' }), | ||
new TextField({ key: 'other_color', label: 'Other Color', visibilityFn: (formVal: any) => formVal.color === 1 }), | ||
new NestedFormGroup({ | ||
key: 'address', label: 'Address', | ||
fields: [ | ||
new TextField({ key: 'street', label: 'Street' }), | ||
new TextField({ key: 'city', label: 'City' }), | ||
] | ||
}), | ||
new NestedFormGroup({ | ||
key: 'nested2', | ||
fields: [ | ||
new TextField({ key: 'field1', label: 'Field1' }), | ||
] | ||
}), | ||
new DateField({ key: 'date', label: 'Date' }), | ||
new CheckboxField({ key: 'isChecked', label: 'Is Checked' }), | ||
]; | ||
|
||
fixture.detectChanges(); | ||
component.setValue(model); | ||
}); | ||
|
||
|
||
it('should patch value', () => { | ||
expect(component.xform.form.value.nested2).toBeTruthy(); | ||
}); | ||
|
||
|
||
it('should emit form value on submit', (done: any) => { | ||
const editBtnEl = fixture.debugElement.query(By.css(`#formEditBtn`)); | ||
editBtnEl.nativeElement.click(); | ||
fixture.detectChanges(); | ||
|
||
component.submit.subscribe((value: any) => { | ||
setTimeout(() => { | ||
expect(value.text1).toBe(model.text1); | ||
expect(value.comments).toBe(model.comments); | ||
expect(value.date).toBe(dateTest); | ||
expect(value.isChecked).toBeFalsy(); | ||
done(); | ||
}); | ||
}); | ||
const buttonEl = fixture.debugElement.query(By.css(`#formSubmitBtn`)); | ||
fixture.detectChanges(); | ||
expect(buttonEl).toBeTruthy(); | ||
expect(component.xform.form.valid).toBeTruthy(); | ||
buttonEl.nativeElement.click(); | ||
}); | ||
|
||
it('should be read mode', () => { | ||
const editBtnEl = fixture.debugElement.query(By.css(`#formEditBtn`)); | ||
editBtnEl.nativeElement.click(); | ||
fixture.detectChanges(); | ||
|
||
expect(component.editing).toBeTruthy(); | ||
const cancelBtnEl = fixture.debugElement.query(By.css(`#formCancelBtn`)); | ||
cancelBtnEl.nativeElement.click(); | ||
fixture.detectChanges(); | ||
expect(component.editing).toBeFalsy(); | ||
expectFormLabel('text1', 'Text 1', model.text1); | ||
expectFormLabel('comments', 'Comments', model.comments); | ||
// implement tes of value | ||
expectFormLabel('street', 'Street', model.address.street); | ||
expectFormLabel('city', 'City', model.address.city); | ||
expectFormLabel('date', 'Date', datePipe.transform(dateTest, 'mediumDate', 'en')); | ||
}); | ||
|
||
|
||
function expectFormLabel(fieldId: string, caption: string, value: any) { | ||
const el = fixture.debugElement.query(By.css(`#${fieldId}-div`)); | ||
expect(el).toBeTruthy(); | ||
expect(el.nativeElement).toBeTruthy(); | ||
expect(el.nativeElement.hidden).toBeFalsy(); | ||
const label = el.query(By.css('label')); | ||
expect(label.nativeElement.textContent.trim()).toBe(caption); | ||
const labelValue = el.query(By.css('div.text-muted')); | ||
expect(labelValue).toBeTruthy(); | ||
expect(labelValue.nativeElement.textContent.trim()).toBe(value); | ||
} | ||
|
||
}); |
61 changes: 61 additions & 0 deletions
61
src/ng-xform/ng-xform-save-edit/ng-xform-save-edit.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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { NgXformComponent } from '../ng-xform.component'; | ||
import { | ||
Component, | ||
Input, | ||
EventEmitter, | ||
Output, | ||
ViewChild | ||
} from '@angular/core'; | ||
|
||
import { DynamicField } from '../fields/dynamic-field'; | ||
|
||
/** | ||
* This component builds a form that switches from editable/non-editable mode. | ||
* | ||
* :editing: Flag to control components state | ||
*/ | ||
@Component({ | ||
selector: 'ng-xform-save-edit', | ||
templateUrl: './ng-xform-save-edit.component.html', | ||
styles: [] | ||
}) | ||
export class NgXformSaveEditComponent { | ||
|
||
@Input() fields: DynamicField[]; | ||
@Input() editing = false; | ||
@Input() horizontalForm = false; | ||
@Input() labelWidth: number; | ||
@Output() editingChange = new EventEmitter(); | ||
/** To listening submitSuccessful event */ | ||
@Output() submit = new EventEmitter(); | ||
/** To listening submitSuccessful event */ | ||
@Output() cancel = new EventEmitter(); | ||
|
||
@ViewChild(NgXformComponent) xform: NgXformComponent; | ||
|
||
private currentModel: any; | ||
|
||
setEditing(editing: boolean) { | ||
if (editing) { | ||
this.currentModel = this.xform.getModel(); | ||
} | ||
this.editing = editing; | ||
} | ||
|
||
onSubmit() { | ||
if (this.xform.form.invalid) { | ||
return; | ||
} | ||
this.submit.emit(this.xform.getModel()); | ||
} | ||
|
||
onCancel() { | ||
this.cancel.emit(); | ||
this.xform.setValue(this.currentModel); | ||
this.editing = false; | ||
} | ||
|
||
setValue(newModel: any) { | ||
this.xform.setValue(newModel); | ||
} | ||
} |
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,16 +1,3 @@ | ||
<div> | ||
<form [formGroup]="form" [class.form-horizontal]="horizontalForm"> | ||
<ng-xform-form-group [isHorizontal]="horizontalForm" [labelWidth]="labelWidth" [fields]='fields' [form]=form [editing]='editing'></ng-xform-form-group> | ||
</form> | ||
</div> | ||
<div class="row"> | ||
<div class=" col-sm-12 text-right"> | ||
<button type="submit" class="btn btn-primary" (click)="setEditing(true)" *ngIf="editing === false"> | ||
<span class="fa fa-pencil"></span> Edit | ||
</button> | ||
<ng-template [ngIf]="editing !== false"> | ||
<button [disabled]="form.invalid" type="submit" id="formSubmitBtn" class="btn btn-primary" (click)="submit()">Save</button> | ||
<button type="button" class="btn btn-default" (click)="cancel()">Cancel</button> | ||
</ng-template> | ||
</div> | ||
</div> | ||
<form [formGroup]="form" [class.form-horizontal]="horizontalForm"> | ||
<ng-xform-form-group [isHorizontal]="horizontalForm" [labelWidth]="labelWidth" [fields]='fields' [form]=form [editing]='editing'></ng-xform-form-group> | ||
</form> |
Oops, something went wrong.