Skip to content

Commit

Permalink
feat(dropdown): implement "nonInput" auto-close mode
Browse files Browse the repository at this point in the history
… because that’s what [the original Boostrap 4 code does](https://github.com/twbs/bootstrap/blob/a1bf344c4f041ad88acaf5b2b3777c733d3afe40/js/src/dropdown.js#L174-L176).

- Add NONINPUT constant and copy Bootstrap’s behavior
- Change the default auto-close behaviour to `NONINPUT`
- Add `nonInput` to documentation / readme
  • Loading branch information
rluba committed Feb 10, 2016
1 parent eec3cb4 commit 94d9909
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 2 additions & 2 deletions components/dropdown/dropdown.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Query, QueryList
} from 'angular2/core';

import {dropdownService, ALWAYS} from './dropdown.service';
import {dropdownService, NONINPUT} from './dropdown.service';

@Directive({selector: '[dropdown]'})
export class Dropdown implements OnInit, OnDestroy {
Expand Down Expand Up @@ -56,7 +56,7 @@ export class Dropdown implements OnInit, OnDestroy {
}

ngOnInit() {
this.autoClose = this.autoClose || ALWAYS;
this.autoClose = this.autoClose || NONINPUT;
if (this.isOpen) {
// todo: watch for event get-isOpen?
}
Expand Down
8 changes: 8 additions & 0 deletions components/dropdown/dropdown.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const ALWAYS = 'always';
export const DISABLED = 'disabled';
export const OUTSIDECLICK = 'outsideClick';
export const NONINPUT = 'nonInput';

import {Dropdown} from './dropdown.directive';

Expand Down Expand Up @@ -48,6 +49,13 @@ export class DropdownService {
return;
}

if (event && this.openScope.autoClose === NONINPUT &&
this.openScope.menuEl &&
/input|textarea/i.test((<any> event.target).tagName) &&
this.openScope.menuEl.nativeElement.contains(event.target)) {
return;
}

if (event && this.openScope.autoClose === OUTSIDECLICK &&
this.openScope.menuEl &&
this.openScope.menuEl.nativeElement === event.target) {
Expand Down
5 changes: 3 additions & 2 deletions components/dropdown/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Dropdown implements OnInit, OnDestroy {
@Input() public get isOpen():boolean {}
@Input() public autoClose:string;
@Input() public keyboardNav:boolean;
// enum string: ['always', 'outsideClick', 'disabled']
// enum string: ['nonInput', always', 'outsideClick', 'disabled']
@Input() public appendToBody:boolean;
@Output() public onToggle:EventEmitter<boolean>;
}
Expand All @@ -47,7 +47,8 @@ export const DROPDOWN_DIRECTIVES: Array<any> = [Dropdown, DropdownMenu, Dropdown
### Dropdown properties
- `isOpen` (`?boolean=false`) - if `true` dropdown will be opened
- `autoClose` (`?string='always'`) - behaviour vary:
* `always` - (default) automatically closes the dropdown when any of its elements is clicked
* `nonInput` - (default) automatically closes the dropdown when any of its elements is clicked — as long as the clicked element is not an `input` or a `textarea`.
* `always` - automatically closes the dropdown when any of its elements is clicked
* `outsideClick` - closes the dropdown automatically only when the user clicks any element outside the dropdown
* `disabled` - disables the auto close. You can then control the open/close status of the dropdown manually, by using `is-open`. Please notice that the dropdown will still close if the toggle is clicked, the `esc` key is pressed or another dropdown is open
- `keyboardNav` (`?boolean=false`) - if `true` will enable navigation of dropdown list elements with the arrow keys
Expand Down

0 comments on commit 94d9909

Please sign in to comment.