Skip to content

Commit

Permalink
fix(dropdown): change to contains
Browse files Browse the repository at this point in the history
- Change to check if event target is contained in toggle to catch all child element clicks

Fixes #803
Closes #810
  • Loading branch information
wesleycho committed Sep 29, 2016
1 parent cb0e038 commit fdf8d4b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/dropdown/dropdown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,30 @@ describe('ngb-dropdown-toggle', () => {
expect(buttonEl.getAttribute('aria-expanded')).toBe('false');
});

it('should toggle dropdown on click of child of toggle', () => {
const html = `
<div ngbDropdown>
<button ngbDropdownToggle>
<span class="toggle">Toggle dropdown</span>
</button>
</div>`;

const fixture = createTestComponent(html);
const compiled = fixture.nativeElement;
let dropdownEl = getDropdownEl(compiled);
let toggleEl = compiled.querySelector('.toggle');

expect(dropdownEl).not.toHaveCssClass('open');

toggleEl.click();
fixture.detectChanges();
expect(dropdownEl).toHaveCssClass('open');

toggleEl.click();
fixture.detectChanges();
expect(dropdownEl).not.toHaveCssClass('open');
});

it('should close on outside click', () => {
const html = `<button>Outside</button><div ngbDropdown [open]="true"></div>`;

Expand Down
2 changes: 1 addition & 1 deletion src/dropdown/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class NgbDropdown {
*/
set toggleElement(toggleElement: any) { this._toggleElement = toggleElement; }

private _isEventFromToggle($event) { return $event.target === this._toggleElement; }
private _isEventFromToggle($event) { return !!this._toggleElement && this._toggleElement.contains($event.target); }
}

/**
Expand Down

0 comments on commit fdf8d4b

Please sign in to comment.