Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 93a426a

Browse files
authored
fix(flex): wait for parent element until template is initialized (#1237)
1 parent 990586b commit 93a426a

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

src/lib/core/base/base2.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ export abstract class BaseDirective2 implements OnChanges, OnDestroy {
132132
return 'row';
133133
}
134134

135+
protected hasWrap(target: HTMLElement): boolean {
136+
return this.styler.hasWrap(target);
137+
}
138+
135139
/** Applies styles given via string pair or object map to the directive element */
136140
protected applyStyleToElement(style: StyleDefinition,
137141
value?: string | number,

src/lib/core/style-utils/style-utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ export class StyleUtils {
6060
return [value || 'row', hasInlineValue];
6161
}
6262

63+
hasWrap(target: HTMLElement): boolean {
64+
const query = 'flex-wrap';
65+
return this.lookupStyle(target, query) === 'wrap';
66+
}
67+
6368
/**
6469
* Find the DOM element's raw attribute value (if any)
6570
*/

src/lib/flex/flex/flex.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,26 @@ describe('flex directive', () => {
9898
expectEl(element).toHaveStyle({'flex': '10 1 auto'}, styler);
9999
});
100100

101+
it('should add correct styles for `fxFlex` with NgForOf', () => {
102+
componentWithTemplate(`
103+
<div [fxLayout]="direction">
104+
<div *ngFor="let el of els" fxFlex="50"></div>
105+
</div>
106+
`);
107+
fixture.componentInstance.direction = 'row';
108+
fixture.detectChanges();
109+
let element = queryFor(fixture, '[fxFlex]')[0];
110+
expectEl(element).toHaveStyle({'max-width': '50%'}, styler);
111+
expectEl(element).toHaveStyle({'box-sizing': 'border-box'}, styler);
112+
expectEl(element).toHaveStyle({'flex': '1 1 100%'}, styler);
113+
fixture.componentInstance.direction = 'column';
114+
fixture.detectChanges();
115+
element = queryFor(fixture, '[fxFlex]')[0];
116+
expectEl(element).toHaveStyle({'max-height': '50%'}, styler);
117+
expectEl(element).toHaveStyle({'box-sizing': 'border-box'}, styler);
118+
expectEl(element).toHaveStyle({'flex': '1 1 100%'}, styler);
119+
});
120+
101121
it('should add correct styles for `fxFlex` and ngStyle without layout change', () => {
102122
// NOTE: the presence of ngIf on the child element is imperative for detecting issue 700
103123
componentWithTemplate(`
@@ -119,6 +139,7 @@ describe('flex directive', () => {
119139
<div fxFlex="30"></div>
120140
</div>
121141
`);
142+
fixture.debugElement.componentInstance.direction = 'column';
122143
fixture.detectChanges();
123144
let element = queryFor(fixture, '[fxFlex]')[0];
124145
expectNativeEl(fixture).toHaveStyle({'flex-direction': 'column'}, styler);
@@ -1088,6 +1109,7 @@ export class MockFlexStyleBuilder extends StyleBuilder {
10881109
})
10891110
class TestFlexComponent {
10901111
direction = 'column';
1112+
els = new Array(5);
10911113
}
10921114

10931115
@Component({

src/lib/flex/flex/flex.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import {Directive, ElementRef, Inject, Injectable, Input} from '@angular/core';
8+
import {Directive, ElementRef, Inject, Injectable, Input, OnInit} from '@angular/core';
99
import {
1010
BaseDirective2,
1111
LayoutConfigOptions,
@@ -200,11 +200,11 @@ const selector = `
200200
* @see https://css-tricks.com/snippets/css/a-guide-to-flexbox/
201201
*/
202202
@Directive()
203-
export class FlexDirective extends BaseDirective2 {
203+
export class FlexDirective extends BaseDirective2 implements OnInit {
204204

205205
protected DIRECTIVE_KEY = 'flex';
206-
protected direction = '';
207-
protected wrap = false;
206+
protected direction?: string = undefined;
207+
protected wrap?: boolean = undefined;
208208

209209

210210
@Input('fxShrink')
@@ -228,9 +228,12 @@ export class FlexDirective extends BaseDirective2 {
228228
styleUtils: StyleUtils,
229229
@Inject(LAYOUT_CONFIG) protected layoutConfig: LayoutConfigOptions,
230230
styleBuilder: FlexStyleBuilder,
231-
marshal: MediaMarshaller) {
231+
protected marshal: MediaMarshaller) {
232232
super(elRef, styleBuilder, styleUtils, marshal);
233233
this.init();
234+
}
235+
236+
ngOnInit() {
234237
if (this.parentElement) {
235238
this.marshal.trackValue(this.parentElement, 'layout')
236239
.pipe(takeUntil(this.destroySubject))
@@ -256,9 +259,12 @@ export class FlexDirective extends BaseDirective2 {
256259
/** Input to this is exclusively the basis input value */
257260
protected updateWithValue(value: string) {
258261
const addFlexToParent = this.layoutConfig.addFlexToParent !== false;
259-
if (!this.direction) {
262+
if (this.direction === undefined) {
260263
this.direction = this.getFlexFlowDirection(this.parentElement!, addFlexToParent);
261264
}
265+
if (this.wrap === undefined) {
266+
this.wrap = this.hasWrap(this.parentElement!);
267+
}
262268
const direction = this.direction;
263269
const isHorizontal = direction.startsWith('row');
264270
const hasWrap = this.wrap;

0 commit comments

Comments
 (0)