# @high-lab/dynamic-form

> An angular 10+ module that allows to generate forms by config

Latest version **0.3.19** (published 2022-06-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install @high-lab/dynamic-form
pnpm add @high-lab/dynamic-form
yarn add @high-lab/dynamic-form
bun add @high-lab/dynamic-form
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.19 |
| Published | 2022-06-21 |
| First published | 2021-09-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 364.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | michael-kravchuk |

## Links

- npm: https://www.npmjs.com/package/@high-lab/dynamic-form
- Repository: https://github.com/MichaelKravchuk/high-lab
- Homepage: https://github.com/MichaelKravchuk/high-lab#readme
- Issues: https://github.com/MichaelKravchuk/high-lab/issues
- npm.io page: https://npm.io/package/@high-lab/dynamic-form

## Dependencies (1)

- [tslib](https://npm.io/package/tslib.md) ^2.1.0

## Recent versions

- 0.3.19 (latest) — 2022-06-21
- 0.3.18 — 2022-06-21
- 0.3.17 — 2022-06-21
- 0.3.16 — 2022-06-20
- 0.3.15 — 2022-06-19
- 0.3.14 — 2022-06-19
- 0.3.13 — 2022-06-19
- 0.3.12 — 2022-06-19
- 0.3.11 — 2022-06-18
- 0.3.10 — 2022-06-18
- 0.3.9 — 2022-06-18
- 0.3.8 — 2022-06-18
- 0.3.7 — 2022-06-14
- 0.3.6 — 2022-06-08
- 0.3.5 — 2022-06-07
- … 48 more at https://npm.io/package/@high-lab/dynamic-form/versions

## README

# Angular Dynamic Form

An angular 10+ module that allows to generate forms by config

[![npm version](https://badge.fury.io/js/@high-lab%2Fdynamic-form.svg)](//npmjs.com/package/@high-lab/dynamic-form)
[![GitHub issues](https://img.shields.io/github/issues/MichaelKravchuk/high-lab.svg)](https://github.com/MichaelKravchuk/high-lab/issues)
[![GitHub stars](https://img.shields.io/github/stars/MichaelKravchuk/high-lab.svg)](https://github.com/MichaelKravchuk/high-lab/stargazers)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/MichaelKravchuk/high-lab/master/LICENSE)

## Demo
http://high-lab.com.ua/dynamic-form

## Usage


**Step 1:** Install @high-lab/dynamic-form

```sh
npm install @high-lab/dynamic-form --save
```

**Step 2:** Import angular dynamic form module into your app module,
provide default validation messages,
and your custom components for each config type

```ts
...
import { DynamicFormModule } from '@high-lab/dynamic-form';
...

import { DynamicFormConfig } from '@high-lab/dynamic-form';
import {
  FormGroupField,
  FormGroupFieldComponent,
  CheckboxField,
  CheckboxFieldComponent,
  NumberField,
  NumberFieldComponent,
  TextField,
  TextFieldComponent,
  ...
} from './fields';

export const MY_DYNAMIC_FORM_CONFIG: DynamicFormConfig = [
  { component: FormGroupFieldComponent, config: FormGroupField },
  { component: CheckboxFieldComponent, config: CheckboxField },
  { component: NumberFieldComponent, config: NumberField },
  { component: TextFieldComponent, config: TextField },
  ...
];

export function VALIDATION_MESSAGES() {
  return {
    required: 'This field can’t be blank.',
    min: params => `Value must be greater than ${params.min}`,
    ...
  };
}

@NgModule({
    ...
    imports: [
        ...
        DynamicFormModule.config(MY_DYNAMIC_FORM_CONFIG, VALIDATION_MESSAGES),
    ],
    ...
})
export class AppModule { }
```

**Example of config:** All your config must extend ```AbstractField``` or ```ControlField``` class

```ts
import { ControlField, ControlFieldInterface } from '@high-lab/dynamic-form';

export type CheckboxFieldInterface = ControlFieldInterface;

export class CheckboxField extends ControlField {
  public static readonly type = 'checkbox';

  constructor(options: CheckboxFieldInterface) {
    super(options);
  }
}
```

**Example of control:** All your control components must extend ```BaseFieldComponent``` class

```ts
import { Component, Input } from '@angular/core';
import { BaseFieldComponent } from '@high-lab/dynamic-form';
import { CheckboxField } from './checkbox-field';

@Component({
  selector: 'checkbox-field',
  templateUrl: './checkbox-field.component.html',
  styleUrls: ['./checkbox-field.component.scss']
})
export class CheckboxFieldComponent extends BaseFieldComponent {
  @Input()
  public readonly fieldConfig: CheckboxField;
}
```
**checkbox-field template**

```html
<ng-container *dynamicFormContent="self" [formGroup]="formGroup">
  <mat-checkbox class="checkbox" color="primary" [formControlName]="fieldConfig.key">{{label}}</mat-checkbox>
</ng-container>

```

**Step 3:** Use in your app

```html
<dynamic-form [form]="form">
  <div class="action-buttons" formFooter>
    <button mat-stroked-button (click)="skipChanges()" [disabled]="!form.isChangedByUser || null">Cancel</button>
    <button mat-raised-button color="primary" (click)="save($event)" [disabled]="!form.isChangedByUser || null">Save</button>
  </div>
</dynamic-form>
```

```ts
import { createDynamicForm, ExtendedFormGroup } from '@high-lab/dynamic-form';

export class SomeComponent implements OnChanges, OnDestroy {
  public form: ExtendedFormGroup;

  constructor() {
    this.form = createDynamicForm(SOME_FORM(someConfig));
  }
}

export const SOME_FORM = (someConfig: any): AbstractField[] => [
  ...
  new NumberField({
    key: 'age',
    label: `Age`,
    order: 1,
    min: 12,
    max: 150,
    initialValue: { value: '', disabled: someConfig.ageDisabled },
    validatorOrOpts: [Validators.min(0), Validators.max(150)],
    relatedFields: [{
      checkVisibility: (value, formValue) => value >= 18,
      configs: [
        new TextField({
          key: 'someKey',
          label: `Some Label`,
          order: 2,
        }),
      ]
    } , {
      checkVisibility: (value, formValue) => value >= 60,
      configs: [
        new TextField({
          key: 'someKey',
          label: `Some Label 2`,
          order: 2,
        }),
        new FormGroupField({
          key: 'size',
          order: 3,
          configs: [
            new NumberField({
              key: 'width',
              label: `Width`,
              initialValue: 30,
            }),
            new NumberField({
              key: 'height',
              label: `Height`,
              initialValue: 30,
            })
          ]
        }),
      ]
    }]
  })
];
```

## License
[MIT](https://choosealicense.com/licenses/mit/)

---
_Source: https://npm.io/package/@high-lab/dynamic-form · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
