-
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): options search #3091
Changes from 12 commits
86475f5
e99e689
697f8dc
80f42bc
212b94e
ab0465d
c9a6589
74683b7
c7547b0
51fdbd5
fee9d3e
b0f58d4
40b6ef2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ | |
|
||
:host { | ||
display: flex; | ||
&[hidden] { | ||
display: none; | ||
} | ||
|
||
&:hover { | ||
cursor: pointer; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ import { | |
} from '@angular/core'; | ||
import { NgClass } from '@angular/common'; | ||
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; | ||
import { merge, Subject, BehaviorSubject, from } from 'rxjs'; | ||
import { merge, Subject, BehaviorSubject, from, combineLatest } from 'rxjs'; | ||
import { startWith, switchMap, takeUntil, filter, map, finalize, take } from 'rxjs/operators'; | ||
|
||
import { NbStatusService } from '../../services/status.service'; | ||
|
@@ -697,6 +697,18 @@ export class NbSelectComponent | |
**/ | ||
@Input() scrollStrategy: NbScrollStrategies = 'block'; | ||
|
||
/** | ||
* Experimental input. | ||
* Could be changed without any prior notice. | ||
* Use at your own risk. | ||
* | ||
* It replaces the button with input when the select is opened. | ||
* That replacement provides a very basic API to implement options filtering functionality. | ||
* Filtering itself isn't implemented inside select. | ||
* So it should be implemented by the user. | ||
*/ | ||
@Input() withOptionSearch: boolean = false; | ||
|
||
@HostBinding('class') | ||
get additionalClasses(): string[] { | ||
if (this.statusService.isCustomStatus(this.status)) { | ||
|
@@ -709,6 +721,9 @@ export class NbSelectComponent | |
* Will be emitted when selected value changes. | ||
* */ | ||
@Output() selectedChange: EventEmitter<any> = new EventEmitter(); | ||
@Output() selectOpen: EventEmitter<void> = new EventEmitter(); | ||
@Output() selectClose: EventEmitter<void> = new EventEmitter(); | ||
@Output() optionSearchChange: EventEmitter<string> = new EventEmitter(); | ||
|
||
/** | ||
* List of `NbOptionComponent`'s components passed as content. | ||
|
@@ -726,7 +741,8 @@ export class NbSelectComponent | |
* */ | ||
@ViewChild(NbPortalDirective) portal: NbPortalDirective; | ||
|
||
@ViewChild('selectButton', { read: ElementRef }) button: ElementRef<HTMLButtonElement>; | ||
@ViewChild('selectButton', { read: ElementRef }) button: ElementRef<HTMLButtonElement> | undefined; | ||
@ViewChild('optionSearchInput', { read: ElementRef }) optionSearchInput: ElementRef<HTMLInputElement> | undefined; | ||
|
||
/** | ||
* Determines is select opened. | ||
|
@@ -736,6 +752,10 @@ export class NbSelectComponent | |
return this.ref && this.ref.hasAttached(); | ||
} | ||
|
||
get isOptionSearchInputAllowed(): boolean { | ||
return this.withOptionSearch && this.isOpen && !this.multiple; | ||
} | ||
|
||
/** | ||
* List of selected options. | ||
* */ | ||
|
@@ -822,6 +842,9 @@ export class NbSelectComponent | |
* Returns width of the select button. | ||
* */ | ||
get hostWidth(): number { | ||
if (this.isOptionSearchInputAllowed) { | ||
return this.optionSearchInput.nativeElement.getBoundingClientRect().width; | ||
} | ||
return this.button.nativeElement.getBoundingClientRect().width; | ||
} | ||
|
||
|
@@ -849,7 +872,7 @@ export class NbSelectComponent | |
return this.selectionModel.map((option: NbOptionComponent) => option.content).join(', '); | ||
} | ||
|
||
return this.selectionModel[0].content; | ||
return this.selectionModel[0]?.content ?? ''; | ||
} | ||
|
||
ngOnChanges({ disabled, status, size, fullWidth }: SimpleChanges) { | ||
|
@@ -911,14 +934,24 @@ export class NbSelectComponent | |
} | ||
} | ||
|
||
onInput(event: Event) { | ||
this.optionSearchChange.emit((event.target as HTMLInputElement).value); | ||
} | ||
|
||
show() { | ||
if (this.shouldShow()) { | ||
this.attachToOverlay(); | ||
|
||
this.positionStrategy.positionChange.pipe(take(1), takeUntil(this.destroy$)).subscribe(() => { | ||
this.setActiveOption(); | ||
if (this.isOptionSearchInputAllowed) { | ||
this.optionSearchInput.nativeElement.focus(); | ||
} else { | ||
this.setActiveOption(); | ||
} | ||
}); | ||
|
||
this.selectOpen.emit(); | ||
|
||
this.cd.markForCheck(); | ||
} | ||
} | ||
|
@@ -927,6 +960,10 @@ export class NbSelectComponent | |
if (this.isOpen) { | ||
this.ref.detach(); | ||
this.cd.markForCheck(); | ||
this.selectClose.emit(); | ||
|
||
this.optionSearchInput.nativeElement.value = this.selectionView; | ||
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. why don't we use FormControl for optionSearchInput? 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.
|
||
this.optionSearchChange.emit(''); | ||
} | ||
} | ||
|
||
|
@@ -1061,8 +1098,11 @@ export class NbSelectComponent | |
} | ||
|
||
protected createPositionStrategy(): NbAdjustableConnectedPositionStrategy { | ||
const element: ElementRef<HTMLInputElement | HTMLButtonElement> = this.withOptionSearch | ||
? this.optionSearchInput | ||
: this.button; | ||
return this.positionBuilder | ||
.connectedTo(this.button) | ||
.connectedTo(element) | ||
.position(NbPosition.BOTTOM) | ||
.offset(this.optionsOverlayOffset) | ||
.adjustment(NbAdjustment.VERTICAL); | ||
|
@@ -1123,9 +1163,9 @@ export class NbSelectComponent | |
) | ||
.subscribe((event: KeyboardEvent) => { | ||
if (event.keyCode === ESCAPE) { | ||
this.button.nativeElement.focus(); | ||
this.hide(); | ||
} else { | ||
this.button.nativeElement.focus(); | ||
} else if (!this.isOptionSearchInputAllowed) { | ||
this.keyManager.onKeydown(event); | ||
} | ||
}); | ||
|
@@ -1137,11 +1177,21 @@ export class NbSelectComponent | |
} | ||
|
||
protected subscribeOnButtonFocus() { | ||
this.focusMonitor | ||
.monitor(this.button) | ||
const buttonFocus$ = this.focusMonitor.monitor(this.button).pipe( | ||
map((origin) => !!origin), | ||
startWith(false), | ||
finalize(() => this.focusMonitor.stopMonitoring(this.button)), | ||
); | ||
|
||
const filterInputFocus$ = this.focusMonitor.monitor(this.optionSearchInput).pipe( | ||
map((origin) => !!origin), | ||
startWith(false), | ||
finalize(() => this.focusMonitor.stopMonitoring(this.button)), | ||
); | ||
|
||
combineLatest([buttonFocus$, filterInputFocus$]) | ||
.pipe( | ||
map((origin) => !!origin), | ||
finalize(() => this.focusMonitor.stopMonitoring(this.button)), | ||
map(([buttonFocus, filterInputFocus]) => buttonFocus || filterInputFocus), | ||
takeUntil(this.destroy$), | ||
) | ||
.subscribe(this.focused$); | ||
|
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.
why is it hidden? why not *ngIf?
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.
Just simpler. Nebular subscribes to some button events like focus. And with
*ngIf
we should implement unsubscription and resubscription mechanisms. Looks too complicated for now