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-hook-form-yup

v1.0.0-alpha.2

Published

Enhances the integration of yup schemas into react-hook-form

Downloads

136

Readme

react-hook-form-yup

Enhances the integration of yup schemas into react-hook-form.

Description

This library provides the yup schema state for a field when using the Controller component or useController hook, so that validation rules can be displayed in the UI.

Installation

To install the library:

npm install react-hook-form-yup

Example

Schema definition

import * as yup from 'yup'

const schema = yup.object().shape({
  minSize: yup.number().moreThan(1).lessThan(yup.ref('maxSize')).required(),
  maxSize: yup.number().moreThan(yup.ref('minSize')).required(),
})

Form setup

import React from 'react'
import { useForm, FormProvider, useController, NumberSchemaState } from 'react-hook-form-yup'
import { schema } from './schema'

const NumberInput = (props: { name: string, type: string }) => {
  const { field, fieldState: { error }, schemaState } = useController<any, any, NumberSchemaState>(props.name)
  const { required, min, max, lessThan, moreThan } = schemaState
  const minMsg = min ? `Min ${min}` : moreThan ? `More than ${moreThan}` : ''
  const maxMsg = max ? `Max ${max}` : lessThan ? `Less than ${lessThan}` : ''
  const placeholder = [minMsg, maxMsg].filter(Boolean).join(' and ')

  return (
    <>
      <input
        {...field}
        style={{ display: 'block', width: 250 }}
        required={required}
        placeholder={placeholder}
      />
      {error && (<p style={{ color: 'red' }}>{error.message}</p>)}
    </p>
  )
}

const MyForm = () => {
  const props = useForm({ schema })

  return (
    <FormProvider {...props}>
      <NumberInput name="minSize" type="number" />
      <NumberInput name="maxSize" type="number" />
    </FormProvider>
  )
}

export default MyForm

Controller component

The Controller component has been updated in the same way as the useController hook, with schemaState being included in the render function

Customization

This library will try to keep the schema state and form validation aligned with the given form values. By default, the schema state for all fields will be updated on blur, so any refs or conditional validations can be shown in the UI. Additionally, unless the current validation mode is onSubmit, any field that had been previously qualified for validation will be revalidated to align with the schema state (dirty fields for onChange, touched fields for onBlur, both for onTouched). These behaviors can be overridden though.

Schema sync mode

Set schemaSyncMode to change when the schema is synced with the form values. Can be onBlur, onTouched, onChange, all, or false to disable. Default is onBlur and is recommended for good performance

<FormProvider schemaSyncMode="onChange" {...props} />

Disable validate on schema sync

Set disableValidateOnSchemaSync to disable validation from occuring on schema sync.

<FormProvider disableValidateOnSchemaSync {...props} />