Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Added blur event to select-field #11

Merged
merged 9 commits into from
Feb 20, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
[singleSelectPlaceholderText]="singleSelectPlaceholderText"
[pickerHeading]="pickerHeading"
[ngModel]="formData.modelValue"
(ngModelChange)="onModelChange($event)">
(blur)="onTouch()"
blackbaud-conorwright marked this conversation as resolved.
Show resolved Hide resolved
(ngModelChange)="onModelChange($event)"
>
</sky-select-field>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class SkySelectFieldTestComponent implements OnInit, OnDestroy {
public singleSelectOpenButtonTitle: string;
public singleSelectPlaceholderText: string;
public pickerHeading: string;
public touched = false;

public data = new BehaviorSubject<any[]>([]);

Expand Down Expand Up @@ -58,4 +59,8 @@ export class SkySelectFieldTestComponent implements OnInit, OnDestroy {
public setValue(value: any) {
this.formData.modelValue = value;
}

public onTouch() {
this.touched = true;
}
}
10 changes: 10 additions & 0 deletions src/app/public/modules/select-field/select-field.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ describe('Select field component', () => {
expect(selectField.singleSelectPlaceholderText).toEqual('placeholder');
expect(selectField.pickerHeading).toEqual('heading');
});

it('should trigger blur event onTouch', fakeAsync(() => {
fixture.detectChanges();
setValue(undefined);
openPicker();
fixture.detectChanges();
closePicker();
fixture.detectChanges();
expect(component.touched).toBeTruthy();
}));
});

describe('multiple select', () => {
Expand Down
19 changes: 15 additions & 4 deletions src/app/public/modules/select-field/select-field.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
ChangeDetectorRef,
Component,
forwardRef,
Input
Input,
EventEmitter,
Output
} from '@angular/core';

import {
Expand Down Expand Up @@ -100,6 +102,9 @@ export class SkySelectFieldComponent implements ControlValueAccessor {
@Input()
public pickerHeading: string;

@Output()
public blur = new EventEmitter();
blackbaud-conorwright marked this conversation as resolved.
Show resolved Hide resolved

public get value(): any {
return this._value;
}
Expand Down Expand Up @@ -196,18 +201,21 @@ export class SkySelectFieldComponent implements ControlValueAccessor {
}
}

public onTouched(): void {
this._onTouched();
this.blur.emit();
}

// Angular automatically constructs these methods.
/* istanbul ignore next */
public onChange = (value: any) => { };
/* istanbul ignore next */
public onTouched = () => { };
blackbaud-conorwright marked this conversation as resolved.
Show resolved Hide resolved

public registerOnChange(fn: (value: any) => void) {
this.onChange = fn;
}

public registerOnTouched(fn: () => void) {
this.onTouched = fn;
this._onTouched = fn;
}

public setDisabledState(disabled: boolean) {
Expand All @@ -219,6 +227,9 @@ export class SkySelectFieldComponent implements ControlValueAccessor {
this.value = undefined;
}

/* istanbul ignore next */
private _onTouched = () => { };

private setTokensFromValue() {
// Tokens only appear for multiple select mode.
if (this.selectMode === 'single') {
Expand Down