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

react-contextual-forms

v0.6.0

Published

Simple forms with React

Downloads

5

Readme

React Contextual Forms

version

Simple forms for React using context.

Features

  • Easy to use
  • Bring your own components
  • Write your own validators
  • Made with TypeScript (available types)

Installation

This package uses hooks so it requires at least version 16.8 of React.

npm install react-contextual-forms

Usage

After installing the package from NPM you can start using the 3 available components (Form, Field and Interaction) to build your forms.

Components

Form

The 'Form' component is the one that will provide the context for the others, you can place 'Field' and 'Interaction' components within it. There is no restriction on the number of forms you can use next to each other, how many you use or where you place the fields/interactions in the form.

| Prop | Type | Required | Description | | -------- | -------- | -------- | ----------------------------------------- | | onChange | function | no | Event that triggers when the form changes | | onSubmit | function | yes | Submit event for the form |

Field

A 'Field' component only serves as a wrapper for the actual component and provides it with a number of props, how you want to display the field is completely up to you.

| Prop | Type | Required | Description | | ------------- | ----------------------------------------- | -------- | --------------------------------------------------------- | | component | FunctionComponent< IFieldComponentProps > | yes | The component to render as the field | | defaultValue | string | no | The default value of the field | | id | string | yes | ID of this field | | onChange | function | no | Returns the value of the field when it changes | | props | object | no | Props of the field which are passed down to the component | | validators | Array< IFieldValidator > | no | Collection of all validator functions |

IFieldComponentProps

| Name | Type | Description | | --------- | ------------- | ------------------------------------------------------------------------- | | error | string / null | The error message for the field if there is a validation error, else null | | isTouched | boolean | Has the field been touched by the user or not | | props | object | Passed down props from the field | | value | string | The current value of the field | | update | function | [(value: string) => void] Function to update the field value |

IFieldValidator

| Name | Type | Description | | ------- | -------- | -------------------------------------------------------------------------------- | | check | function | [(value: string) => boolean] Function to validate the field, must return boolean | | message | string | The error message to show if the validation fails |

Interaction

If you find the need to update a form value directly, you can use an 'Interaction' component. Note that these only work when they are located within a Form component.

| Prop | Type | Required | Description | | --------- | ----------------------------------------------- | -------- | --------------------------------------------------------------- | | component | FunctionComponent< IInteractionComponentProps > | yes | The component to render as the interaction | | props | object | no | Props of the interaction which are passed down to the component |

IInteractionComponentProps

| Name | Type | Description | | ------ | ---------------- | ---------------------------------------------------------------------- | | props | object | Passed down props from the intercaction | | update | function | [(id: string, value: string) => void] Function to update field's value |

Examples

Javascript

import React from 'react'
import { Form, Field, Interaction } from 'react-contextual-forms'

const formValidators = {
    required: {
        check: (value) => {  
            if (!value) return false
            return true
        },
        message: 'This field is required'
    }
}

const Input = ({ error, isTouched, value, update }) => {
    return (
        <span>
            <input type="text" value={value} onChange={(e) => update(e.target.value)} />
            <p>{(error && isTouched) && error}</p>
        </span>
    )
}

const Select = ({ error, isTouched, value, update }) => {
    return (
        <span>
            <select value={value} onChange={(e) => update(e.target.value)}>
                <option>A</option>
                <option>B</option>
                <option>C</option>
            </select>
            <p>{(error && isTouched) && error}</p>
        </span>
    )
}

const ChangeButton = ({ update }) => {
    return (
        <button 
            type="button" 
            onClick={() => { 
                update('lastname', 'Doe')
                update('firstname', 'John')
            }}
        >Change</button>
    )
}

function App() {
    return (
        <div className="App">
            <p>FORM</p>
            <Form onSubmit={(form) => console.log(form)}>
                <div>
                    <Field id="lastname" validators={[formValidators.required]} component={Input} />
                    <Field id="firstname" validators={[formValidators.required]} component={Input} />
                </div>
                <div>
                    <Field id="choice" defaultValue="B" component={Select} />
                </div>
                <Interaction component={ChangeButton} />
                <button type="submit">Submit</button>
            </Form>
        </div>
    )
}

Typescript

import React from 'react'
import { Form, Field, Interaction, IFieldComponentProps } from 'react-contextual-forms'

const formValidators = {
    required: {
        check: (value) => {  
            if (!value) return false
            return true
        },
        message: 'This field is required'
    }
}

const Input: React.FunctionComponent<IFieldComponentProps> = ({ error, isTouched, props, value, update }) => {
    return (
        <span>
            <label>{props?.label}</label>
            <input type="text" value={value} onChange={(e) => update(e.target.value)} />
            <p>{(error && isTouched) && error}</p>
        </span>
    )
}

function App() {
    return (
        <div className="App">
            <p>FORM</p>
            <Form onChange={(form) => console.log('CHANGE', form)} onSubmit={(form) => console.log('SUBMIT', form)}>
                <div>
                    <Field id="lastname" validators={[formValidators.required]} component={Input} props={{ label: 'Last name' }} />
                    <Field id="firstname" validators={[formValidators.required]} component={Input} />
                </div>
                <button type="submit">Submit</button>
            </Form>
        </div>
    )
}

License

MIT