Coder Social home page Coder Social logo

byteclaw / forms Goto Github PK

View Code? Open in Web Editor NEW
8.0 4.0 0.0 2.39 MB

πŸ“‹ Easily built forms in React using Hooks API

Home Page: https://www.npmjs.com/package/@byteclaw/forms

License: MIT License

JavaScript 0.23% TypeScript 99.77%
react forms validation library hooks-api-react hooks yup-compatibility

forms's Introduction

@byteclaw/forms

CircleCI All Contributors Version License

Easily create complex forms in React.

Installation

npm install @byteclaw/forms

#Β or using yarn

yarn add @byteclaw/forms

Yup compatibility

This library supports Yup validation library. In order to use it please install Yup because it isn't a part of this library.

Requirements

  • react >= 16.8.3

Up and running example

import { Fragment } from 'react';
import {
  ArrayField,
  Field,
  Form,
  FormProvider,
  ObjectField,
  createValidatorFromYup,
} from '@byteclaw/forms';
import * as yup from 'yup';

const validator = createValidatorFromYup(
  yup.object().shape({
    productTitle: yup.string().required(),
    images: yup
      .array()
      .of(
        yup.object().shape({
          title: yup.string().required(),
          url: yup
            .string()
            .url()
            .required(),
        }),
      )
      .required(),
    attributes: yup.object().shape({
      color: yup
        .string()
        .matches(/^#[0-9a-fA-F]{6}$/)
        .required(),
    }),
  }),
);

<Form onSubmit={async values => {}} onValidate={validator}>
  {/* global error of form */}
  <FieldError>{({ error }) => error || null}</FieldError>
  <Field name="productTitle" />
  <FieldError name="productTitle">{({ error }) => error || null}</FieldError>
  <ArrayField name="images">
    {({ value }, dispatch) => (
      <Fragment>
        {/* value can be undefined/null if is not initialized */}
        {(value || []).map((val, i) => (
          <ObjectField name={i}>
            <Field name="url" type="url" />
            <FieldError name="url">{({ error }) => error || null}</FieldError>
            <Field name="title" />
            <FieldError name="title">{({ error }) => error || null}</FieldError>
            <button onClick={() => removeItem(i)} type="button">
              Remove
            </button>
          </ObjectField>
        ))}
        <button onClick={() => dispatch({ type: 'CHANGE', value: [...value, {}] })} type="button">
          Add image
        </button>
      </Fragment>
    )}
  </ArrayField>
  <FieldError name="images">{({ error }) => error || null}</FieldError>
  <ObjectField name="attributes">
    <Field name="color" />
    <FieldError name="color">{({ error }) => error || null}</FieldError>
  </ObjectField>
  <FieldError name="attributes">{({ error }) => error || null}</FieldError>
  <FormProvider>
    {form => (
      <button disabled={form.status !== 'IDLE'} type="submit">
        Save
      </button>
    )}
  </FormProvider>
</Form>;

API

Form component is a root component necessary to use Forms at all. It provides a context for all fields in a given form.

Form accepts onValidate, onSubmit and validateOnChange props along with standard attributes accepted by <form />.

  • onValidate<TValues>(values?: TValues): Promise<TValues | void>
    • optional validation function
    • in case of an error please throw ValidationError provided by this library
  • onSubmit<TValues>(values: TValues): Promise<void>
    • optional submit function
    • it can validate form too, in case of an error throw ValidationError provided by this library
  • onChange<TValues>(values: TValues): Promise<void>
    • optional on change function
  • validateOnChange
    • default is false
    • optional
    • will validate form on change

FormProvider is a way to connect to FormState when you need to react on something.

  • children: (state: FormState) => any

Field is a base component to construct simple widgets using <input /> elements. On change is debounced.

FieldError is a base component if you want to render validation errors that are connected with a specific field or a form root.

To connect to a root of ObjectField/ArrayField/Form use it like this:

<FieldError name="">{err => JSON.stringify(err, null, ' ')}<FieldError>

Or specify name as a name that was used by Field component.

ArrayField is a base component to construct complex fields for array values.

ObjectField is a base component to construct nested objects in your form.

ValidationError is a way how to map errors to fields in your form.

Examples

Contributors


Michal KvasničÑk

πŸ’¬ πŸ’» 🎨 πŸ“– πŸ’‘ πŸ€” πŸ‘€ ⚠️

Juraj HrΓ­b

πŸ’¬ πŸ› πŸ’» πŸ“– πŸ€” πŸ‘€

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT License

forms's People

Contributors

dependabot[bot] avatar michalkvasnicak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

forms's Issues

Fix initialValue deletion in ArrayField

When there is an initial value for an array field and I remove it using removeItem method, then when I add new item using addItem method, I don't want to see previous value from that index.

Investigate using more complex reducer with nested fields

Basically store the whole form values state and use form reducer to dispatch actions over all fields, etc. Then just reflect the state of a field in local field state so we can easily read a field state without need to hack the state.

Investigate removing fields from ArrayField using REMOVE_FIELD

When I add a value to array field and then I invoke REMOVE_FIELD it still stays there. Seems like some sort of race condition where the value disappears from value array but is again added because subsequent Field component with given index as name re-adds itself automatically on mount.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.