Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@labshare/ngx-forms

Package Overview
Dependencies
Maintainers
17
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@labshare/ngx-forms

[![Greenkeeper badge](https://badges.greenkeeper.io/LabShare/ngx-forms.svg?token=4a12f6b1da0f082ac1bbf2c72bbcaf01b001705746c61c36eee1db6bda3d7c11&ts=1528346534395)](https://greenkeeper.io/) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93

  • 8.0.0
  • npm
  • Socket score

Version published
Weekly downloads
6
decreased by-40%
Maintainers
17
Weekly downloads
 
Created
Source

Greenkeeper badge semantic-release Build Status

ngx-forms

Dynamic form generator, creates Angular Reactive forms from json schema

Alt text

Installation

Install Package npm i --save @labshare/ngx-forms

Add import to the main module

import { NgxFormModule } from '@labshare/ngx-forms';

@NgModule({
  imports: [
    NgxFormModule
  ]
})

Add html tag with bindings

<dynamic-form [formConfig]="config" #form="dynamicForm" [model]="data"></dynamic-form>
  • config - json array that contains fields definitions
  • #form="dynamicForm" - Output. Bind to dynamicForm object that has form output object
  • model - Input data. One way binding only

Add reference in the component controller

export class MyFormComponent {
    @ViewChild('form') public formReference: DynamicFormDirective;

    public const config = {
      layout: 'basic',
      fields: [ 
        { type: 'text', label: 'Title', name: 'title' },
        { type: 'text', label: 'Project Name', name: 'projectName', placeholder: 'Enter project name', minLength: 2, maxLength: 5 },
      ]
    };
 
    public const model = { title: 'Example', projectName: 'Demo' }
    
}

Extending form

ngx-forms can be extended with custom layout or custom inputs

export const customFields = {
  peoplePicker: PeoplePickerComponent,
};

export const customLayouts: LayoutDictionary = {
  myCustomLayout: CustomLayoutComponent,
};

@NgModule({
  imports: [
    ReactiveFormsModule,
    FormsModule,
    CommonModule,
    NgxFormModule.forRoot({
      layoutDictionary: customLayouts,
      fieldDictionary: customFields,
    }),
  ],
  declarations: [PeoplePickerComponent],
  entryComponents: [CustomLayoutComponent, PeoplePickerComponent],
})
export class NgxFormsExtensionsModule {}

then use custom fields and layouts in the form config:

{  
    layout: 'myCustomLayout',
    fields: [ 
        { type: 'text', label: 'Title', name: 'title' },
        { type: 'peoplePicker', label: 'People', name: 'people' },
    ]
};

Creating custom layouts

Custom layout component needs to extend BaseLayout from ngx-forms:

import { Component } from '@angular/core';
import { BaseLayout } from '@labshare/ngx-forms/src/app/layouts/base-layout';

@Component({
  selector: 'custom-forms-layout',
  template: require('./custom-layout.component.html'),
})
export class CustomLayoutComponent extends BaseLayout {}

Custom layout template must have entry directive dynamicField through which an input will be injected:

<div class="row">
    <div class="col-12">
        <div *ngFor="let field of formConfig.fields" class="mb-2">
            <ng-container *ngIf="!field.hidden">
                <label>{{field.label}}
                </label>
                <div dynamicField [field]="field" [group]="group" [model]="model"></div>
            </ng-container>
        </div>
    </div>
</div>

Creating custom inputs

Inputs can be of any kind and return objects, arrays, strings etc.

Creating and field itself is not difficult but in most cases you may have to create a custom ControlValueAccessor

Custom field must implement Field interface from ngx-forms

import {Component} from '@angular/core';
import {FormGroup} from '@angular/forms';
import {FieldConfig, Field} from '@labshare/ngx-forms';

@Component({
  selector: 'field-editor',
  template: require('./field-editor.component.html'),
})
export class PeoplePickerComponent implements Field {
  public field: FieldConfig;
  public group: FormGroup;
}

And template:

<ng-container [formGroup]="group">
    <people-picker-accessor [options]="field.options" [formControlName]="field.name"></people-picker-accessor>
</ng-container>

In this case people-picker-accessor is a custom ControlValueAccessor

Overwriting existing types

It is possible to overwrite existing out of the box fields and layouts by simply using their names in the dictionary in forRoot. For example if you need to overwrite text input field:

export const customFields = {
  text: PeoplePickerComponent,
};

Field options and examples

NameTypeDescriptionExample
disabled?booleandisable field if truedisabled: true
label?stringfield labellabel: "Project Title"
namestringfield namename: "projectTitle"
options?string[]options for <select> dropdownoptions: [ "Option 1", "Option 2" ]
placeholder?stringtext placeholderplaceholder: "Enter Project Title"
typestringfield type (see field type description)type: "select"
value?anyfield valuevalue: 123
required?booleanValidation: required or notrequired: true
minLength?numberValidation: minimum length of a text fieldminLength: 5
maxLength?numberValidation: maximum length of a text fieldmaxLength: 12
email?booleanValidation: field must be an email addressemail: true
min?numberValidation: minumum value for number fieldsmin: 100
max?numberValidation: maximum value for number fieldsmax: 1000
pattern?RegExpValidation: regular expressionpattern: "^[a-zA-Z0-9_]*$"
nullValidator?anyValidation: null validationnullValidator: true
hiddenbooleanhide the field by default when the form loadinghidden: true

Out of box field types:

  • text - text input <input type="text">
  • select - text input <select>
  • textarea - text input <textarea>
  • editor - Rich text editor based on ngx-quill
  • hidden - hidden value field <input type="hidden">.
  • radio - radio buttons
  • checkbox - checkbox buttons
  • date - datepicker

FAQs

Package last updated on 23 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc