Skip to content

Commit 5790778

Browse files
committed
chore(select): Address comments-5 #5911
1 parent a52bc85 commit 5790778

File tree

1 file changed

+25
-38
lines changed

1 file changed

+25
-38
lines changed

projects/igniteui-angular/src/lib/select/select-positioning-strategy.ts

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -69,37 +69,35 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
6969
this.setStyles(contentElement, selectFit, initialCall);
7070
}
7171

72-
public itemIsInvisible(selectFit: SelectFit) {
73-
// selected item is completely invisible
74-
return Math.round(selectFit.itemRect.top * 100) / 100 >=
75-
Math.round(this.select.scrollContainer.getBoundingClientRect().bottom * 100) / 100 ||
76-
Math.round(selectFit.itemRect.bottom * 100) / 100 <=
77-
Math.round(this.select.scrollContainer.getBoundingClientRect().top * 100) / 100 ||
78-
// selected item is partially invisible at ddl bottom
79-
Math.round(selectFit.itemRect.top * 100) / 100 <=
80-
(this.select.scrollContainer.getBoundingClientRect().bottom * 100) / 100 &&
81-
Math.round(selectFit.itemRect.bottom * 100) / 100 >=
82-
(this.select.scrollContainer.getBoundingClientRect().bottom * 100) / 100;
83-
}
84-
8572
private manageScrollToItem(selectFit: SelectFit) {
8673
// Scroll and compensate the item's container position, when the selected item is not visible.
87-
if (this.itemIsInvisible(selectFit)) {
88-
const compensation = this.scrollToItem(selectFit);
89-
this.compensateYScroll(selectFit, compensation);
74+
// selected item is completely invisible
75+
const itemTop = Math.round(selectFit.itemRect.top * 100) / 100;
76+
const itemBottom = Math.round(selectFit.itemRect.bottom * 100) / 100;
77+
const scrollContainerRect = this.select.scrollContainer.getBoundingClientRect();
78+
const scrollContainerBottom = Math.round(scrollContainerRect.bottom * 100) / 100;
79+
const scrollContainerTop = Math.round(scrollContainerRect.top * 100) / 100;
80+
const itemIsNotCompletelyVisible =
81+
itemTop >= scrollContainerBottom || itemBottom <= scrollContainerTop ||
82+
// selected item is partially invisible at ddl bottom
83+
itemTop <= scrollContainerBottom && itemBottom >= scrollContainerBottom;
84+
if (itemIsNotCompletelyVisible) {
85+
const scrollAmount = selectFit.itemElement ? Math.floor(itemBottom - scrollContainerBottom) : 0;
86+
this.scrollToItem(scrollAmount);
87+
selectFit.verticalOffset += scrollAmount;
88+
this.global_yOffset = selectFit.verticalOffset;
9089
}
9190
}
9291

93-
private scrollToItem(selectFit: SelectFit): number {
94-
const itemPosition = this.calculateScrollPosition(selectFit);
92+
private scrollToItem(scrollAmount: number): number {
9593
if (isIE()) {
9694
setTimeout(() => {
97-
this.select.scrollContainer.scrollTop = (itemPosition);
95+
this.select.scrollContainer.scrollTop = (scrollAmount);
9896
}, 1);
9997
} else {
100-
this.select.scrollContainer.scrollTop = (itemPosition);
98+
this.select.scrollContainer.scrollTop = (scrollAmount);
10199
}
102-
return itemPosition;
100+
return scrollAmount;
103101
}
104102

105103
private calculateScrollPosition(selectFit: SelectFit): number {
@@ -113,16 +111,15 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
113111
return Math.floor(scrollPosition);
114112
}
115113

116-
// This method can be scrambled and combined in manageScrollToItem()
117-
private compensateYScroll(selectFit: SelectFit, compensation: number ) {
118-
selectFit.verticalOffset += compensation;
119-
this.global_yOffset = selectFit.verticalOffset;
120-
}
121-
122114
// Position the items outer container Below or Above the input.
123115
fitInViewport(contentElement: HTMLElement, selectFit: SelectFit) {
124116
// Position Select component's container below target/input as preferred positioning over above target/input
125-
if (this.canFitBelowInput(selectFit) || !this.canFitAboveInput(selectFit)) {
117+
const canFitBelowInput = selectFit.targetRect.top - selectFit.styles.itemTextToInputTextDiff + selectFit.contentElementRect.height <
118+
selectFit.viewPortRect.bottom;
119+
const canFitAboveInput = selectFit.targetRect.bottom + selectFit.styles.itemTextToInputTextDiff -
120+
selectFit.contentElementRect.height > selectFit.viewPortRect.top;
121+
// Position Select component's container below target/input as preferred positioning over above target/input
122+
if (canFitBelowInput || !canFitAboveInput) {
126123
// Calculate container starting point;
127124
// TODO: modify the yOffset instead & use one call to super.setStyle
128125
selectFit.top = selectFit.targetRect.top - selectFit.styles.itemTextToInputTextDiff;
@@ -135,16 +132,6 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
135132
}
136133
}
137134

138-
protected canFitBelowInput(selectFit: SelectFit): boolean {
139-
return selectFit.targetRect.top - selectFit.styles.itemTextToInputTextDiff + selectFit.contentElementRect.height <
140-
selectFit.viewPortRect.bottom;
141-
}
142-
143-
protected canFitAboveInput(selectFit: SelectFit): boolean {
144-
return selectFit.targetRect.bottom + selectFit.styles.itemTextToInputTextDiff -
145-
selectFit.contentElementRect.height > selectFit.viewPortRect.top;
146-
}
147-
148135
protected setStyles(contentElement: HTMLElement, selectFit: SelectFit, initialCall?: boolean) {
149136
// The Select component's container is about to be displayed. Set its position.
150137
if (initialCall) {

0 commit comments

Comments
 (0)