Skip to content

Commit

Permalink
fix(module:select): fix virtual scroll hover bug
Browse files Browse the repository at this point in the history
  • Loading branch information
vthinkxie committed Apr 24, 2020
1 parent 83cdc84 commit d241123
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
23 changes: 15 additions & 8 deletions components/select/option-container.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { CdkVirtualScrollViewport } from '@angular/cdk/scrolling';
import {
AfterViewInit,
ChangeDetectionStrategy,
Component,
EventEmitter,
Expand Down Expand Up @@ -71,7 +72,7 @@ import { NzSelectItemInterface, NzSelectModeType } from './select.types';
'[class.ant-select-dropdown]': 'true'
}
})
export class NzOptionContainerComponent implements OnChanges {
export class NzOptionContainerComponent implements OnChanges, AfterViewInit {
@Input() notFoundContent: string | TemplateRef<NzSafeAny> | undefined = undefined;
@Input() menuItemSelectedIcon: TemplateRef<NzSafeAny> | null = null;
@Input() dropdownRender: TemplateRef<NzSafeAny> | null = null;
Expand All @@ -82,7 +83,6 @@ export class NzOptionContainerComponent implements OnChanges {
@Input() matchWidth = true;
@Input() listOfContainerItem: NzSelectItemInterface[] = [];
@Output() readonly itemClick = new EventEmitter<NzSafeAny>();
@Output() readonly itemHover = new EventEmitter<NzSafeAny>();
@Output() readonly scrollToBottom = new EventEmitter<void>();
@ViewChild(CdkVirtualScrollViewport, { static: true }) cdkVirtualScrollViewport: CdkVirtualScrollViewport;
private scrolledIndex = 0;
Expand All @@ -94,8 +94,8 @@ export class NzOptionContainerComponent implements OnChanges {
}

onItemHover(value: NzSafeAny): void {
// TODO: bug when mouse inside the option container & keydown
this.itemHover.emit(value);
// TODO: keydown.enter won't activate this value
this.activatedValue = value;
}

trackValue(_index: number, option: NzSelectItemInterface): NzSafeAny {
Expand All @@ -109,13 +109,20 @@ export class NzOptionContainerComponent implements OnChanges {
}
}

scrollToActivatedValue(): void {
const index = this.listOfContainerItem.findIndex(item => this.compareWith(item.key, this.activatedValue));
if (index < this.scrolledIndex || index >= this.scrolledIndex + this.maxItemLength) {
this.cdkVirtualScrollViewport.scrollToIndex(index || 0);
}
}

ngOnChanges(changes: SimpleChanges): void {
const { listOfContainerItem, activatedValue } = changes;
if (listOfContainerItem || activatedValue) {
const index = this.listOfContainerItem.findIndex(item => this.compareWith(item.key, this.activatedValue));
if (index < this.scrolledIndex || index >= this.scrolledIndex + this.maxItemLength) {
this.cdkVirtualScrollViewport.scrollToIndex(index || 0);
}
this.scrollToActivatedValue();
}
}
ngAfterViewInit(): void {
setTimeout(() => this.scrollToActivatedValue());
}
}
5 changes: 0 additions & 5 deletions components/select/select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ export type NzSelectSizeType = 'large' | 'default' | 'small';
[mode]="nzMode"
(keydown)="onKeyDown($event)"
(itemClick)="onItemClick($event)"
(itemHover)="onItemHover($event)"
(scrollToBottom)="nzScrollToBottom.emit()"
></nz-option-container>
</ng-template>
Expand Down Expand Up @@ -265,10 +264,6 @@ export class NzSelectComponent implements ControlValueAccessor, OnInit, AfterVie
this.clearInput();
}

onItemHover(value: NzSafeAny): void {
this.activatedValue = value;
}

updateListOfContainerItem(): void {
let listOfContainerItem = this.listOfTagAndTemplateItem
.filter(item => !item.nzHide)
Expand Down

0 comments on commit d241123

Please sign in to comment.