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

@savedo/re-form

v2.0.0

Published

react form builder library

Downloads

18

Readme

re-form

React form library.

re-form

Installation

re-form requires

  • react/react-dom 16.8.3 or later to build your form.

$ npm install --save @savedo/re-form

or

$ yarn add @savedo/re-form

Usage

Basic usage: Creating a react form component with three input fields (name, age, email) validation and values assignment.

Create your form component:

import React from 'react';
import { FormBuilder } from '@savedo/re-form';

const MyForm = ({ handleSubmit }) => {
  const fields = [
    'name',
    'age',
    'email'
  ];

  const fieldOptions = {
    name: {
      label: 'Name:'
    },
    age: {
      label: 'Age:',
      type: 'number'
    },
    email: {
      label: 'E-mail:'
    }
  };

  const validate = (formData) => {
    const errors = {};
    const { name, age } = formData;

    if (!name) {
      errors.name = 'Name is required!';
    }
    if (!age) {
      errors.age = 'Age is required!';
    } else if (Number(age) < 18) {
      errors.age = 'You should be 18 years old or older!';
    }

    return errors;
  };

  return (
    <div>
      <FormBuilder { ...{ fields, fieldOptions, handleSubmit, validate } } />
    </div>
  );
};

export default MyForm;

And use form component elsewhere:

import React from 'react';
import MyForm from './MyForm';

const App = () => {
  const handleSubmit = (formData) => {
    console.log(formData)
  };

  return (
    <MyForm handleSubmit={ handleSubmit } />
  );
};

export default App;

Configuration

The FormBuilder component has props as shown below. It has FormBuilderPropsType<T> type. T refers to your object type having key (Field name, ) / values (Field options ).

FormBuilderPropsType

Property | Type | Description --- | --- | --- fields | string[] | Unique field names fieldOptions | { key (string): options (FieldOptionsValueType) } | Field/Component options (See table below) values | object | Init values for the fields handleSubmit | function | Callback function to handle submit behaviour if validation successful validate | method | Callback function for validation (form data will be passed as an argument), supports sync/async functions. submitSection | React.FC | React component to provide submit button or submit event

FieldOptionsValueType

Property | Type | Default | Optional | Description --- | --- | --- | --- | --- name | string | field key name | true | name of the field label | string | field key name | true | label for the form field element | input, select, textarea | input[type=text] | true | HTML tag for the form field type | input types (eg. text, number, email, checkbox etc) | text | true | type attribute for HTMLInputElement component | FunctionalComponent | N/A | true | Pass your FunctionalComponent with props (FormFieldPropsType). element and type becomes redundant when component is used. keyValues | { [key: string]: any } | N/A | true | Only viable when element is select. This object provides the list of <option value="key">value</option> className | string | N/A | true | CSS class(es) for the element defaultValue | string | N/A | true | Default value for a field. disabled | boolean | N/A | true | Disabled prop for inputs. placeholder | string | N/A | true | Placeholder text for the input. checked | boolean | N/A | true | Used for checkboxes to pass default value when type is checkbox. checkboxText | string | N/A | true | Used for pass label for chebox element, normal label is being used for input lable.

FormFieldPropsType

Props below will be passed to your custom component.

Property | Type | Description --- | --- | --- options | FieldOptionsValueType | Options defined for field (See table above) error | string | Error message to pass into component if the input is not valid setValue | Function | N/A. For internal usage value | any | N/A. For internal usage checked | boolean | Passed when type is checkbox in place of Value. name | string | N/A. For internal usage

Development

In order to start dev server, type

yarn develop

it opens your browser with url http://localhost:9100/

License

MIT