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

@4react/forms

v2020.1.3

Published

Ready-to-use form context and validation for React Applications.

Downloads

8

Readme

@4react / forms

Ready-to-use form context and validation for React Applications.

Usage

Import dependency

npm i @4react/forms

Compose fields

import { Form, FormField, FormData } from '@4react/forms'

const App = () => (
  <Form>
    <FormField name="username" accept={/^\w+$/}>
      {({ update, valid }) => <input ... />}
    </FormField>
    <FormField name="password">
      {({ update, valid }) => <input ... />}
    </FormField>
    <FormData>
      {({ values, valid }) => <button ... />}
    </FormData>
  </Form>
)

Live example

API

Form

Component that provides a context for a form.

import { Form } from '@4react/forms'

const App = () => (
  <Form>
    ...
  </Form>
)

| Name | Type | Default | Description | | --- | --- | --- | --- | | initialValues | object | - | optional - Initial values for the form fields. |

FormField

Component that registers a new field for the surrounding form.

import { FormField } from '@4react/forms'

...

<FormField name="email">
  {({ update, valid }) => (
    <input
      type="text"
      onChange={e => update(e.target.value)}
      style={{ borderColor: valid ? 'gray' : 'red' }}
    />
  )}
</FormField>

| Name | Type | Default | Description | | --- | --- | --- | --- | | name | string | - | Name of the field | | defaultValue | any | - | optional - Default value of the field. | | accept | Function, RegExp | - | optional - If provided, this function is used to validate the value of the field. For string fields (and compatible types fields) a RegExp can be use also. | | validateOn | "init", "update" | "update" | optional - Use to control which moments are take into consideration for validation. A list of values is also accepted. |

This components accept a child function, and provides to it an object with the following properties:

| Name | Type | Description | | --- | --- | --- | | update | Function | Required to update the value of the field. | | valid | boolean | Points if last validation is passed or none is executed yet. | value | any | The stored value of the field (for controlled inputs). |

FormData

Component that retrieves all values of the current form.

import { FormData } from '@4react/forms'

...
    
<FormData>
  {({ values, valid }) => (
    <button
      disabled={!valid}
      onClick={() => sendData(values)}
    >
      SEND DATA
    </button>
  )}
</FormData>

| Name | Type | Default | Description | | --- | --- | --- | --- | | validate | boolean | true | optional - Used for performance issues, setting this property to false will disable automatic validation. Manual validation can be triggered by calling the validate method passed to children. |

This components accept a child function, and provides to it the following properties:

| Name | Type | Description | | --- | --- | --- | | values | object | Object containing all values of the form. | | valid | boolean | Points if the values in all the fields are considered valid. Returns false if no validation is done yet. | validate | Function | For performance reasons it's possible to force validation only on demand. In these cases invoking this function will validate the entire form. |

Hooks

Alternatively to the use of the above components, it's possible to create custom fields and data consumers, using the following hooks:

useFormField

This hook will register a new field with the specified name, and returns the same set of properties of the FormField component (see FormField).

import { useFormField } form '@4react/forms'

const MyCustomField = ({ name }) => {
  const { update, valid } = useFormField(name)

  return <input ... />
}

| Name | Type | Default | Description | | --- | --- | --- | --- | | name | string | - | Name of the field. | | options | object | {} | optional - Object accepting all optional props of the FormField component (see FormField). |

useFormData

This hook retrieves collected values of the current form, and returns the same set of properties of the FormData component (see FormData).

import { useFormData } from '@4react/forms'

const MyCustomSubmit = () => {
  const { values, valid } = useFormData()

  return <button ... />
}

| Name | Type | Default | Description | | --- | --- | --- | --- | | options | object | {} | optional - Object accepting all optional props of the FormData component (see FormData). |