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

formaction

v1.1.0

Published

A library for react forms menaging

Downloads

8

Readme

Formaction

A library for react forms management

100% code coverage with Jest

NPM JavaScript Style Guide

Install

npm install --save formaction
yarn add formaction

Usage

Basically the form take a submit handler, then call the handler with form state, if all validations pass.

import React from 'react'

import { Form, Field, isEmail } from 'formaction';

class Example extends React.Component {
  handleSubmit(values) {
    console.log(values);
  }

  render () {
    return (
      <Form
        initialValues={{ 'Forms with fun': 'hola' }}
        onSubmit={values => this.handleSubmit(values)}
      >
        <Field
          name='name'
          type='text'
        />

        <Field
          name='labeledField'
          type='email'
          label='Email'
          validators={[isEmail]}
        />

        <button type='submit'>Click me</button>
      </Form>
    );
  }
}

Can I controll my form?

Yes, you can pass state and a onChange handler to form, and controll it by yourself. Knows what it means? Exactually, if we need it, we can store the form state in redux. It's still running validations before successfully submitting the form.

import React from 'react'

import { Form, Field, isEmail } from 'formaction';

class Example extends React.Component {
  constructor() {
    super();

    this.state = {
      form: {}
    }
  }

  handleSubmit(values) {
    console.log(values);
  }

  handleChange(form) {
    this.setState({
      form
    });
  }

  render () {
    return (
      <Form
        initialValues={{ 'name': 'Gabriel Oak' }}
        onSubmit={values => this.handleSubmit(values)}
        onChange={values => this.handleChange(values)}
        values={this.state.form}
      >
        <Field
          name='name'
          type='text'
        />

        <Field
          name='email'
          type='email'
          validators={[isEmail]}
        />

        <button type='submit'>Click me</button>
      </Form>
    );
  }
}

Creating a personalised component

Fild component may have a renderComponent property, it takes a component, that component will be rendered rather than native html input. Field will pass by props all the native input props, like value and onChange, and a meta object. Meta contains info about validators errors and if field has been touched.

import React from 'react'

import { Form, Field, isRequired } from 'formaction';

const renderSelect = props => {
  const { input, meta: { touched, errors } } = props;

  return (
    <div>
      <select
        {...input}
      >
        <option value='happiness'>Be happy</option>
        <option value='cool'>Be coll</option>
      </select>
      <div>
        {touched && errors[0]}
      </div>
    </div>
  );
}

class Example extends React.Component {

  handleSubmit(values) {
    console.log(values);
  }

  render () {
    return (
      <Form
        onSubmit={values => this.handleSubmit(values)}
      >
        <Field
          name='someCoolLabel'
          renderComponent={renderSelect}
          validators={[isRequired]}
        />

        <Field
          name='name'
          type='email'
          validators={[isEmail]}
        />

        <button type='submit'>Click me</button>
      </Form>
    );
  }
}

Validators:

Validators are just pure functions, they receive input value, and do something, and then return a string containing the error, or just undefined. You can build your own validators.

export const isEmail = (value: string) => (
  !value || !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value)
    ? 'This field must be of type email'
    : undefined
);

export const isNumber = (value: any) => (
  !value || !isNaN(+value)
    ? undefined
    : 'This field must be of type number'
);

export const isRequired = (value?: any) => (
  value || typeof value === 'number'
    ? undefined
    : 'This field is mandatory'
);

And yes, it works with MATERIAL-UI

import React from 'react';
import { Form, Field } from 'formaction';
import { TextField } from '@material-ui/core';


const renderTextField = props => {
  const {
    input,
    meta: { touched, error },
    ...rest
  } = props;

  return (
    <TextField
      {...input}
      {...rest}
      error={touched && error[0]}
      helperText={touched && error[0]}
      className="Field"
    />
  );
}

class Example extends React.Component {

  handleSubmit(values) {
    console.log(values);
  }

  render () {
    return (
      <Form
        onSubmit={values => this.handleSubmit(values)}
      >
        <Field
          name='user'
          label="Usuário"
          variant="outlined"
          className='poweeeeeeer'
          renderComponent={renderTextField}
        />

        <button type='submit'>Click me</button>
      </Form>
    );
  }
}

License

MIT © gabriel-Oak