Skip to content

Commit b0f42b5

Browse files
fix(tabs): corrige posicionamento do dropdown
Foi realizado uma correção no posicionamento do dropdown. (Ao utilizar o po-tabs com tabs agrupadas pelo dropdown o listbox não abre na posição correta estando dentro de um po-page-default) fixes DTHFUI-9361
1 parent 03befd7 commit b0f42b5

File tree

2 files changed

+69
-30
lines changed

2 files changed

+69
-30
lines changed

projects/ui/src/lib/components/po-tabs/po-tab-dropdown/po-tab-dropdown.component.spec.ts

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,26 @@ describe('PoTabDropdownComponent:', () => {
2323
const buttonElementRefMock = {
2424
nativeElement: {
2525
getBoundingClientRect: () => ({
26-
right: 100,
27-
bottom: 100
26+
right: 200,
27+
height: 50
2828
}),
29-
closest: (selector: string) => ({
30-
getBoundingClientRect: () => ({
31-
width: 300,
32-
bottom: 120
33-
})
34-
})
29+
closest: (selector: string) => {
30+
if (selector === '.po-tabs-container') {
31+
return {
32+
getBoundingClientRect: () => ({
33+
bottom: 150,
34+
width: 400
35+
})
36+
};
37+
}
38+
return null;
39+
}
40+
}
41+
};
42+
43+
const elementRefMock = {
44+
nativeElement: {
45+
closest: (selector: string) => null
3546
}
3647
};
3748

@@ -108,42 +119,54 @@ describe('PoTabDropdownComponent:', () => {
108119
expect(component.closeDropdown).toHaveBeenCalled();
109120
});
110121

111-
it('setDropdownPosition: should set dropdownStyles with correct values when rightPosition is positive', () => {
122+
it('setDropdownPosition: should set dropdownStyles with correct values when not inside a PoPage', () => {
123+
component['elementRef'] = elementRefMock as ElementRef;
124+
112125
component.setDropdownPosition();
113126

114127
const expectedStyles = {
115-
top: `${120 + 4 + 50}px`,
128+
top: `${150 + 4 + window.scrollY}px`,
116129
maxWidth: '300px',
117-
right: `${300 - 100}px`
130+
right: `${200}px`
118131
};
119132

120133
expect(component.dropdownStyles).toEqual(expectedStyles);
121134
});
122135

123-
it('setDropdownPosition: should set dropdownStyles with correct values when rightPosition is zero', () => {
124-
const buttonElementRefMockZero = {
136+
it('setDropdownPosition: should set dropdownStyles with correct values when inside a PoPage', () => {
137+
const buttonElementRefMock = {
125138
nativeElement: {
126-
getBoundingClientRect: () => ({
127-
right: 350,
128-
bottom: 100
129-
}),
130-
closest: (selector: string) => ({
131-
getBoundingClientRect: () => ({
132-
width: 300,
133-
bottom: 120
134-
})
135-
})
139+
getBoundingClientRect: () => ({ right: 300, height: 100 }),
140+
closest: (selector: string) => {
141+
if (selector === '.po-tabs-container') {
142+
return { getBoundingClientRect: () => ({ right: 350, height: 120 }) };
143+
} else if (selector === '.po-page-content') {
144+
return true;
145+
}
146+
return null;
147+
}
148+
}
149+
};
150+
const elementRefMock = {
151+
nativeElement: {
152+
closest: (selector: string) => {
153+
if (selector === '.po-page-content') {
154+
return true;
155+
}
156+
return null;
157+
}
136158
}
137159
};
138160

139-
component.button.buttonElement = buttonElementRefMockZero as ElementRef;
161+
component.button.buttonElement = buttonElementRefMock as ElementRef;
162+
component['elementRef'] = elementRefMock as ElementRef;
140163

141164
component.setDropdownPosition();
142165

143166
const expectedStyles = {
144-
top: `${120 + 4 + 50}px`,
167+
top: `${120 + 4 + window.scrollY}px`,
145168
maxWidth: '300px',
146-
right: '0px'
169+
right: '50px'
147170
};
148171

149172
expect(component.dropdownStyles).toEqual(expectedStyles);

projects/ui/src/lib/components/po-tabs/po-tab-dropdown/po-tab-dropdown.component.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,28 @@ export class PoTabDropdownComponent implements AfterViewInit {
8282
const tabsContainerRect = this.buttonElement.nativeElement.closest('.po-tabs-container').getBoundingClientRect();
8383
const dropdownWidth = 300;
8484

85-
let rightPosition = tabsContainerRect.width - buttonRect.right;
86-
if (rightPosition < 0) {
87-
rightPosition = 0;
85+
const isInsidePage = this.elementRef.nativeElement.closest('.po-page-content');
86+
this.dropdownStyles = this.calculateDropdownStyles(buttonRect, tabsContainerRect, dropdownWidth, isInsidePage);
87+
}
88+
89+
private calculateDropdownStyles(
90+
buttonRect: DOMRect,
91+
tabsContainerRect: DOMRect,
92+
dropdownWidth: number,
93+
isInsidePage: boolean
94+
) {
95+
if (isInsidePage) {
96+
return {
97+
top: `${tabsContainerRect.height + 4 + window.scrollY}px`,
98+
maxWidth: `${dropdownWidth}px`,
99+
right: `${tabsContainerRect.right - buttonRect.right}px`
100+
};
88101
}
89102

90-
this.dropdownStyles = {
103+
let rightPosition = tabsContainerRect.width - buttonRect.right;
104+
rightPosition = Math.max(0, rightPosition);
105+
106+
return {
91107
top: `${tabsContainerRect.bottom + 4 + window.scrollY}px`,
92108
maxWidth: `${dropdownWidth}px`,
93109
right: `${rightPosition}px`

0 commit comments

Comments
 (0)