0.0.14 • Published 3 years ago

es-dynamic-form v0.0.14

Weekly downloads
16
License
MIT
Repository
github
Last release
3 years ago

DynamicForm

This library was generated with Angular CLI version 9.1.13.

API

import { DynamicFormModule } from 'dynamic-form' selector: <dynamic-form>

@Inputs()

InputTypeRequiredDescription
submitButtonbooleanOptional, default: trueshowing the submit button of the form
formGroupListFormGroupRequiredFormGroup on which the form is built
styleClassstringOptional, default: 'dynamic-form'form container class
submitNamestringOptional, default: 'Submit'submit button name
debugbooleanOptional, default: falseshowing the current value of the FormGroup

@Outputs()

OutputTypeRequiredDescription
onSubmitFormEventEmitter<FormGroup'value'>Optionalform event when clicking on the Submit button

Available properties of the "question" variable

PropertyTypePurpose
valuestring or number or booleanvalue for the input field
keystring or numberkey in the FormGroup
labelstring or numberlabel for the input field
controlTypestringthe type of FormControl used
requiredbooleanis the field required
optionsanycontainer for user settings
_others_anyinherited from FormControl

|Method|Return type|

The current list of FormControls

FormCheckBox

FormText

FormNumber

FormDropDown

FormColor

FormRange

Usage

1) Register the DynamicFormModule in your app module. import { DynamicFormModule } from 'es-dynamic-form'

app.module.ts

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';

import {AppComponent} from './app.component'; import {CommonModule} from '@angular/common'; import {FormsModule, ReactiveFormsModule} from '@angular/forms'; import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; import {DynamicFormModule} from 'es-dynamic-form';

@NgModule({ declarations: AppComponent,
, imports: CommonModule, BrowserAnimationsModule, BrowserModule, ReactiveFormsModule, FormsModule, DynamicFormModule, , entryComponents: [], bootstrap: AppComponent }) export class AppModule { }

2) Create your `FormGroup` in parent component
  > `app.component.ts`

``` typescript
import {DynamicFormBuilder, FormColor, FormDropDown, FormNumber, FormText, IFormChangeEvent} from 'es-dynamic-form';

export class AppComponent implements OnInit {
    private readonly cars = [
        {label: 'Audi', value: 'Audi'},
        {label: 'BMW', value: 'BMW'},
        {label: 'Fiat', value: 'Fiat'},
    ];
    public fg: FormGroup;

    constructor() {
    }

    ngOnInit() {
        this.fg = new FormGroup({
          checkbox: new FormText({value: true, key: 'checkbox', label: 'checkbox'}),
          text: new FormText({value: 'simple text', key: 'text', label: 'insert text'}),
          drop: new FormDropDown({
              value: this.cars[0].value, key: 'drop', label: 'cars list', options: {items: this.cars}
          }),
          arrayNumber: new FormArray([
              new FormNumber({value: 1, key: 0, label: 'number 1'}, Validators.min(0)),
              new FormNumber({value: 2, key: 1, label: 'number 2'}, Validators.min(0)),
              new FormNumber({value: 3, key: 2, label: 'number 3'}, Validators.min(0)),
          ]),
          color: new FormColor({value: '#fff222', key: 'color', label: 'choose color'}),
      });
    }
}

3) Add the created FormGroup to the 'dynamic-form' component

app.component.html

<dynamic-form [FormGroupList]="fg" [submitButton]="true"></dynamic-form>

4) Done, the form will be automatically created on the page

alt text

Using custom templates

Any component or template can be used. Adding a custom template is done via the questTemplate directive. It accepts as input an array of pattern names to replace with the current one.

    <ng-template [questTemplate]="['checkbox']"></ng-template>

The context passed to has the following structure

"context" : {
    $implicit: question <FormNumber | FormText | ... | FormDropDown>,
    fg: formGroup <FormGroup>
}

question - FormControl for the current template

fg - the FormGroup that the question belongs to

Let's say we want to use the p-checkbox from PrimeNG. The replacement template will look like this
<dynamic-form
    [FormGroupList]="fg"
    [submitButton]="true"
>
    <ng-template [questTemplate]="['checkbox']" let-control let-formgroup="fg">
        <div [formGroup]="formgroup">            
            <p-checkbox
                    [formControlName]="control.key" 
                    [binary]="true"                    
                    [label]="'primeng checkbox'"
            ></p-checkbox>
            <!--or [formControl]="control"-->
        </div>
    </ng-template>
</dynamic-form>

alt text

If you want to use 1 template for several FormControls, it is enough to specify the replaced template types in the questTemplate array separated by commas
    <ng-template [questTemplate]="['text', 'color', 'number', 'dropdown']" let-formcontrol>
        <span style="font-size: 20px; color: green">My custom template for {{formcontrol.controlType}}</span>
    </ng-template>

alt text

0.0.14

3 years ago

0.0.12

3 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago