# ng-flat-form

> JSON driven dynamic, and reactive forms for Angular applications

Latest version **1.2.2** (published 2021-12-09) · 0 weekly downloads

## Install

```sh
npm install ng-flat-form
pnpm add ng-flat-form
yarn add ng-flat-form
bun add ng-flat-form
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.2 |
| Published | 2021-12-09 |
| First published | 2020-01-31 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 745.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Austen DeWolf |
| Maintainers | deausten |

## Links

- npm: https://www.npmjs.com/package/ng-flat-form
- Repository: https://github.com/deausten/ng-flat-form
- Homepage: https://github.com/deausten/ng-flat-form#readme
- Issues: https://github.com/deausten/ng-flat-form/issues
- npm.io page: https://npm.io/package/ng-flat-form

## Dependencies (1)

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

## Recent versions

- 1.2.2 (latest) — 2021-12-09
- 1.2.1 — 2021-10-22
- 1.2.0 — 2021-10-22
- 1.1.2 — 2021-10-18
- 1.1.1 — 2021-10-15
- 1.0.24 — 2021-10-14
- 1.0.23 — 2021-10-13
- 1.0.22 — 2021-10-13
- 1.0.21 — 2021-10-13
- 1.0.20 — 2021-09-19
- 1.0.19 — 2021-09-16
- 1.0.18 — 2021-09-16
- 1.0.17 — 2021-09-16
- 1.0.16 — 2021-09-16
- 1.0.15 — 2021-09-15
- … 16 more at https://npm.io/package/ng-flat-form/versions

## README

<div align="center">
  <h2>ng-flat-form</h2>
  <br />
  JSON driven dynamic forms in a compact form UI for Angular. Package can be downloaded via npm <a href="https://www.npmjs.com/package/ng-flat-form">here</a>.
  <br />
  <br />

[![npm version](https://badge.fury.io/js/ng-flat-form.svg)](https://badge.fury.io/js/ng-flat-form)
[![Downloads](https://img.shields.io/npm/dt/ng-flat-form?style=flat-square)](https://www.npmjs.com/package/ng-flat-form)
</div>

---

# NgFlatForm

Angular 8+ static and dynamic forms library. Supports validation, async calls, and floating labels. Support for the following input types:
- text
- number
- password
- email
- textarea
- date
- select

## Getting started

#### 1. Install ng-flat-form
Download from npm [here](https://www.npmjs.com/package/ng-flat-form). Or install locally by running:
```bash
  npm install ng-flat-form --save
```

#### 2. Import the installed libraries
```ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { NgFlatFormModule } from 'ng-flat-form/lib/ng-flat-form.module';

import { AppComponent } from './app';

@NgModule({
  ...
  imports: [
    ...,
    NgFlatFormModule
  ],
})
export class AppModule {}
```

#### 3. Create a flat-form in in your template
**View Template**
```html
<div class="container">
  <ng-flat-form [form]="form"></ng-flat-form>
</div>
```

**Component**
```ts
  public form: FlatForm;

  constructor() { }

  ngOnInit(): void {
    this.initializeForm();
  }

  private initializeForm(): void {
    this.form = new FlatForm([
      new FlatFormControlGroup({
        title: '',
        description: '',
        controls: [
          new FlatFormControl({
            class: 'border-bottom half border-right',
            key: 'name',
            placeholder: 'Name',
            type: FlatFormControlType.INPUT_TEXT,
            required: true,
            showValidation: true,
            showLength: true,
          }),
          new FlatFormControl({
            class: 'border-bottom half',
            key: 'email',
            placeholder: 'Email',
            type: FlatFormControlType.INPUT_EMAIL,
            required: true,
            showValidation: true,
            manualValidation: true,
            showLength: true,
            onChange: this.handlePasswordChange,
            debounceTime: 1000,
          })
        ]);
      })
    ];
  }
```

#### 4. Subscribe to form changes

**Component**
```ts
  public form: FlatForm;

  constructor() { }

  ngOnInit(): void {
    this.initializeForm();
    this.handleFormChange();
  }

  private handleFormChange(): void {
    this.form.statusChanges.subscribe((event: any) => {
      console.log(this.form.value);
    });
  }
```

#### 5. Set form value
You can set the forms value in two ways: During initialization, by providing a value to the `value` property within `FlatFormControl`. 
Or by calling `setForm({..})` on the `FlatForm` instance.

**Component**
```ts
  public form: FlatForm;

  constructor() { }

  ngOnInit(): void {
    this.initializeForm();
    this.handleFormChange();
    this.setForm();
  }

  private setForm(): void {
    this.form.setValue({
      name: 'Ron Swanson',
      email: 'ron.swanson@pnr.giv'
    });
  }
```

## Properties
**FlatFormControl<T>**

| Property      | Type         | Required | Description |
| ------------- |-------------| -----| ----|
value           | T            | false | get/set the value of the control |
key | string | true | the object key associated with the form value, also the corresponding key for json driven forms |
placeholder | string | false | corresponds to the placeholder property of the <input /> element
required | boolean | false | flag for whether the required field validator is applied to the given control 
disabled | boolean | false | flag for whether the given control will render as disabled
type | FlatFormControlType | true | sets the control type (input, textarea, select)
maxLength | number | false | corresponds to the maxLength property of the <input /> element
minLength | number | false | corresponds to the minLength property of the <input /> element
max | number | false | corresponds to the max property of the <input /> element
min | number | false | corresponds to the min property of the <input /> element
loading | boolean | false | flag controlling the visibility of the loading icon |
hidden | boolean | false | corresponds to the hidden property of the <input /> element |
showLoadingOnChange | boolean | false | flag for whether the loading icon should be shown when onChange is called |
selectOptions | {key: string, value: string}[] = [] | false | array of key/value pairs to be used for select options |
rows | number | false | corresponds to the rows attribute of the <textarea /> element |
showValidation | boolean | false | flag for whether or not valid/invalid icons are shown while editing the given control |
manualValidation | boolean | false | disables automatic validation allowing for manual updating of the given controls state |
showLength | boolean | false | flat for whether or not dynamic character count is shown when maxLength is also defined |
onChange | (event: any, control: FlatFormControl<T>, controls: FlatFormControl<T>[]) => void | |
selectOptionsAsync | () => any | false | callback for retrieving select options dynamically |
selectOptionsMap | { keyProperty: string, valueProperty: string } | false | function for mapping selectOptionsAsync to key/value pairs as defined for selectOptions |
class | string | false | free form string for assigning custom classes to the given control |
state | string | false | represents the status of the given control at any time (valid, invalid) |
debounceTime | number | false | milliseconds delay between an input change event and the callback to onChange |

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