Skip to content

Instantly share code, notes, and snippets.

@anddam
Created July 15, 2020 14:19
Show Gist options
  • Save anddam/d0451d9f15cbe0a8684f365b9afe234e to your computer and use it in GitHub Desktop.
Save anddam/d0451d9f15cbe0a8684f365b9afe234e to your computer and use it in GitHub Desktop.
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { tap } from 'rxjs/operators';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig, FormlyFormOptions } from '@ngx-formly/core';
import { FormlyJsonschema } from '@ngx-formly/core/json-schema';
export interface IFormlyUser {
type: string;
form: FormGroup;
fields: FormlyFieldConfig[];
options: Object;
}
@Injectable({
providedIn: 'root'
})
export class SchemaLoader {
public type: string;
constructor(
private http: HttpClient,
private formlyJsonschema: FormlyJsonschema,
) { };
loadJsonSchema(type: string, instance: IFormlyUser) {
this.http.get<any>(`assets/user/json-schema/${type}.json`).pipe(
tap(({ schema }) => {
instance.type = type;
instance.form = new FormGroup({});
instance.options = {};
instance.fields = [this.formlyJsonschema.toFieldConfig(schema)].map(field => {
field.fieldGroup.map(fieldGroup => {
let property = schema.properties[fieldGroup.key];
if ('hide' in property) {
fieldGroup.hide = property.hide;
}
return fieldGroup;
});
return field;
});
}),
).subscribe();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment