1.2.2 • Published 2 years ago

ng-flat-form v1.2.2

Weekly downloads
59
License
-
Repository
github
Last release
2 years ago

npm version Downloads


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. Or install locally by running:

  npm install ng-flat-form --save

2. Import the installed libraries

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

<div class="container">
  <ng-flat-form [form]="form"></ng-flat-form>
</div>

Component

  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

  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

  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

PropertyTypeRequiredDescription
valueTfalseget/set the value of the control
keystringtruethe object key associated with the form value, also the corresponding key for json driven forms
placeholderstringfalsecorresponds to the placeholder property of the element
requiredbooleanfalseflag for whether the required field validator is applied to the given control
disabledbooleanfalseflag for whether the given control will render as disabled
typeFlatFormControlTypetruesets the control type (input, textarea, select)
maxLengthnumberfalsecorresponds to the maxLength property of the element
minLengthnumberfalsecorresponds to the minLength property of the element
maxnumberfalsecorresponds to the max property of the element
minnumberfalsecorresponds to the min property of the element
loadingbooleanfalseflag controlling the visibility of the loading icon
hiddenbooleanfalsecorresponds to the hidden property of the element
showLoadingOnChangebooleanfalseflag for whether the loading icon should be shown when onChange is called
selectOptions{key: string, value: string}[] = []falsearray of key/value pairs to be used for select options
rowsnumberfalsecorresponds to the rows attribute of the element
showValidationbooleanfalseflag for whether or not valid/invalid icons are shown while editing the given control
manualValidationbooleanfalsedisables automatic validation allowing for manual updating of the given controls state
showLengthbooleanfalseflat for whether or not dynamic character count is shown when maxLength is also defined
onChange(event: any, control: FlatFormControl, controls: FlatFormControl[]) => void
selectOptionsAsync() => anyfalsecallback for retrieving select options dynamically
selectOptionsMap{ keyProperty: string, valueProperty: string }falsefunction for mapping selectOptionsAsync to key/value pairs as defined for selectOptions
classstringfalsefree form string for assigning custom classes to the given control
statestringfalserepresents the status of the given control at any time (valid, invalid)
debounceTimenumberfalsemilliseconds delay between an input change event and the callback to onChange
1.2.2

2 years ago

1.2.0

3 years ago

1.2.1

3 years ago

1.1.1

3 years ago

1.1.2

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.11

4 years ago

1.0.12

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.1

4 years ago