1.0.4 • Published 4 months ago

ngx-dynamic-json-form-material v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

ngx-dynamic-json-form-material

The easy way to generate JSON based forms with angular.

Quick Start

This is the UI material package for ngx-dynamic-json-form.

Without ngx-dynamic-json-form-core this library is not usable. So please take care to install it too.

Versions

Install and Usage

This is a step by step instruction to install and use ngx-dynamic-json-form-material.

1. Install all packages:

npm i ngx-dynamic-json-form-core ngx-dynamic-json-form-material ngx-mat-select-search --save

Please make sure, that @angular/forms and "@angular/material are already installed.

2. Add ngx-dynamic-json-form-material

The forRoot() method call is required on root level.

This method is used to override default configurations and is needed to register custom components.

More information can be found in the global configuration section in the docs.

2.1. to your AppModule (Module based)

import { NgxDynamicJsonFormMaterialModule } from "ngx-dynamic-json-form-material";

@NgModule({
  imports: [
    // ...
    NgxDynamicJsonFormMaterialModule.forRoot(),
  ],
  // ...
})
export class AppModule {}

2.2. to your appConfig.ts (Standalone Version)

import { NgxDynamicJsonFormMaterialModule } from "ngx-dynamic-json-form-material";

export const appConfig: ApplicationConfig = {
  providers: [
    importProvidersFrom(NgxDynamicJsonFormMaterialModule.forRoot()),
    // ...
  ],
};

Hint: Don't forget to add NgxDynamicJsonFormMaterialModule in your standalone component as well.

import { NgxDynamicJsonFormMaterialModule } from "ngx-dynamic-json-form-material";

@Component({
  // ...
  standalone: true,
  imports: [CommonModule, NgxDynamicJsonFormMaterialModule], // <- Must Have
  changeDetection: ChangeDetectionStrategy.OnPush, // <- Recommended
})
export class MyStandaloneComponent {
  // ...
}

3. Configure the form in the component TS

@Component({
  // ...
  changeDetection: ChangeDetectionStrategy.OnPush, // <- Recommended
})
export class MyComponent implements OnInit {
  public form: FormGroup = new FormGroup({}); // <- The form instance

  // The form fields
  public fields: FormField[] = [
    {
      type: "select",
      key: "language",
      label: "Language",
      options: [
        { label: "Language 1", value: "1" },
        // ...
      ],
    },
  ];

  // The callback method to get the instance of the form
  public setForm(form: FormGroup): void {
    this.form = form;
  }
}

4. Use ngx-dynamic-json-form in the component HTML

<ngx-dynamic-json-form [fields]="fields" (getForm)="setForm($event)" [initial]="initialValues" formClassName="my-form-class"></ngx-dynamic-json-form>