Created
July 15, 2020 14:19
-
-
Save anddam/d0451d9f15cbe0a8684f365b9afe234e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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