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-context-perf-form

v1.4.2

Published

Basic react form builder that is optimised to be blazing fast

Downloads

3

Readme

react-context-perf-form

A minimal form library for React built with React Context and validation using Yup that provides better performance.

Available props

FormProvider

| Name | Type | Default | Description | | - | - | - | - | | validateOnMount | boolean | false | Determine if the form should run validation on mount | | validateOnChange | boolean | false | Determine if a field should be validated on change in value | | initialTouched | boolean | false | Determine if fields will be touched initially | | initialValues | object | {} | Initial values for the form | | onSubmit | function | | (values, form: FormContextType) => {} Callback function to be called when the form is submitted | | validations | object | {} | Yup Object containing the validation schema for the form | | onFormValueChange | function | | ({ fieldName: string; prevValue: any; value: any; form: FormContextType }) => {} Callback function to be called when a field value changes | enableReinitialize | boolean | | Determines if reinitialisation is required on initialValue change

FastField

| Name | Type | Description | | - | - | - | | name | string | Name of the field | | component | JSX.Element | Component to render for a field |

NOTE: Pass component specific props with spread operator

FormContextType

type FormContextType = {
  values: Record<string, any>;
  errors: Partial<Record<string, any>>;
  touched: Partial<Record<string, any>>;
  handleChange: (key: string) => (value: any) => void;
  handleTouched: (key: string) => (value: any) => void;
  handleError: (key: string) => (value: any) => void;
  setValues: (args: Partial<Record<string, any>>) => void;
  resetValues: (args: Partial<Record<string, any>>) => void;
  setErrors: (args: Partial<Record<string, any>>) => void;
  setTouched: (args: Partial<Record<string, any>>) => void;
  validateForm: (
    values: any,
    submit: (values: any, form: any) => void,
  ) => Promise<void>;
}

Using the form

Use case 1

For simple forms with single input fields such as text, select, radio, checkbox or any custom component with one input field, we can make use of FastField.

const submitDetails = (values, form) => { ... };

const onFormValueChange = ({
  fieldName,
  prevValue,
  value: currentValue,
  form,
}) => {
  case 'firstName':
    // Do something;
    break;
  case 'lastName':
    // Do something;
    break;
}

<FormProvider
  validateOnMount
  validateOnChange
  initialValues={{}}
  onSubmit={submitDetails}
  validations={validationSchema} // Yup validation schema
  onFormValueChange={onFormValueChange}
>
  {(form: FormContextType) => (
    <>
      <FastField
        name="firstName"
        component={SomeComponent} 
        {...props} // SomeComponent's props 
      />
      <FastField
        name="lastName"
        component={SomeComponent} 
        {...props} // SomeComponent's props 
      />
    </>
  )}
</FormProvider>

NOTE: FastField works only inside FormProvider.

Use case 2

For more sophisticated form fields, we might want to keep the logic for the field separate, in such cases we can have the following approach:

const ComplexField = () => {
  return (
    <View>
      ...
      <FastField ... />
      ...
    </View>
  );
};
const submitDetails = (values, form) => { ... };

const handleFormValueChange = ({
  fieldName,
  prevValue,
  value: currentValue,
  form,
}) => {
  case 'firstName':
    // Do something;
    break;
  case 'lastName':
    // Do something;
    break;
}

<FormProvider
  validateOnMount
  validateOnChange
  initialValues={{}}
  onSubmit={submitDetails}
  validations={validationSchema} // Yup validation schema
  handleFormValueChange={handleFormValueChange}
>
  {(form: FormContextType) => (
    <>
      <ComplexField />
    </>
  )}
</FormProvider>

Other use case

useField, useFormContext are also available for use cases that are not covered above and that require more complex business logic and implementation.

useField

const [field, meta, helpers] = useField({ name });
  • field contains one property -

    • value - field’s value
  • meta contains two properties -

    • error - field’s error message

    • touched - boolean

  • helpers contains three properties -

    • setValue - used to set value of field(s)

    • setTouched - used to set touched status of field(s)

    • setError - used to set error on field(s)

useFormContext

This can be used to get all properties defined in FormContextType above.