3.0.0 • Published 11 months ago

@overflo-srl/dynamic-form v3.0.0

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
11 months ago

npm version Overflo Live Demo Angular Angular material

@overflo-srl/dynamic-form

Generalized and responsive angular reactive form library combined with angular material.

Supports the following form control types: select multipleselect text textarea radio checkbox date datetime inputchips

This library was developed and is maintained by Overflo team.

Installation

Run npm i --save @overflo-srl/dynamic-form.

Add DynamicFormModule and BrowserAnimationsModule to the imports list in your angular module

import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; 
import { DynamicFormModule } from 'dynamic-form';
imports: [
    BrowserAnimationsModule,
    DynamicFormModule,
],

Usage examples

Readonly form for displaying data

Add the component in your component.html

<lib-dynamic-form [forms]="forms"></lib-dynamic-form>

Initialize the form in your component.ts

  forms: BaseForm[] = [];
  
  ngOnInit(): void {
    const forms = [
      new BaseForm({
        key: 'name',
        value: 'John',
        label: 'Name',
        controlType: 'text',
        readonly: true,
      }),
      new BaseForm({
        key: 'surname',
        value: 'Doe',
        label: 'Surname',
        controlType: 'text',
        readonly: true,
      }),
    ];

    this.forms = this.forms.concat(forms);
  }

Form with default submit and required parameters

Add the component in your component.html

<lib-dynamic-form [forms]="forms" (send)="save($event)" [showButtonAndEmit]="true" [showConfirmationDialog]="true"></lib-dynamic-form>

Initialize the form in your component.ts

  forms: BaseForm[] = [];
  
  ngOnInit(): void {
    const forms = [
      new BaseForm({
        key: 'name',
        value: 'John',
        label: 'Name',
        controlType: 'text',
        required: true
      }),
      new BaseForm({
        key: 'surname',
        value: 'Doe',
        label: 'Surname',
        controlType: 'text',
        required: true
      }),
    ];

    this.forms = this.forms.concat(forms);
  }
  
  save(formResult: {name: string, surname: string}) {
    console.log(formResult);
  }

Form with custom submit

Add the component in your component.html

<lib-dynamic-form #dynamicForm [forms]="forms" (send)="save($event)" [showButtonAndEmit]="false" [showConfirmationDialog]="false"></lib-dynamic-form>
<button (click)="confirm()">save</button>

Initialize the form in your component.ts

  @ViewChild(DynamicFormComponent) dynamicFormComponent!: DynamicFormComponent;
  forms: BaseForm[] = [];
  submitting: boolean = false;
  
  ngOnInit(): void {
    const forms = [
      new BaseForm({
        key: 'name',
        value: 'John',
        label: 'Name',
        controlType: 'text',
      }),
      new BaseForm({
        key: 'surname',
        value: 'Doe',
        label: 'Surname',
        controlType: 'text',
      }),
    ];

    this.forms = this.forms.concat(forms);
  }

  confirm() {
    this.submitting = true;
    this.dynamicFormComponent.onSubmit();
    this.submitting = false;
  }

  save(formResult: {name: string, surname: string}) {
    if (!this.submitting) {
      return;
    }
    console.log(formResult);
  }

Form with full width fields

Add the component in your component.html

<lib-dynamic-form [forms]="forms"></lib-dynamic-form>

Initialize the form in your component.ts

  forms: BaseForm[] = [];
  
  ngOnInit(): void {
    const forms = [
      new BaseForm({
        key: 'name',
        value: 'John',
        label: 'Name',
        controlType: 'text',
        readonly: true,
        fullWidth: true
      }),
      new BaseForm({
        key: 'surname',
        value: 'Doe',
        label: 'Surname',
        controlType: 'text',
        readonly: true,
        fullWidth: true
      }),
    ];

    this.forms = this.forms.concat(forms);
  }

Form with live preview

Add the component in your component.html

<lib-dynamic-form [forms]="forms" (send)="preview($event)" [showButtonAndEmit]="false" [showConfirmationDialog]="false"></lib-dynamic-form>
<div>{{fullName}}</div>

Initialize the form in your component.ts

  forms: BaseForm[] = [];
  fullName?: string;
  
  ngOnInit(): void {
    const forms = [
      new BaseForm({
        key: 'name',
        value: 'John',
        label: 'Name',
        controlType: 'text',
      }),
      new BaseForm({
        key: 'surname',
        value: 'Doe',
        label: 'Surname',
        controlType: 'text',
      }),
    ];

    this.forms = this.forms.concat(forms);
  }

  preview(formResult: {name: string, surname: string}) {
    const {name, surname} = formResult;
    this.fullName = `${name} ${surname}`;
  }

Documentation

DynamicFormComponent

<lib-dynamic-form></lib-dynamic-form>

ParameterTypeDefaultDescription
formsBaseForm[][]Form elements
(send)EventEmitter<any>-Returns form value
showButtonAndEmitbooleantrueDisplays save button and enables (send) emitter
justHideButtonbooleanfalseHides save button
partialSendbooleantrueReturns form result even if invalid
showConfirmationDialogbooleantrueDisplays confirmation dialog

BaseForm

new BaseForm()

ParameterTypeDefaultDescription
valueanyundefinedValue of the form field
keystringempty stringForm field property name (will match the returned value)
labelstringempty stringForm field label (Visible field name)
helpstringundefinedForm field help tooltip (Will show after the label)
requiredbooleanundefinedRequired form field
ordernumber1Form field order
controlType'select' | 'multipleselect' | 'text'| 'password' | 'textarea' | 'radio' | 'checkbox' | 'date' | 'datetime' | 'photos'empty stringForm field type
typestringempty stringinput type (works only with controlType text and textarea)
options{ key: string; value: any; }[][]Select options (works only with controlType select and multipleselect)
optionsFunc(form: any) => { key: string; value: any }[]undefinedArrow function which take a form as parameter and returns an array of options ( {key: string; value: any} ). The returned array will replace the current array of options for the select.
searchWithDebounceFunc(searchValue: string) => Promise<SelectOptionModel[]>undefinedAsync arrow function implemented on select search and taking the search value as parameter. It will replace the options given in the options property with the options returned from this function. This property is only implemented in controls of types select and the options value should not be an Object.
debounceTime: numbernumber300A number used to set the debounce time of searchWithDebouceFunc.
readonlybooleanfalseReadonly form field
fullWidthbooleanfalseFull width form field
modifyValueOnValueChange(value: string) => stringundefinedArrow function implemented on value change of the input and taking that value as parameter. It will replace the current value with the string returned from this function. This property is only implemented in controls of types text, textarea, and password.
validatorsCustomValidator[]undefinedAn array of validators that will be added to the control. It can be used to set many CustomValidator returning null or a CustomError containing a message that will be displayed on top of form control.
3.0.0

11 months ago

2.1.0

1 year ago

2.0.0

1 year ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.1

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.6

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago