Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
fix: avoid filling in superfluous props (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl authored and tilmx committed Jan 31, 2019
1 parent a8223b7 commit 65a9410
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
34 changes: 27 additions & 7 deletions packages/core/src/preview/preview-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,29 @@ export class PreviewStore<V> {
return this.project.getPages().find(page => page.getActive());
}

public getChildren<T>(element: Model.Element, render: (element: Model.Element) => T): T[] {
public getChildren<T>(
element: Model.Element,
render: (element: Model.Element) => T
): T[] | null {
const childContent = element.getContentBySlotType(Types.SlotType.Children);

if (!childContent) {
return [];
return null;
}

const slot = childContent.getSlot();

if (!slot) {
return null;
}

return childContent.getElements().map(render);
const elements = childContent.getElements();

if (elements.length === 0 && !slot.getRequired()) {
return null;
}

return elements.map(render);
}

public getComponent(element: Model.Element): V | undefined {
Expand Down Expand Up @@ -201,17 +216,22 @@ export class PreviewStore<V> {
public getSlots<T>(
element: Model.Element,
render: (element: Model.Element) => T
): { [propName: string]: T[] } {
): { [propName: string]: T[] | null } {
return element
.getContents()
.filter(content => content.getSlotType() !== Types.SlotType.Children)
.reduce<{ [key: string]: T[] }>((renderProperties, content) => {
.reduce<{ [key: string]: T[] | null }>((renderProperties, content) => {
const slot = content.getSlot();

if (slot) {
renderProperties[slot.getPropertyName()] = content.getElements().map(render);
if (!slot) {
return renderProperties;
}

const elements = content.getElements();
const children =
elements.length === 0 && !slot.getRequired() ? null : elements.map(render);

renderProperties[slot.getPropertyName()] = children;
return renderProperties;
}, {});
}
Expand Down
10 changes: 7 additions & 3 deletions packages/model/src/element/element-property/element-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,22 @@ export class ElementProperty {
}

public getValue(): Types.ElementPropertyValue {
if (this.referencedUserStoreProperty && this.patternProperty) {
if (!this.patternProperty) {
return undefined;
}

if (this.referencedUserStoreProperty) {
const referencedValue = this.referencedUserStoreProperty.getValue();
return this.patternProperty.coerceValue(referencedValue);
}

const concreteValue = this.getConcreteValue();

if (typeof concreteValue !== 'undefined' && this.patternProperty) {
if (typeof concreteValue !== 'undefined') {
return this.patternProperty.coerceValue(concreteValue);
}

return this.getDefaultValue();
return this.patternProperty.getRequired() ? this.getDefaultValue() : undefined;
}

public getRawValue(): string {
Expand Down
4 changes: 4 additions & 0 deletions packages/model/src/pattern/pattern-slot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export class PatternSlot {
return this.propertyName;
}

public getRequired(): boolean {
return this.required;
}

public getType(): Types.SlotType {
return this.type;
}
Expand Down

1 comment on commit 65a9410

@marionebl
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.