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

@lucaslab/forms

v1.0.0

Published

**@lucaslab** is an Angular library designed to dynamically generate forms based on configurable options. It supports validation, custom validators, and allows easy handling of form submissions and cancellations, making form generation flexible and fast.

Downloads

3

Readme

@lucaslab Packages

@lucaslab is an Angular library designed to dynamically generate forms based on configurable options. It supports validation, custom validators, and allows easy handling of form submissions and cancellations, making form generation flexible and fast.

Table of Contents

Installation

To install the @lucaslab/form package in your Angular project, run the following command:

npm install @lucaslab/form

Usage

Step 1: Import the FormComponent

In your Angular application, import FormComponent from @lucaslab/form. This component will render the form based on the configuration you provide.

import { FormComponent, FormConfiguration } from "@lucaslab/form";

Step 2: Define Your Form Configuration

You need to provide an array of form configuration objects that define each field in the form. Each configuration object includes properties like label, property, type, and optional validators.

configurationForm: FormConfiguration[] = [
  {
    label: 'Name',
    property: 'name',
    placeholder: 'name',
    type: 'text',
  },
  {
    label: 'Email',
    property: 'email',
    placeholder: '[email protected]',
    type: 'email',
    validators: [Validators.required, Validators.email],
  },
  {
    label: 'Phone',
    property: 'phone',
    placeholder: '000000000',
    type: 'number',
    validators: [Validators.required, phoneValidator()],
  },
];

Configuration Options

The FormConfiguration interface defines how each form field is rendered and validated. Below are the available configuration options:

| Property | Type | Description | | -------------- | --------------- | ---------------------------------------------------------------- | | label | string | The label displayed above the form field. | | property | string | The form control name (used for binding the value). | | placeholder | string | Placeholder text for the form field. | | type | string | The type of input (text, email, number, password, etc.). | | validators | ValidatorFn[] | Optional array of validators (Angular built-in or custom). | | options | any[] | (Optional) Used for select dropdowns or radio buttons. | | initialValue | any | (Optional) Initial value of the form field. |

Step 3: Add the FormComponent to Your Template

In your component template, use the <lucaslab-form> component to generate the form dynamically. Pass the configurationForm to the [configurations] input and listen for form submission and cancellation events.

<lucaslab-form [configurations]="configurationForm" (submittedData)="onSubmit($event)" (cancelEvent)="cancel()"></lucaslab-form>

Custom Validators

You can define custom validators like the phoneValidator() function below, which ensures that the phone number is valid.

export function phoneValidator(): ValidatorFn {
  return (control: AbstractControl): ValidationErrors | null => {
    const validPhoneNumber = /^\d{10}$/; // Example: 10-digit phone number
    const isValid = validPhoneNumber.test(control.value);
    return isValid ? null : { invalidPhoneNumber: { value: control.value } };
  };
}

Add this custom validator to the form configuration:

{
  label: 'Phone',
  property: 'phone',
  placeholder: '000000000',
  type: 'number',
  validators: [Validators.required, phoneValidator()],
}

Step 4: Handle Events

The FormComponent emits two main events:

  1. (submittedData): Triggered when the form is submitted. The data object containing the form values will be passed as the event payload.
  2. (cancelEvent): Triggered when the cancel button is pressed.

Example

Here’s a complete example of how to use the @lucaslab/form library in an Angular application:

import { Component } from "@angular/core";
import { AbstractControl, ValidationErrors, ValidatorFn, Validators } from "@angular/forms";
import { FormComponent, FormConfiguration } from "@lucaslab/form";

export function phoneValidator(): ValidatorFn {
  return (control: AbstractControl): ValidationErrors | null => {
    const validPhoneNumber = /^\d{10}$/; // Example: 10-digit phone number
    const isValid = validPhoneNumber.test(control.value);
    return isValid ? null : { invalidPhoneNumber: { value: control.value } };
  };
}

@Component({
  selector: "app-root",
  standalone: true,
  template: `
    <h1>Welcome to {{ title }}!</h1>
    <lucaslab-form [configurations]="configurationForm" (submittedData)="onSubmit($event)" (cancelEvent)="cancel()"></lucaslab-form>
  `,
  styles: [``],
  imports: [FormComponent],
})
export class AppComponent {
  title = "form-sample-app";

  configurationForm: FormConfiguration[] = [
    {
      label: "Name",
      property: "name",
      placeholder: "name",
      type: "text",
    },
    {
      label: "Email",
      property: "email",
      placeholder: "[email protected]",
      type: "email",
      validators: [Validators.required, Validators.email],
    },
    {
      label: "Phone",
      property: "phone",
      placeholder: "000000000",
      type: "number",
      validators: [Validators.required, phoneValidator()],
    },
  ];

  onSubmit(data: any) {
    alert(JSON.stringify(data, null, 2));
  }

  cancel() {
    console.log("canceled");
  }
}

Contributing

We welcome contributions to the @lucaslab/form library. If you have ideas for new features, improvements, or bug fixes, feel free to submit a pull request or open an issue.

License

This project is licensed under the MIT License. See the LICENSE file for details.


By following this guide, you can now generate dynamic forms, define custom validation logic, and easily manage form submissions with @lucaslab/form.