-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(select): keyboard support #1417
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4c3bc6f
feat: add ckd key manager mapping
yggg cdf1278
feat(select): add keyboard support
yggg b4f6bd1
refactor(option): move option text in multiple select to option
yggg 4a549e4
feat(option): align checkbox with option text
yggg 4fa811b
Merge branch 'next' into feat/select-keyboard-support
yggg 1ad04ab
style: add semicolon
yggg 2544ab9
Merge remote-tracking branch 'origin/feat/select-keyboard-support' in…
yggg 1784cda
build: include keycodes into bundle
yggg 8166853
test(select): update pass event to onclick calls
yggg 2cbccbe
Merge branch 'next' into feat/select-keyboard-support
yggg 17f2e1b
Merge branch 'next' into feat/select-keyboard-support
yggg c8822b2
refactor(select): extract conditional code into separate methods
yggg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { FocusableOption, FocusKeyManager } from '@angular/cdk/a11y'; | ||
|
||
export type NbFocusableOption = FocusableOption; | ||
export class NbFocusKeyManager<T> extends FocusKeyManager<T> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
export * from './overlay'; | ||
export * from './a11y/focus-key-manager'; | ||
export * from './a11y/a11y.module'; | ||
export * from './a11y/focus-trap'; | ||
export * from './adapter/overlay-container-adapter'; | ||
export * from './adapter/scroll-dispatcher-adapter'; | ||
export * from './adapter/viewport-ruler-adapter'; | ||
export * from './keycodes/keycodes'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '@angular/cdk/keycodes'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,14 +20,13 @@ import { | |
Inject, | ||
Input, | ||
OnDestroy, | ||
OnInit, | ||
Output, | ||
QueryList, | ||
ViewChild, | ||
} from '@angular/core'; | ||
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; | ||
import { merge, Observable, Subject } from 'rxjs'; | ||
import { startWith, switchMap, takeWhile } from 'rxjs/operators'; | ||
import { startWith, switchMap, takeWhile, filter } from 'rxjs/operators'; | ||
|
||
import { | ||
NbAdjustableConnectedPositionStrategy, | ||
|
@@ -45,8 +44,9 @@ import { | |
import { NbComponentSize } from '../component-size'; | ||
import { NbComponentShape } from '../component-shape'; | ||
import { NbComponentStatus } from '../component-status'; | ||
import { NbOptionComponent } from './option.component'; | ||
import { NB_DOCUMENT } from '../../theme.options'; | ||
import { NbFocusKeyManager, ESCAPE } from '../cdk'; | ||
import { NbOptionComponent } from './option.component'; | ||
import { convertToBoolProperty } from '../helpers'; | ||
|
||
export type NbSelectAppearance = 'outline' | 'filled' | 'hero'; | ||
|
@@ -386,7 +386,8 @@ export class NbSelectLabelComponent { | |
}, | ||
], | ||
}) | ||
export class NbSelectComponent<T> implements OnInit, AfterViewInit, AfterContentInit, OnDestroy, ControlValueAccessor { | ||
export class NbSelectComponent<T> implements AfterViewInit, AfterContentInit, OnDestroy, ControlValueAccessor { | ||
|
||
/** | ||
* Select size, available sizes: | ||
* `tiny`, `small`, `medium` (default), `large`, `giant` | ||
|
@@ -565,6 +566,8 @@ export class NbSelectComponent<T> implements OnInit, AfterViewInit, AfterContent | |
|
||
protected alive: boolean = true; | ||
|
||
protected keyManager: NbFocusKeyManager<NbOptionComponent<T>>; | ||
|
||
/** | ||
* If a user assigns value before content nb-options's rendered the value will be putted in this variable. | ||
* And then applied after content rendered. | ||
|
@@ -634,10 +637,6 @@ export class NbSelectComponent<T> implements OnInit, AfterViewInit, AfterContent | |
return this.selectionModel[0].content; | ||
} | ||
|
||
ngOnInit() { | ||
this.createOverlay(); | ||
} | ||
|
||
ngAfterContentInit() { | ||
if (this.queue) { | ||
// Call 'writeValue' when current change detection run is finished. | ||
|
@@ -655,7 +654,6 @@ export class NbSelectComponent<T> implements OnInit, AfterViewInit, AfterContent | |
this.triggerStrategy = this.createTriggerStrategy(); | ||
|
||
this.subscribeOnTriggers(); | ||
this.subscribeOnPositionChange(); | ||
this.subscribeOnOptionClick(); | ||
} | ||
|
||
|
@@ -672,7 +670,19 @@ export class NbSelectComponent<T> implements OnInit, AfterViewInit, AfterContent | |
|
||
show() { | ||
if (this.isHidden) { | ||
if (!this.ref) { | ||
this.createOverlay(); | ||
this.subscribeOnPositionChange(); | ||
this.createKeyManager(); | ||
this.subscribeOnOverlayKeys(); | ||
} | ||
|
||
this.ref.attach(this.portal); | ||
if (this.selectionModel.length) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not put if in if. Please, move in the separate function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed c8822b2 |
||
this.keyManager.setActiveItem(this.selectionModel[0]); | ||
} else { | ||
this.keyManager.setFirstItemActive(); | ||
} | ||
this.cd.markForCheck(); | ||
} | ||
} | ||
|
@@ -783,6 +793,10 @@ export class NbSelectComponent<T> implements OnInit, AfterViewInit, AfterContent | |
this.ref = this.overlay.create({ positionStrategy: this.positionStrategy, scrollStrategy }); | ||
} | ||
|
||
protected createKeyManager(): void { | ||
this.keyManager = new NbFocusKeyManager<NbOptionComponent<T>>(this.options).withTypeAhead(200); | ||
} | ||
|
||
protected createPositionStrategy(): NbAdjustableConnectedPositionStrategy { | ||
return this.positionBuilder | ||
.connectedTo(this.hostRef) | ||
|
@@ -842,6 +856,26 @@ export class NbSelectComponent<T> implements OnInit, AfterViewInit, AfterContent | |
}); | ||
} | ||
|
||
protected subscribeOnOverlayKeys(): void { | ||
this.ref.keydownEvents() | ||
.pipe( | ||
takeWhile(() => this.alive), | ||
filter(() => this.isOpen), | ||
) | ||
.subscribe((event: KeyboardEvent) => { | ||
if (event.keyCode === ESCAPE) { | ||
this.button.nativeElement.focus(); | ||
this.hide(); | ||
} else { | ||
this.keyManager.onKeydown(event); | ||
} | ||
}); | ||
|
||
this.keyManager.tabOut | ||
.pipe(takeWhile(() => this.alive)) | ||
.subscribe(() => this.hide()); | ||
} | ||
|
||
protected getContainer() { | ||
return this.ref && this.ref.hasAttached() && <ComponentRef<any>> { | ||
location: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not put if in if. Please, move in the separate function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed c8822b2