-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mgr/dashboard: Add submit button component
This component is to be used inside a form. It will render a submit button with the given label. The button will disabled itself and show a loading icon when the user clicks it, usually initiating a request to the server, and it will stay in that state until the request is finished. To indicate that the request failed, returning the button to the enable state, you need to insert an error in the form with the 'cdSubmitButton' key. It will also check if the form is valid, when clicking the button, and will focus on the first invalid input. Signed-off-by: Tiago Melo <tmelo@suse.com>
- Loading branch information
Tiago Melo
committed
Mar 23, 2018
1 parent
44f16c9
commit 70f8f61
Showing
5 changed files
with
110 additions
and
2 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
9 changes: 9 additions & 0 deletions
9
...r/dashboard/frontend/src/app/shared/components/submit-button/submit-button.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,9 @@ | ||
<button type="submit" | ||
class="btn btn-sm btn-primary tc_submitButton" | ||
[disabled]="loading" | ||
(click)="submit()"> | ||
<ng-content></ng-content> | ||
<span *ngIf="loading"> | ||
<i class="fa fa-spinner fa-spin fa-fw"></i> | ||
</span> | ||
</button> |
Empty file.
30 changes: 30 additions & 0 deletions
30
...ashboard/frontend/src/app/shared/components/submit-button/submit-button.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,30 @@ | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { FormGroup } from '@angular/forms'; | ||
|
||
import { SubmitButtonComponent } from './submit-button.component'; | ||
|
||
describe('SubmitButtonComponent', () => { | ||
let component: SubmitButtonComponent; | ||
let fixture: ComponentFixture<SubmitButtonComponent>; | ||
|
||
beforeEach( | ||
async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [SubmitButtonComponent] | ||
}).compileComponents(); | ||
}) | ||
); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(SubmitButtonComponent); | ||
component = fixture.componentInstance; | ||
|
||
component.form = new FormGroup({}, {}); | ||
|
||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
66 changes: 66 additions & 0 deletions
66
...mgr/dashboard/frontend/src/app/shared/components/submit-button/submit-button.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,66 @@ | ||
import { Component, ElementRef, EventEmitter, Input, OnInit, Output } from '@angular/core'; | ||
import { FormGroup } from '@angular/forms'; | ||
|
||
import * as _ from 'lodash'; | ||
|
||
/** | ||
* This component will render a submit button with the given label. | ||
* | ||
* The button will disabled itself and show a loading icon when the user clicks | ||
* it, usually initiating a request to the server, and it will stay in that | ||
* state until the request is finished. | ||
* | ||
* To indicate that the request failed, returning the button to the enable | ||
* state, you need to insert an error in the form with the 'cdSubmitButton' key. | ||
* p.e.: this.rbdForm.setErrors({'cdSubmitButton': true}); | ||
* | ||
* It will also check if the form is valid, when clicking the button, and will | ||
* focus on the first invalid input. | ||
* | ||
* @export | ||
* @class SubmitButtonComponent | ||
* @implements {OnInit} | ||
*/ | ||
@Component({ | ||
selector: 'cd-submit-button', | ||
templateUrl: './submit-button.component.html', | ||
styleUrls: ['./submit-button.component.scss'] | ||
}) | ||
export class SubmitButtonComponent implements OnInit { | ||
@Input() form: FormGroup; | ||
@Output() submitAction = new EventEmitter(); | ||
|
||
loading = false; | ||
|
||
constructor(private elRef: ElementRef) {} | ||
|
||
ngOnInit() { | ||
this.form.statusChanges.subscribe(() => { | ||
if (_.has(this.form.errors, 'cdSubmitButton')) { | ||
this.loading = false; | ||
_.unset(this.form.errors, 'cdSubmitButton'); | ||
this.form.updateValueAndValidity(); | ||
} | ||
}); | ||
} | ||
|
||
submit() { | ||
if (this.form.invalid) { | ||
this.focusInvalid(); | ||
return; | ||
} | ||
|
||
this.loading = true; | ||
this.submitAction.emit(); | ||
} | ||
|
||
focusInvalid() { | ||
const target = this.elRef.nativeElement.offsetParent.querySelector( | ||
'input.ng-invalid, select.ng-invalid' | ||
); | ||
|
||
if (target) { | ||
target.focus(); | ||
} | ||
} | ||
} |