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

zod-react-form

v1.0.31

Published

To handle form validation in a React application using `zod`, follow these steps:

Downloads

5

Readme

zod-react-form

To handle form validation in a React application using zod, follow these steps:

  1. Define a zod schema for your form data, specifying the expected shape and types of the fields.

  2. Set an initial state for the form fields that matches the structure of the zod schema.

  3. Use the useZodFormValidation hook from the zod-react-form package to handle form validation. Pass in the FormSchema and initialFormState values, and the hook will manage the form state, validation errors, and other form-related functionality.

  4. In your form components, use the values and setFieldValue props provided by the hook to manage the state of your form fields. Make sure to use the correct value prop when rendering form fields, such as values.name instead of a component-specific state variable.

import { useZodFormValidation } from "zod-react-form"

const FormSchema = z.object({
  name: z.string().min(1),
  email: z.string().email(),
})

const initialFormState = {
  name: "",
  email: "",
}

// Infer the types from the schema:
type FormSchemaType = z.infer<typeof FormSchema>

// Pass the interface, schema and initial form the the hook.
const { values, errors, setFieldValue, onBlur, touched, reset } =
  useZodFormValidation <FormSchemaType> (FormSchema, initialFormState)

// TextField example
<TextField
  name="name"
  value={values.name}
  onChange={(e) => setFieldValue("name", e.target.value)}
  autoFocus
  margin="dense"
  id="name"
  label="Name"
  type="text"
  fullWidth
  onBlur={() => onBlur("name")}
  helperText={(touched.name && errors.name) || " "}
  error={Boolean(touched.name && errors.name)}
/>


API Description

values: an object containing the current form values.

errors: an object containing any validation errors that have occurred in the form.

setFieldValue: a function that takes a key and value as inputs, and updates the form data with the new value.

onBlur: a function that takes a key as input and sets the corresponding field as "touched".

touched: an object containing a boolean value for each field, indicating whether it has been "touched" or not.

reset: a function that resets the form to its initial state.

isDisabled: a function that takes an optional validator function as input, and returns a boolean indicating whether the form should be disabled or not. The validator function can be used to check additional conditions before disabling the form.