From 7c2a059b7e22637eeeb507e3246e39865c657423 Mon Sep 17 00:00:00 2001 From: Wendy Yang Date: Fri, 7 Jun 2019 16:52:23 -0400 Subject: [PATCH] fix(autocomplete): put back closed and opened event emitter --- .../src/autocomplete.component.spec.ts | 14 ++++++ .../src/autocomplete.component.ts | 49 +++++++++++++++++-- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/terminus-ui/autocomplete/src/autocomplete.component.spec.ts b/terminus-ui/autocomplete/src/autocomplete.component.spec.ts index b40be39a4..ec402436d 100644 --- a/terminus-ui/autocomplete/src/autocomplete.component.spec.ts +++ b/terminus-ui/autocomplete/src/autocomplete.component.spec.ts @@ -111,6 +111,19 @@ describe(`TsAutocompleteComponent`, function() { expect(autocomplete.classList).toContain('ts-autocomplete--disabled'); }); + test(`should not open when disabled`, () => { + const fixture = createComponent(testComponents.Disabled); + fixture.detectChanges(); + fixture.componentInstance.wasOpened = jest.fn(); + const trigger = getAutocompleteTriggerElement(fixture); + dispatchMouseEvent(trigger, 'click'); + fixture.detectChanges(); + const instance = getAutocompleteInstance(fixture); + + expect(fixture.componentInstance.wasOpened).not.toHaveBeenCalled(); + expect(instance.panelOpen).toEqual(false); + }); + describe(`chips`, function() { test(`should show selections as chips`, () => { @@ -453,6 +466,7 @@ describe(`TsAutocompleteComponent`, function() { fixture.detectChanges(); expect(triggerInstance.panelOpen).toEqual(false); + expect(instance.panelOpen).toEqual(false); }); diff --git a/terminus-ui/autocomplete/src/autocomplete.component.ts b/terminus-ui/autocomplete/src/autocomplete.component.ts index bd8beab1d..9e2f05911 100644 --- a/terminus-ui/autocomplete/src/autocomplete.component.ts +++ b/terminus-ui/autocomplete/src/autocomplete.component.ts @@ -94,19 +94,21 @@ export class TsAutocompleteChange { * * @example * * * https://getterminus.github.io/ui-demos-release/components/autocomplete @@ -455,12 +457,24 @@ export class TsAutocompleteComponent implements OnInit, * EMITTERS */ + /** + * Event for when the panel is closed + */ + @Output() + public readonly closed: EventEmitter = new EventEmitter(); + /** * Event for when a duplicate selection is made */ @Output() public readonly duplicateSelection: EventEmitter = new EventEmitter(); + /** + * Event for when the panel is opened + */ + @Output() + public readonly opened: EventEmitter = new EventEmitter(); + /** * Emit the selected chip */ @@ -576,6 +590,9 @@ export class TsAutocompleteComponent implements OnInit, const queryIsValid = (query === inputValue) || (query === ''); this.queryChange.emit(queryIsValid ? query : inputValue); + if (!this.panelOpen) { + this.open(); + } }); // Propagate changes from form control @@ -631,7 +648,19 @@ export class TsAutocompleteComponent implements OnInit, // istanbul ignore next public onTouched = () => { }; - + /** + * Close the overlay panel + */ + public close(): void { + if (this.autocompleteTrigger.panelOpen) { + this.panelOpen = false; + this.changeDetectorRef.markForCheck(); + this.onTouched(); + this.updateValueAndValidity(); + // Alert the consumer + this.closed.emit(); + } + } /** * Set up a key manager to listen to keyboard events on the overlay panel @@ -654,6 +683,16 @@ export class TsAutocompleteComponent implements OnInit, this.inputElement.nativeElement.focus(); } + /** + * Open the overlay panel + */ + public open(): void { + if (this.isDisabled || !this.options || !this.options.length || this.panelOpen) { + return; + } + this.opened.emit(); + } + /** * Emit a change event to set the model value * @@ -745,7 +784,7 @@ export class TsAutocompleteComponent implements OnInit, if (hasRelatedTarget && hasNodeName) { // If the blur event comes from the user clicking an option, `event.relatedTarget.nodeName` - // will be `TS_SELECT_OPTION`. + // will be `TS-OPTION`. // istanbul ignore else // NOTE: TypeScript warns `Property 'nodeName' does not exist on type 'EventTarget'.` // eslint-disable-next-line dot-notation @@ -753,6 +792,7 @@ export class TsAutocompleteComponent implements OnInit, this.resetAutocompleteQuery(); } } else if (this.autocompleteTrigger.panelOpen) { + this.close(); this.autocompleteTrigger.closePanel(true); } @@ -801,6 +841,7 @@ export class TsAutocompleteComponent implements OnInit, // If supporting multiple selections, reset the input text value as long as the panel should NOT reopen // istanbul ignore else if (!this.reopenAfterSelection) { + this.close(); this.resetAutocompleteQuery(); }