Skip to content

Commit 0327c1b

Browse files
committed
fixup! feat(cdk-experimental/menu): allow configuration of typeahead and menu position
1 parent e3fad64 commit 0327c1b

File tree

5 files changed

+48
-29
lines changed

5 files changed

+48
-29
lines changed

src/cdk-experimental/menu/context-menu.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
Overlay,
2828
OverlayConfig,
2929
OverlayRef,
30+
STANDARD_DROPDOWN_BELOW_POSITIONS,
3031
} from '@angular/cdk/overlay';
3132
import {Portal, TemplatePortal} from '@angular/cdk/portal';
3233
import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
@@ -239,14 +240,7 @@ export class CdkContextMenuTrigger extends MenuTrigger implements OnDestroy {
239240
.flexibleConnectedTo(coordinates)
240241
.withDefaultOffsetX(this._options?.offsetX ?? 2)
241242
.withDefaultOffsetY(this._options?.offsetY ?? 2)
242-
.withPositions(
243-
this._options?.preferredPositions ?? [
244-
{originX: 'end', originY: 'top', overlayX: 'start', overlayY: 'top'},
245-
{originX: 'start', originY: 'top', overlayX: 'end', overlayY: 'top'},
246-
{originX: 'end', originY: 'bottom', overlayX: 'start', overlayY: 'bottom'},
247-
{originX: 'start', originY: 'bottom', overlayX: 'end', overlayY: 'bottom'},
248-
],
249-
);
243+
.withPositions(this._options?.preferredPositions ?? STANDARD_DROPDOWN_BELOW_POSITIONS);
250244
}
251245

252246
/**

src/cdk-experimental/menu/menu-item-trigger.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import {
2929
Overlay,
3030
OverlayConfig,
3131
OverlayRef,
32+
STANDARD_DROPDOWN_ADJACENT_POSITIONS,
33+
STANDARD_DROPDOWN_BELOW_POSITIONS,
3234
} from '@angular/cdk/overlay';
3335
import {DOWN_ARROW, ENTER, LEFT_ARROW, RIGHT_ARROW, SPACE, UP_ARROW} from '@angular/cdk/keycodes';
3436
import {fromEvent, merge, Subject} from 'rxjs';
@@ -288,18 +290,8 @@ export class CdkMenuItemTrigger extends MenuTrigger implements OnDestroy {
288290
/** Determine and return where to position the opened menu relative to the menu item */
289291
private _getOverlayPositions(): ConnectedPosition[] {
290292
return !this._parentMenu || this._parentMenu.orientation === 'horizontal'
291-
? [
292-
{originX: 'start', originY: 'bottom', overlayX: 'start', overlayY: 'top'},
293-
{originX: 'start', originY: 'top', overlayX: 'start', overlayY: 'bottom'},
294-
{originX: 'end', originY: 'bottom', overlayX: 'end', overlayY: 'top'},
295-
{originX: 'end', originY: 'top', overlayX: 'end', overlayY: 'bottom'},
296-
]
297-
: [
298-
{originX: 'end', originY: 'top', overlayX: 'start', overlayY: 'top'},
299-
{originX: 'end', originY: 'bottom', overlayX: 'start', overlayY: 'bottom'},
300-
{originX: 'start', originY: 'top', overlayX: 'end', overlayY: 'top'},
301-
{originX: 'start', originY: 'bottom', overlayX: 'end', overlayY: 'bottom'},
302-
];
293+
? STANDARD_DROPDOWN_BELOW_POSITIONS
294+
: STANDARD_DROPDOWN_ADJACENT_POSITIONS;
303295
}
304296

305297
/**

src/cdk-experimental/menu/menu-item.spec.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ describe('MenuItem', () => {
9090
fixture.detectChanges();
9191

9292
menuItem = fixture.debugElement.query(By.directive(CdkMenuItem)).injector.get(CdkMenuItem);
93+
return fixture;
9394
}
9495

9596
it('should get the text for a simple menu item with no nested or wrapped elements', () => {
@@ -98,15 +99,21 @@ describe('MenuItem', () => {
9899
});
99100

100101
it('should get the text for menu item with a single nested mat icon component', () => {
101-
createComponent(MenuItemWithIcon);
102+
const fixture = createComponent(MenuItemWithIcon);
103+
expect(menuItem.getLabel()).toEqual('unicorn Click me!');
104+
fixture.componentInstance.typeahead = 'Click me!';
105+
fixture.detectChanges();
102106
expect(menuItem.getLabel()).toEqual('Click me!');
103107
});
104108

105109
it(
106110
'should get the text for menu item with single nested component with the material ' +
107111
'icon class',
108112
() => {
109-
createComponent(MenuItemWithIconClass);
113+
const fixture = createComponent(MenuItemWithIconClass);
114+
expect(menuItem.getLabel()).toEqual('unicorn Click me!');
115+
fixture.componentInstance.typeahead = 'Click me!';
116+
fixture.detectChanges();
110117
expect(menuItem.getLabel()).toEqual('Click me!');
111118
},
112119
);
@@ -120,7 +127,10 @@ describe('MenuItem', () => {
120127
'should get the text for a menu item with nested icon, nested icon class and nested ' +
121128
'wrapping elements',
122129
() => {
123-
createComponent(MenuItemWithMultipleNestings);
130+
const fixture = createComponent(MenuItemWithMultipleNestings);
131+
expect(menuItem.getLabel()).toEqual('unicorn Click menume!');
132+
fixture.componentInstance.typeahead = 'Click me!';
133+
fixture.detectChanges();
124134
expect(menuItem.getLabel()).toEqual('Click me!');
125135
},
126136
);
@@ -134,22 +144,27 @@ class SingleMenuItem {}
134144

135145
@Component({
136146
template: `
137-
<button cdkMenuItem>
147+
<button cdkMenuItem [typeahead]="typeahead">
138148
<mat-icon>unicorn</mat-icon>
139149
Click me!
140150
</button>
141151
`,
142152
})
143-
class MenuItemWithIcon {}
153+
class MenuItemWithIcon {
154+
typeahead: string;
155+
}
156+
144157
@Component({
145158
template: `
146-
<button cdkMenuItem>
159+
<button cdkMenuItem [typeahead]="typeahead">
147160
<div class="material-icons">unicorn</div>
148161
Click me!
149162
</button>
150163
`,
151164
})
152-
class MenuItemWithIconClass {}
165+
class MenuItemWithIconClass {
166+
typeahead: string;
167+
}
153168

154169
@Component({
155170
template: ` <button cdkMenuItem><b>Click</b> me!</button> `,
@@ -158,7 +173,7 @@ class MenuItemWithBoldElement {}
158173

159174
@Component({
160175
template: `
161-
<button cdkMenuItem>
176+
<button cdkMenuItem [typeahead]="typeahead">
162177
<div>
163178
<div class="material-icons">unicorn</div>
164179
<div>
@@ -170,7 +185,9 @@ class MenuItemWithBoldElement {}
170185
</button>
171186
`,
172187
})
173-
class MenuItemWithMultipleNestings {}
188+
class MenuItemWithMultipleNestings {
189+
typeahead: string;
190+
}
174191

175192
@Component({
176193
selector: 'mat-icon',

src/cdk/overlay/position/flexible-connected-position-strategy.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,3 +1288,17 @@ function getRoundedBoundingClientRect(clientRect: Dimensions): Dimensions {
12881288
height: Math.floor(clientRect.height),
12891289
};
12901290
}
1291+
1292+
export const STANDARD_DROPDOWN_BELOW_POSITIONS: ConnectedPosition[] = [
1293+
{originX: 'start', originY: 'bottom', overlayX: 'start', overlayY: 'top'},
1294+
{originX: 'start', originY: 'top', overlayX: 'start', overlayY: 'bottom'},
1295+
{originX: 'end', originY: 'bottom', overlayX: 'end', overlayY: 'top'},
1296+
{originX: 'end', originY: 'top', overlayX: 'end', overlayY: 'bottom'},
1297+
];
1298+
1299+
export const STANDARD_DROPDOWN_ADJACENT_POSITIONS: ConnectedPosition[] = [
1300+
{originX: 'end', originY: 'top', overlayX: 'start', overlayY: 'top'},
1301+
{originX: 'end', originY: 'bottom', overlayX: 'start', overlayY: 'bottom'},
1302+
{originX: 'start', originY: 'top', overlayX: 'end', overlayY: 'top'},
1303+
{originX: 'start', originY: 'bottom', overlayX: 'end', overlayY: 'bottom'},
1304+
];

src/cdk/overlay/public-api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ export {
2727
ConnectedPosition,
2828
FlexibleConnectedPositionStrategy,
2929
FlexibleConnectedPositionStrategyOrigin,
30+
STANDARD_DROPDOWN_ADJACENT_POSITIONS,
31+
STANDARD_DROPDOWN_BELOW_POSITIONS,
3032
} from './position/flexible-connected-position-strategy';

0 commit comments

Comments
 (0)