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

pao-form

v0.2.15

Published

A lightweight form builder library for both TypeScript and plain JavaScript

Downloads

5

Readme

Pao Form Builder

A lightweight form builder library for both TypeScript and plain JavaScript, inspired by Angular's FormBuilder. This library provides a simple API for creating and managing forms with FormControl, FormGroup, and FormArray.

Getting Started

  1. Install the library in your project:

    // via npm
    npm install pao-form
    
    // ... or just plug & play import
  2. Import the library into your TypeScript or JavaScript files:

      // For TypeScript
      import { FormBuilder, FormGroup, FormControl, FormArray } from 'pao-form';
    
      // For Plug & Play JavaScript 
      import { FormBuilder, FormGroup, FormControl, FormArray } from 'https://cdn.jsdelivr.net/npm/pao-form@latest/dist/pao-form.min.js';
    

Usage

Create a Form

  // Initialize the FormBuilder
  const fb = new FormBuilder();
  
  // Create controls with validators
  const nameControl = fb.control('John', [Validator.required]);
  const ageControl = fb.control(25, [Validator.number]);
  
  // Create a form group
  const userFormGroup = fb.group({
    name: nameControl,
    age: ageControl,
  });
  
  // Create an array of controls
  const hobbiesArray = fb.array([
    fb.control('Reading'),
    fb.control('Traveling'),
  ]);

  // It is required to validate the user form group initially
  userFormGroup.validateAll()

Subscribe to Changes

    // Subscribe to changes in the entire form group
    userFormGroup.subscribe(value => {
      console.log('Form value changed:', value);
    });

   
    // Subscribe to changes in a specific control
    nameControl.subscribe(value => {
      console.log('Name control changed:', value);
    });

Set & Clear Values

  // Set values for the entire form group
  userFormGroup.setValue({
    name: 'Bob',
    age: 28,
  });

    
  // Clear values for the entire form group
  userFormGroup.clearValue();

 

Nested Group Controls

  // Create nested form groups
  const addressGroup = fb.group({
    city: fb.control('New York'),
    postalCode: fb.control('10001'),
  });
  
  // Add nested group to the main form group
  userFormGroup.addControl('address', addressGroup);

Validators example

    const Validators = {
          required: {
            validator: (value) => !!value,
            errorMessage: 'This field is required!',
          },      
          number: {
            validator: (value) => !isNaN(value),
            errorMessage: 'Invalid number!',
          }
    }

Dynamically Adding Controls to FormArray

  // Dynamically add controls to a FormArray
  hobbiesArray.controls.push(fb.control('Coding'));

Accessing Values

  // Access values in the form group
  const formValues = fb.getValue();
  console.log('Form values:', formValues);

HTML Element Association

To associate the form controls with HTML elements, you can use the provided control names in element id when defining your HTML. For example:

<!-- Input for 'name' control -->
<input type="text" id="name" />
<!-- display of 'name' control validator message by prefixing Error -->
<div id="nameError"></div>

<!-- Input for 'age' control -->
<input type="number" id="age"/>
<!-- display of 'name' control validator message by orefing Error -->
<div id="ageError"></div>

<!-- Nested group controls -->
<div id="address">
  <input type="text" id="city"/>
  <div id="cityError"></div>

  <input type="text" id="postalCode" />
  <div id="postalCodeError"></div>

</div>

How to run development server?

git clone [email protected]:josnin/pao-form.git 
cd ~/Documents/pao-form/
npm install
npm run dev

Help

Need help? Open an issue in: ISSUES

Contributing

Want to improve and add feature? Fork the repo, add your changes and send a pull request.

License

This library is released under the MIT License.