npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gabrielmedeiros2/json-auto-forms

v1.0.1

Published

<h1 align="center">Json Auto Forms</h1>

Downloads

2

Readme

npm npm downloads

GitHub contributors GitHub stars

Installing

$ npm install --save json-auto-forms

Quickstart

Import json-auto-forms module

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
      AutoFormLibModule,
  ]
  ...
})
...

Usage

On your component, set the json object corresponding to the form you want to be generated and create a function to receive the submitted form.

import {FormLayout} from "./form-layout.model";

@Component({
  selector: 'my-feature',
  templateUrl: './my-feature.component.html',
  styleUrls: ['./my-feature.component.css'],
  standalone: true
})
export class MyFeatureComponent {
  myForm: FormLayout = {
    id: 1,
    fields: [
      {
        name: 'Text Field',
        type: FormFieldType.TEXT_INPUT,
        fieldWidth: '100%',
        order: 1,
        useMatError: true,
        validators: [
          {
            name: FormFieldValidatorEnum.REQUIRED
          }
        ]
      },
      {
        name: 'Number Field',
        type: FormFieldType.NUMBER_INPUT,
        fieldWidth: '49%',
        order: 2,
        useMatError: true
      },
      {
        name: 'Date Field',
        type: FormFieldType.DATE_INPUT,
        fieldWidth: '49%',
        order: 3,
        useMatError: true
      },
    ]
  }

  public mySubmittedValues(form: FormGroup): void {
    console.log(form.getRawValue());
  }
}

In the HTML, insert the library tag making references to the previous steps configurations

<auto-form [formLayout]="myForm" (submitForm)="mySubmittedValues($event)"></auto-form>

For form array usage:

TS

export class MyFeatureComponent {
  myAddFormEventEmmiter: EventEmmiter<any> = new EventEmmiter<any>();
  /* Your code here */
}

HTML

<button click="myAddFormEventEmmiter.emit()">Add Form</button>
<auto-form-array [formLayout]="myForm" [addItem]="myAddFormEventEmmiter" (submitForm)="mySubmittedValues($event)"></auto-form-array>

Form options

Input types

  FormFieldType.TEXT_INPUT;
  FormFieldType.FILE_INPUT;
  FormFieldType.NUMBER_INPUT;
  FormFieldType.DATE_INPUT;
  FormFieldType.DATE_TIME_INPUT;
  FormFieldType.RADIO_INPUT;
  FormFieldType.PASSWORD_INPUT;
  FormFieldType.TEXT_AREA;
  FormFieldType.SELECTBOX;
  FormFieldType.CHECKBOX;

Validator types

  FormFieldValidatorEnum.REQUIRED;
  FormFieldValidatorEnum.EMAIL;
  FormFieldValidatorEnum.MAX_LENGTH;
  FormFieldValidatorEnum.MIN_LENGTH;
  FormFieldValidatorEnum.FUNCTION;

Custom Validators

On this version, now it's possible to create your own custom validators. To archieve that, use the new validator type "function".

import {AbstractControl, ValidationErrors, ValidatorFn} from "@angular/forms";

// Create your custom validator
const myCustomValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
    if(control.value === 'invalid') {
        const error = {customValidator: true};
        control.setErrors(error);
        return error;
    }
    
    return null;
}

const form = {
  /* Form configurations */
  fields: [
    {
      name: 'Text Field',
      type: FormFieldType.TEXT_INPUT,
      fieldWidth: '100%',
      order: 1,
      useMatError: true,
      validators: [
        {
          name: FormFieldValidatorEnum.FUNCTION,
          function: myCustomValidator
        }
      ]
    },
    /* Other fields */
  ]
}

Custom validator messages

TS

export class MyFeatureComponent {
  public myCustomValidatorMessages: ValidationText = {
    required: 'Field is required',
    email: 'Field must be an email',
    minLength: 'Field must contain at least x characters',
    maxLength: 'Field must not contain more than x characters',
    function: 'Field validation failed'
  };
  /* Your component code here */
}

HTML

<auto-form [formLayout]="myForm" [validationTextList]="myCustomValidatorMessages" (submitForm)="mySubmittedValues($event)"></auto-form>

Custom submit button styles

CSS

.myCustomStyle {
  backgroud-color: blue;
  color: white;
  border-radius: 12px;
}

TS

export class MyFeatureComponent {
  myCustomStyleString: string = 'myCustomStyle';
...
}

HTML

<auto-form [formLayout]="myForm" [submitStyles]="myCustomStyleString" (submitForm)="mySubmittedValues($event)"></auto-form>

Add more action buttons

HTML

<auto-form [formLayout]="myForm" [submitStyles]="myCustomStyleString" (submitForm)="mySubmittedValues($event)">
  <button type="button" (click)="goBack()">Go back to previous page</button>
</auto-form>