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-form-stateful

v0.0.3

Published

full featured, extensible form component for react using react hooks

Downloads

9

Readme

react-form-stateful

react-form-stateful a full featured, extensible form component for react using react hooks

NOTE: this project requires the use of an alpha version of react to use.

Getting Started

npm install --save react-form-stateful

Examples

Basic Usage

import { FC } from 'react';
import { StatefulForm, SFInput, SFSelect, SFTextArea } from 'react-form-stateful';
import * as yup from 'yup';

const ValidationSchemeForm: FC = () => {
  return (
    <StatefulForm
      validationSchema={yup.object().shape({
        email: yup
          .string()
          .required('Email required')
          .email('Invalid email address'),
        desc: yup.string().max(256, 'Please keep your description short!'),
        complaint: yup
          .string()
          .required('Complaint required')
          .max(10000, 'Max complaint size: 10,000 characters.'),
      })}
    >
      <div>Feedback form:</div>
      <label>
        Email:
        <SFInput name="email" />
      </label>
      <label>
        Short Description:
        <SFInput name="desc" />
      </label>
      <label>
        Reason for complaint:
        <SFSelect
          name="reason"
          defaultEntry={'Please select a reason'}
          values={['Bug', 'Typo', 'Feature Request', 'Other']}
        />
      </label>
      <label>
        Complaint:
        <SFTextArea name="complaint" />
      </label>
    </StatefulForm>
  );
};

Other examples

Other examples can be seen in the examples folder. ValidationScheme.tsx is a simple form and the other two are more complex.

Customizing Forms

Context is used to expose the state. This allows for helper hooks to be written. Several already exist, but more could easily be written. (Feel free to make a PR).

Components

One of the components from components:

type SFControlProps<T = string> = {
  name: string;
  className?: string;
  errorClassName?: string;
  initialValue?: T;
  defaultValue?: T;
};

export const SFInput: FC<SFControlProps> = props => {
  const { touch, value, setValue, error } = useSFControl(props.name, props.initialValue, props.defaultValue);
  return (
    <Fragment>
      <input className={props.className} onBlur={touch} value={value || ''} onChange={e => setValue(e.target.value)} />
      <div className={props.errorClassName}>{error}</div>
    </Fragment>
  );
};

Example Usage

import { FC, createElement } from 'react';
import { StatefulForm, SFInput } from 'react-form-stateful';

const Form: FC = () => {
  return (
    <StatefulForm>
      <SFInput name="item" />
    </StatefulForm>
  );
};

As you can see component that matches your application's look and feel, but basic components do exist for your convenience.

Extending

While the internal reducer is not exposed, the dispatch and actions are exposed, which allows for extension through side effects. An example of this can be seen in examples/pages/Pages.tsx.

NO_DEFAULT and ASYNC_VALIDATION

There are two special constants that help with extending the functionality of react-form-stateful.

NO_DEFAULT

NO_DEFAULT prevents resets from affecting this value. Useful for hidden from values that are used to control validation. This us used in the advanced example examples/pages/Pages:

const valueState = useSFValue<number[]>(
  '@@pages',
  [0],
  NO_DEFAULT, // Don't get reset
  value => (props.pages.length !== value.length ? 'more pages exist' : null)
);

ASYNC_VALIDATION

ASYNC_VALIDATION Is used to to defer the validation to some external process. This is useful when you want to defer the validation to a separate process. This could also be done with Promises, but there may be cases where ASYNC_VALIDATION is more convenient.

const { error, setValue, value, touch, touched } = useSFControl<string>(props.name, '', '', () => ASYNC_VALIDATION);

Here when a validation is triggered, the error state is set to { async:true }. The form is not submitable until this is resolved. One way to resolve this is to use the useSFError hook and set the error state for the component.

const [, setError] = useSFError(props.name);
setError('Invalid username');

Prior art