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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@jkr26/react-forms-builder-logic

v1.1.2

Published

Small library to simplify the use of forms in React.

Readme

Small library to simplify the use of forms in React

build status Coverage Status NPM dependencies license NPM downloads code style: prettier Last commit PRs welcome node version minzipped size

With a simple higher-order component (HOC), you can get:

  1. The values of the inputs.
  2. The status of the form.
  3. Control of validations.

Examples.

Getting Started

Installation

You can install with NPM: @jkr26/react-forms-builder-logic

npm i --save @jkr26/react-forms-builder-logic 

Step 1: Create your component and do the HOC

import { formWrapper, Form } from "@jkr26/react-forms-builder-logic";

class ExampleFormComponent extends React.Component {
  ...
}

export const ExampleForm = formWrapper(ExampleFormComponent);

Step 2: Add the fields and initialize the form.

  ...
  constructor(props) {
    super(props);
    this.setFields();
  }

  setFields() {
    this.props.form.initForm({
      name: {
        defaultValue: "asdasd",
        validators: [minStringValidator, maxStringValidator, isRequired]
      },
      age: {
        defaultValue: "2017-06-01",
        validators: [isRequired]
      },
      email: {
        defaultValue: "[email protected]",
        validators: [emailValidator, isRequired]
      },
      repeatEmail: {
        validators: [equalValidatorEmail, isRequired]
      },
      policyPrivacy: {
        defaultValue: true,
        validators: [isRequired]
      }
    },
    true
    );
  }
  ...

initForm second param optional: true for validation fields in real time || [default] false only in submit state

Step 3: Do not forget, add to each of the fields of your html form your handler (props.form.getInput, props.form.getSelect, ...)

  ...
  render() {
    const { form } = this.props;
    return (
      <div>
        <Form form={form}>
          Name:
          <input type="text" {...form.getInput("name")} />
        
          Age:
          <input type="date" {...form.getInput("age")} />
          
          Email:
          <input type="text" {...form.getInput("email")} />
          
          Repeat email:
          <input type="text" {...form.getInput("repeatEmail")} />
          
          Condiciones de privacidad:
          <input type="checkbox" {...form.getCheckbox("policyPrivacy")} />
          
          <button>Submit</button>
        </Form>
      </div>
    );
  }
  ...

Step 4: Wrapper form html

    ...
    <Form form={form}>
    ...

Step 5: Finally, you will receive the result of the form by props, "componentDidUpdate"

    ...
    componentDidUpdate() {
        if (this.props.form.isValidAfterSubmit) {
        console.log("Send data:", this.props.form.values);
        }
    }
    ...

Optional: you can get the errors of each of the fields

    ...
    render() {
        ...
        <Form form={form}>
          Name:
          <input type="text" {...form.getInput("name")} />
          {form.getErrors("name").map(e => (
            <span key={e} style={{ color: "red" }}>
              {e}
            </span>
          ))}

          Age:
          <input type="date" {...form.getInput("age")} />
          {form.getErrors("age").map(e => (
            <span key={e} style={{ color: "red" }}>
              {e}
            </span>
          ))}
    ...
  • You can also get all the errors, with props props.form.errors.

Optional: control submit

    ...
    handleSubmit = e => {
      e.preventDefault();
      (...)
      this.props.form.submit();
    };

    render() {
        ...
        <Form form={form}>
          Name:
          <input type="text" {...form.getInput("name")} />
          {form.getErrors("name").map(e => (
            <span key={e} style={{ color: "red" }}>
              {e}
            </span>
          ))}

          <button onClick={this.handleSubmit}>
            Submit
          </button>
    ...

Doc

Functions

| function | params | description | |--|--|--| | initForm() | { exampleFieldName: { defaultValue: "foo", validators: [Validator1, Validator2, ValidatorN ] } }, boolean | Add the init form fields, along with their default value and validations. The second parameter indicates validations in real time, by default false. Method used in the contructor() | | setFields() | { exampleFieldName: { defaultValue: "foo", validators: [Validator1, Validator2, ValidatorN ] } } | Add the form fields, along with their default value and validations. | | setValues() | { nameField1: "foo", nameField2: "var", nameFieldN: "test" } | Set values. The form must have the loading to false. | | clear() | no params | Set default values ​​for errors, values ​​and isValidAfterSubmit. | getErrors() | nameField | Get the errors of a field. Returns an error array or an empty one. | getInput() | nameField | Get input attributes. Only text, number and date. | getSelect() | nameField | Get input attributes. Only for select. | getCheckbox() | nameField | Get input attributes. Only for simple checkbox: true/false. | getRadio() | "nameField" , "value" | Get input attributes. Only for radio type. | getCheckboxMulti() | "nameField" , "value" | Get input attributes. Only for check with multiple options.

Render props

| prop | types | default value | description | |--|--|--|--| | errors | { elementKey: String[], ... } | {} | Errors by fields. | | values | { element: String, ... } | {} | Values by fields. | | isValidAfterSubmit | boolean | false| All fields comply with their validations. After submit(). | | isValid | boolean | false| All fields comply with their validations in real time. Example: <button style={{ backgroundColor: form.isValid ? "green" : "red" }}>Submit</button> | | init | boolean | true | false, when the form is ready. After initForm().|

Component Form

Although the philosophy is not to create components, for the best control of the form we had to create a Form component to guarantee the load cycles and the alerts of uncontrolled components when they change to controlled:

    ...
    <Form form={this.props.form}>
    ...

Validations

Example

The validation "isRequired" is included in the library.

Other examples of validation:

import { Validator } from "@jkr26/react-forms-builder-logic";
export const startWithJJ = new Validator(value => {
  const error = "The element must start with jj.";
  if (value && /^jj/.test(value)) {
    return false;
  }
  return error;
});

export const maxStringValidator = new Validator(value => {
  const error = "The item must have less than 10 characters.";
  if (value && value.length < 10) {
    return false;
  }
  return error;
});

export const emailValidator = new Validator(value => {
  const error = "Invalid email";
  const emailPattern = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  if (value && emailPattern.test(String(value).toLowerCase())) {
    return false;
  }
  return error;
});

export const equalValidatorEmail = new Validator((value, formFields) => {
  const error = "THE EMAIL DOES NOT MATCH.";
  if (value && value === formFields.email.value) {
    return false;
  }
  return error;
});

The validators work in a very simple way, as the first parameter they receive the value of the element and as a second parameter they receive all the values ​​of the form, as an object: formFields = { name: "John", age: "33", email: "[email protected]", repeatEmail: "john" }


MIT License.