Skip to content

Commit

Permalink
fix(autocomplete): put back closed and opened event emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
atlwendy authored and benjamincharity committed Jun 10, 2019
1 parent e1ff50d commit 7c2a059
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
14 changes: 14 additions & 0 deletions terminus-ui/autocomplete/src/autocomplete.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>(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`, () => {
Expand Down Expand Up @@ -453,6 +466,7 @@ describe(`TsAutocompleteComponent`, function() {
fixture.detectChanges();

expect(triggerInstance.panelOpen).toEqual(false);
expect(instance.panelOpen).toEqual(false);
});


Expand Down
49 changes: 45 additions & 4 deletions terminus-ui/autocomplete/src/autocomplete.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,21 @@ export class TsAutocompleteChange<T = string[] | string> {
*
* @example
* <ts-autocomplete
* [allowMultiple]="allowMultiple"
* debounceDelay="300"
* displayWith="(v) => v.name"
* hint="Begin typing to search.."
* label="Select options:"
* [allowMultiple]="allowMultiple"
* name="product selections"
* options="[{}, {}, ...]"
* [showProgress]="inProgress"
* theme="primary"
* (closed)="panelWasClosed($event)"
* (opened)="panelWasOpened($event)"
* (optionSelected)="mySelected($event)"
* (optionRemoved)="myRemoved($event)"
* (selection)="mySelection($event)"
* (query)="myQuery($event)"
* (selection)="mySelection($event)"
* ></ts-autocomplete>
*
* <example-url>https://getterminus.github.io/ui-demos-release/components/autocomplete</example-url>
Expand Down Expand Up @@ -455,12 +457,24 @@ export class TsAutocompleteComponent implements OnInit,
* EMITTERS
*/

/**
* Event for when the panel is closed
*/
@Output()
public readonly closed: EventEmitter<void> = new EventEmitter();

/**
* Event for when a duplicate selection is made
*/
@Output()
public readonly duplicateSelection: EventEmitter<TsAutocompleteChange> = new EventEmitter();

/**
* Event for when the panel is opened
*/
@Output()
public readonly opened: EventEmitter<void> = new EventEmitter();

/**
* Emit the selected chip
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
*
Expand Down Expand Up @@ -745,14 +784,15 @@ 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
if (event['relatedTarget'].nodeName !== 'TS-OPTION') {
this.resetAutocompleteQuery();
}
} else if (this.autocompleteTrigger.panelOpen) {
this.close();
this.autocompleteTrigger.closePanel(true);
}

Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 7c2a059

Please sign in to comment.