Skip to content

Commit

Permalink
fix(flex): wait for parent element until template is initialized (#1237)
Browse files Browse the repository at this point in the history
  • Loading branch information
CaerusKaru authored May 7, 2020
1 parent 990586b commit 93a426a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/lib/core/base/base2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ export abstract class BaseDirective2 implements OnChanges, OnDestroy {
return 'row';
}

protected hasWrap(target: HTMLElement): boolean {
return this.styler.hasWrap(target);
}

/** Applies styles given via string pair or object map to the directive element */
protected applyStyleToElement(style: StyleDefinition,
value?: string | number,
Expand Down
5 changes: 5 additions & 0 deletions src/lib/core/style-utils/style-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export class StyleUtils {
return [value || 'row', hasInlineValue];
}

hasWrap(target: HTMLElement): boolean {
const query = 'flex-wrap';
return this.lookupStyle(target, query) === 'wrap';
}

/**
* Find the DOM element's raw attribute value (if any)
*/
Expand Down
22 changes: 22 additions & 0 deletions src/lib/flex/flex/flex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@ describe('flex directive', () => {
expectEl(element).toHaveStyle({'flex': '10 1 auto'}, styler);
});

it('should add correct styles for `fxFlex` with NgForOf', () => {
componentWithTemplate(`
<div [fxLayout]="direction">
<div *ngFor="let el of els" fxFlex="50"></div>
</div>
`);
fixture.componentInstance.direction = 'row';
fixture.detectChanges();
let element = queryFor(fixture, '[fxFlex]')[0];
expectEl(element).toHaveStyle({'max-width': '50%'}, styler);
expectEl(element).toHaveStyle({'box-sizing': 'border-box'}, styler);
expectEl(element).toHaveStyle({'flex': '1 1 100%'}, styler);
fixture.componentInstance.direction = 'column';
fixture.detectChanges();
element = queryFor(fixture, '[fxFlex]')[0];
expectEl(element).toHaveStyle({'max-height': '50%'}, styler);
expectEl(element).toHaveStyle({'box-sizing': 'border-box'}, styler);
expectEl(element).toHaveStyle({'flex': '1 1 100%'}, styler);
});

it('should add correct styles for `fxFlex` and ngStyle without layout change', () => {
// NOTE: the presence of ngIf on the child element is imperative for detecting issue 700
componentWithTemplate(`
Expand All @@ -119,6 +139,7 @@ describe('flex directive', () => {
<div fxFlex="30"></div>
</div>
`);
fixture.debugElement.componentInstance.direction = 'column';
fixture.detectChanges();
let element = queryFor(fixture, '[fxFlex]')[0];
expectNativeEl(fixture).toHaveStyle({'flex-direction': 'column'}, styler);
Expand Down Expand Up @@ -1088,6 +1109,7 @@ export class MockFlexStyleBuilder extends StyleBuilder {
})
class TestFlexComponent {
direction = 'column';
els = new Array(5);
}

@Component({
Expand Down
18 changes: 12 additions & 6 deletions src/lib/flex/flex/flex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Directive, ElementRef, Inject, Injectable, Input} from '@angular/core';
import {Directive, ElementRef, Inject, Injectable, Input, OnInit} from '@angular/core';
import {
BaseDirective2,
LayoutConfigOptions,
Expand Down Expand Up @@ -200,11 +200,11 @@ const selector = `
* @see https://css-tricks.com/snippets/css/a-guide-to-flexbox/
*/
@Directive()
export class FlexDirective extends BaseDirective2 {
export class FlexDirective extends BaseDirective2 implements OnInit {

protected DIRECTIVE_KEY = 'flex';
protected direction = '';
protected wrap = false;
protected direction?: string = undefined;
protected wrap?: boolean = undefined;


@Input('fxShrink')
Expand All @@ -228,9 +228,12 @@ export class FlexDirective extends BaseDirective2 {
styleUtils: StyleUtils,
@Inject(LAYOUT_CONFIG) protected layoutConfig: LayoutConfigOptions,
styleBuilder: FlexStyleBuilder,
marshal: MediaMarshaller) {
protected marshal: MediaMarshaller) {
super(elRef, styleBuilder, styleUtils, marshal);
this.init();
}

ngOnInit() {
if (this.parentElement) {
this.marshal.trackValue(this.parentElement, 'layout')
.pipe(takeUntil(this.destroySubject))
Expand All @@ -256,9 +259,12 @@ export class FlexDirective extends BaseDirective2 {
/** Input to this is exclusively the basis input value */
protected updateWithValue(value: string) {
const addFlexToParent = this.layoutConfig.addFlexToParent !== false;
if (!this.direction) {
if (this.direction === undefined) {
this.direction = this.getFlexFlowDirection(this.parentElement!, addFlexToParent);
}
if (this.wrap === undefined) {
this.wrap = this.hasWrap(this.parentElement!);
}
const direction = this.direction;
const isHorizontal = direction.startsWith('row');
const hasWrap = this.wrap;
Expand Down

0 comments on commit 93a426a

Please sign in to comment.