Skip to content

Commit

Permalink
fix(select, autocomplete): don't open overlay without options (#2930)
Browse files Browse the repository at this point in the history
  • Loading branch information
katebatura committed Nov 17, 2021
1 parent 9ccec64 commit fca5cd1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export class NbAutocompleteDirective<T> implements OnDestroy, AfterViewInit, Con
}

show() {
if (this.isClosed) {
if (this.shouldShow()) {
this.attachToOverlay();
this.setActiveItem();
}
Expand Down Expand Up @@ -426,4 +426,8 @@ export class NbAutocompleteDirective<T> implements OnDestroy, AfterViewInit, Con
protected createScrollStrategy(): NbScrollStrategy {
return this.overlay.scrollStrategies[this.scrollStrategy]();
}

protected shouldShow(): boolean {
return this.isClosed && this.autocomplete.options.length > 0;
}
}
6 changes: 5 additions & 1 deletion src/framework/theme/components/select/select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ export class NbSelectComponent
}

show() {
if (this.isHidden) {
if (this.shouldShow()) {
this.attachToOverlay();

this.positionStrategy.positionChange.pipe(take(1), takeUntil(this.destroy$)).subscribe(() => {
Expand Down Expand Up @@ -1220,6 +1220,10 @@ export class NbSelectComponent
}
}

protected shouldShow(): boolean {
return this.isHidden && this.options?.length > 0;
}

/**
* Sets touched if focus moved outside of button and overlay,
* ignoring the case when focus moved to options overlay.
Expand Down
4 changes: 2 additions & 2 deletions src/framework/theme/components/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,8 @@ describe('Component: NbSelectComponent', () => {
it('should not mark touched when select button loose focus and select open', fakeAsync(() => {
const touchedSpy = jasmine.createSpy('touched spy');

const selectFixture = TestBed.createComponent(NbSelectComponent);
select = selectFixture.componentInstance as NbSelectComponent;
const selectFixture = TestBed.createComponent(NbSelectTestComponent);
select = selectFixture.debugElement.query(By.directive(NbSelectComponent)).componentInstance;
selectFixture.detectChanges();
flush();

Expand Down

0 comments on commit fca5cd1

Please sign in to comment.