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

@ofqwx/form

v1.6.1

Published

A simple library to create generic React controlled forms.

Downloads

4

Readme

Wooga Form

A simple library to create generic React controlled forms.

Table of Contents

Getting started.

With yarn:

yarn add @ofqwx/form

With npm:

npm install @ofqwx/form

Playground

Here you can check out a real implementation. If you find any bugs, feel free to report them :).

API

Form

<Form /> component wraps your form and handles the state of it.

Props

onSubmit (required): A function to handle the values when a form is submitted. This function will receive the values from the form in the shape of an object where the keys will be the name of each field and the event of the submit if you need to use it.

initialValues (optional): An object containing initial values if you want to show the form pre-filled. Keys in this object need to be the same as the names of the fields.

Field

<Field /> component will return a children as a function with a field state that you can use in your input component.

Props

name (required): A string that defines the name of the input. This prop is what connects your input with the form state. If you're using initialValues, the name of the input and its initialValues key must the same. Otherwise, this value will not be controlled by the Form component.

type (optional): A string that defines the type of the input element.
The default value is "text".

options (optional): an object that can contain:

children (required): A function that will receive field state as argument.

Field State

type TField = {
  input: {
    name: string;
    label: string | null;
    onChange: (e: SyntheticEvent) => void;
    value: any;
    error: string;
  };
};

I.E

<Form onSubmit={() => undefined}>
  <Field>
    {(fieldProps: TField) => (
      <input {...fieldProps.input}>
    )}
  </Field>
</Form>

Validations

To use the validator functions just import them and send them to options props of the <Field /> component with validations as a key name. Since the validations argument is a list, you can send more that one validation see Multiple Validations Example. The validations will be executed from top to bottom.

Required validation

Required validation requires only one argument: the message to display when the validation fails.

validations.string.required(validationMessage)

import {Form, Field, validations} from '@ofqwx/form`


function MyForm() {
  return
    <Form onSubmit={() => undefined}>
    <Field></Field>
      <Field
        name="firstName"
        options={{
          validations: [validations.string.required('This field is required')],
        }}
      >
        {({ input }) => (
          <div>
            <input {...input} />

            {input.error ? <div>{input.error}</div> : null}
          </div>
        )}
      </Field>

      <button type="submit">Submit</button>
    </Form>
}

Regex validation

Regex validation requires two arguments: the message to display when the validation fails and a regex expression.

validations.string.required(validationMessage, regexExpression)

import {Form, Field, validations} from '@ofqwx/form`

const regex = /^(?:wooga\.name).[\w|\W]{1,}/;

function MyForm() {
  return
    <Form onSubmit={() => undefined}>
      <Field
        name="firstName"
        options={{
          validations: [
            validations.string.regex(
              'First name must start with "wooga.name"',
              regex
            ),
          ],
        }}
      >
        {({ input }) => (
          <div>
            <input {...input} />

            {input.error ? <div>{input.error}</div> : null}
          </div>
        )}
      </Field>

      <button type="submit">Submit</button>
    </Form>
}

Multiple Validations Example

import {Form, Field, validations} from '@ofqwx/form`

const regex = /^(?:wooga\.name).[\w|\W]{1,}/;

function MyForm() {
  return
    <Form onSubmit={() => undefined}>
      <Field
        name="firstName"
        options={{
          validations: [
            validations.string.required('First name is required'),
            validations.string.regex(
              'First name must start with "wooga.name"',
              regex
            ),
          ],
        }}
      >
        {({ input }) => (
          <div>
            <input {...input} />

            {input.error ? <div>{input.error}</div> : null}
          </div>
        )}
      </Field>

      <button type="submit">Submit</button>
    </Form>
}

Custom Validations

If you want to create your validation function, create a function that receives the message you want to show when validation fails and returns a function that receives the value to evaluate and throws the message if the validation fails.

function numberValidation(validationMessage) {
  return (valueToEvaluate) {
    if (typeof valueToEvaluate !== 'number') {
      throw validationMessage
    }
  }
}

<Form onSubmit={() => undefined}>
  <Field
    name="firstName"
    options={{
      validations: [
        numberValidation('First name is required'),
      ],
    }}
  >
    {({ input }) => (
      <div>
        <input {...input} />

        {input.error ? <div>{input.error}</div> : null}
      </div>
    )}
  </Field>

  <button type="submit">Submit</button>
</Form>