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

Commit 0eccec4

Browse files
CaerusKaruThomasBurleson
authored andcommitted
fix(fxLayoutGap): not working with dynamic fxHide (#983)
1 parent f4eb901 commit 0eccec4

File tree

6 files changed

+69
-6
lines changed

6 files changed

+69
-6
lines changed

src/lib/core/base/base2.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ export abstract class BaseDirective2 implements OnChanges, OnDestroy {
107107

108108
/** Force trigger style updates on DOM element */
109109
protected triggerUpdate() {
110-
const val = this.marshal.getValue(this.nativeElement, this.DIRECTIVE_KEY);
111-
if (val !== undefined) {
112-
this.marshal.updateElement(this.nativeElement, this.DIRECTIVE_KEY, val);
113-
}
110+
this.marshal.triggerUpdate(this.nativeElement, this.DIRECTIVE_KEY);
114111
}
115112

116113
/**

src/lib/core/media-marshaller/media-marshaller.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ describe('media-marshaller', () => {
102102
expect(triggered).toBeTruthy();
103103
});
104104

105+
it('should triggerUpdate', () => {
106+
let triggered = false;
107+
const builder = () => {
108+
triggered = true;
109+
};
110+
mediaMarshaller.init(fakeElement, fakeKey, builder);
111+
mediaMarshaller.setValue(fakeElement, fakeKey, 0, '');
112+
mediaMarshaller.triggerUpdate(fakeElement, fakeKey);
113+
expect(triggered).toBeTruthy();
114+
});
115+
105116
it('should get the right value', () => {
106117
const builder = () => {
107118
};
@@ -233,6 +244,17 @@ describe('media-marshaller', () => {
233244
expect(triggered).toBeTruthy();
234245
});
235246

247+
it('should triggerUpdate', () => {
248+
let triggered = false;
249+
const builder = () => {
250+
triggered = true;
251+
};
252+
mediaMarshaller.init(fakeElement, fakeKey, builder);
253+
mediaMarshaller.setValue(fakeElement, fakeKey, 0, '');
254+
mediaMarshaller.triggerUpdate(fakeElement, fakeKey);
255+
expect(triggered).toBeTruthy();
256+
});
257+
236258
it('should get the right value', () => {
237259
const builder = () => {
238260
};

src/lib/core/media-marshaller/media-marshaller.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,25 @@ export class MediaMarshaller {
247247
}
248248
}
249249

250+
/**
251+
* trigger an update for a given element and key (e.g. layout)
252+
* @param element
253+
* @param key
254+
*/
255+
triggerUpdate(element: HTMLElement, key?: string): void {
256+
const bpMap = this.elementMap.get(element);
257+
if (bpMap) {
258+
const valueMap = this.getActivatedValues(bpMap, key);
259+
if (valueMap) {
260+
if (key) {
261+
this.updateElement(element, key, valueMap.get(key));
262+
} else {
263+
valueMap.forEach((v, k) => this.updateElement(element, k, v));
264+
}
265+
}
266+
}
267+
}
268+
250269
/** Cross-reference for HTMLElement with directive key */
251270
private buildElementKeyMap(element: HTMLElement, key: string) {
252271
let keyMap = this.elementKeyMap.get(element);

src/lib/extended/show-hide/show-hide.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export class ShowHideDirective extends BaseDirective2 implements AfterViewInit,
150150
if (isPlatformServer(this.platformId) && this.serverModuleLoaded) {
151151
this.nativeElement.style.setProperty('display', '');
152152
}
153+
this.marshal.triggerUpdate(this.parentElement!, 'layout-gap');
153154
}
154155
}
155156

src/lib/flex/layout-gap/layout-gap.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,29 @@ describe('layout-gap directive', () => {
410410
expectEl(nodes[2]).not.toHaveStyle({'margin-bottom': '*'}, styler);
411411
});
412412

413+
it('should work with dynamic fxHide', () => {
414+
let template = `
415+
<div fxLayout="row" fxLayoutGap="10px">
416+
<div fxFlex>A</div>
417+
<div fxFlex [fxHide]="shouldHide">B</div>
418+
</div>
419+
`;
420+
createTestComponent(template);
421+
fixture.detectChanges();
422+
423+
let nodes = queryFor(fixture, '[fxFlex]');
424+
expect(nodes.length).toEqual(2);
425+
expectEl(nodes[0]).not.toHaveStyle({'margin-right': '*'}, styler);
426+
expectEl(nodes[1]).not.toHaveStyle({'margin-right': '*'}, styler);
427+
428+
let instance = fixture.componentInstance;
429+
instance.shouldHide = false;
430+
fixture.detectChanges();
431+
432+
expectEl(nodes[0]).toHaveStyle({'margin-right': '10px'}, styler);
433+
expectEl(nodes[1]).not.toHaveStyle({'margin-right': '*'}, styler);
434+
});
435+
413436
it('should work with responsive fxHide', () => {
414437
let template = `
415438
<div fxLayoutAlign="center center" fxLayoutGap="13px">
@@ -557,6 +580,7 @@ export class MockLayoutGapStyleBuilder extends StyleBuilder {
557580
class TestLayoutGapComponent implements OnInit {
558581
direction = 'column';
559582
gap = '8px';
583+
shouldHide = true;
560584
rows = new Array(4);
561585

562586
constructor() {

src/lib/flex/layout-gap/layout-gap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ export class LayoutGapStyleBuilder extends StyleBuilder {
6666
const paddingStyles = buildGridPadding(gapValue, parent.directionality);
6767
this._styler.applyStyleToElements(paddingStyles, parent.items);
6868
} else {
69-
const lastItem = items.pop();
69+
const lastItem = items.pop()!;
7070

7171
// For each `element` children EXCEPT the last,
7272
// set the margin right/bottom styles...
7373
const gapCss = buildGapCSS(gapValue, parent);
7474
this._styler.applyStyleToElements(gapCss, items);
7575

7676
// Clear all gaps for all visible elements
77-
this._styler.applyStyleToElements(CLEAR_MARGIN_CSS, [lastItem!]);
77+
this._styler.applyStyleToElements(CLEAR_MARGIN_CSS, [lastItem]);
7878
}
7979
}
8080
}

0 commit comments

Comments
 (0)