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

v4.1.3

Published

Manage React Forms with ease.

Downloads

35

Readme

📜 About

react-component-form is a lightweight form component for React.js, it allows you to get the inputs values without state thanks to onChange or onSubmit props.

There is also a React Hooks to be used in combination with the <Form /> component to validate the data with Ajv JSON schema validator, see advanced usage.

Example demo: https://react-component-form.vercel.app/.

💾 Install

npm install --save react-component-form

⚙️ Usage

Note: The examples use TypeScript, but obviously you can use JavaScript. Be aware that HandleForm is the type definition for the onChange and onSubmit props.

import React from "react"
import { Form } from "react-component-form"
import type { HandleForm } from "react-component-form"

export const Example = () => {
  const handleSubmit: HandleForm = (formData, formElement) => {
    console.log(formData) // { inputName: 'value of the input' }
    formElement.reset()
  }

  return (
    <Form onSubmit={handleSubmit}>
      <input type="text" name="inputName" />
      <button type="submit">Submit</button>
    </Form>
  )
}

Basically you have access to the same props of the HTML form tag in React, but the onSubmit and the onChange props are differents.

Instead to get the event param you get formData and formElement parameters:

  • formData: Object where the keys are the name of your inputs and the current value. Behind the scene, it uses the FormData constructor.
  • formElement: The HTML form element in the DOM so for example you can access the .reset() method on a HTMLFormElement.

⚙️ Advanced Usage

This example shows how to use the <Form /> component with useForm hook to validate the data with Ajv JSON schema validator.

You can see a more detailled example in the ./example folder.

import React from "react"
import { Form, useForm } from "react-component-form"
import type { HandleUseFormCallback } from "react-component-form"

const schema = {
  inputName: {
    type: "string",
    minLength: 3,
    maxLength: 20,
  },
}

export const Example = () => {
  const { handleUseForm, errors, message } = useForm(schema)

  const onSubmit: HandleUseFormCallback<typeof schema> = (
    formData,
    formElement,
  ) => {
    console.log(formData) // { inputName: 'value of the input validated and type-safe' }
    formElement.reset()

    // The return can be either `null` or an object with a global message of type `'error' | 'success'`.
    return {
      type: "success",
      message: "Success: Form submitted",
    }
  }

  return (
    <Form onSubmit={handleUseForm(onSubmit)}>
      <input type="text" name="inputName" />
      {errors.inputName != null && <p>{errors.inputName[0].message}</p>}

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

      {message != null && <p>{message}</p>}
    </Form>
  )
}

API

useForm(schema)

Parameters

Returns

  • handleUseForm(onSubmit): Function to be used with the onSubmit or onChange prop of the <Form /> component.
  • fetchState = 'idle': The current state of the form ('error' | 'success' | 'idle' | 'loading').
  • setFetchState: Function to update the fetchState.
  • message: Global message of the form (not specific to a property).
  • setMessage: Function to update the message.
  • errors: Object of errors:
    • Key: correspond to a property in the JSON Schema.
    • Value: array of ajv ErrorObject. The array will always have at least one element (never empty) in case of errors. If the value is undefined, it means there are no errors for this property.

💡 Contributing

Anyone can help to improve the project, submit a Feature Request, a bug report or even correct a simple spelling mistake.

The steps to contribute can be found in CONTRIBUTING.md.

📄 License

MIT