Skip to content

Instantly share code, notes, and snippets.

@isabelandss
Last active November 17, 2020 13:39
Show Gist options
  • Save isabelandss/a6335cb25571bd260eac5a44a306e863 to your computer and use it in GitHub Desktop.
Save isabelandss/a6335cb25571bd260eac5a44a306e863 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
@Injectable({
providedIn: 'root',
})
export class FormBuilderService {
form: FormGroup;
constructor(private formBuilder: FormBuilder) {}
mount(component) {
return {
form: this.formControlTemplateBuilder(component),
template: this.formControlBuilder(component),
}
}
typeForm(component) {
return component?.items ? 'items' : 'default';
}
private formControlBuilder(component) {
const builder = {
title: {
text: [component?.text],
},
subtitle: {
text: [component?.text],
},
product_card_item: component?.items?.map(item =>
this.formBuilder.group({
avatar: [item?.avatar],
description: [item?.description],
spotlight: [item?.spotlight],
value: [item?.value],
}),
),
};
return this.formBuilder.group({
...builder[component?.type],
})
}
private formControlTemplateBuilder(component) {
const builder = {
title: [
{ component: 'input', props: [] }
],
subtitle: [
{ component: 'input', props: [] }
],
product_card_item: {},
}
return builder[component?.type] || null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment