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

solid-js-createform

v1.0.1

Published

createform provides a way to create complex forms easily for SolidJS.

Downloads

7

Readme

Welcome to createform 👋

A SolidJS package to create forms easily and quickly.

createform

Createform is an open-source package to create forms for SolidJS applications. It's based on useForm

Motivation

I decided to create a package that could simplify the creation of forms, and, make it easier to use. After a quick research, I found that there are not many packages to create forms for SolidJs, so I decided to create one, that could be used by anyone.

The main idea is to create a form easily and quickly, without many lines of code, fortunately, SolidJs provides us a powerful and easy way to do that.

  • Creates form with fields
  • Creates form with validation
  • Supports custom fields
  • Update touched state of form fields independently or all together
  • Update values state of form fields independently or all together
  • Update errors state of form fields independently or all together

How to use it

To use createform you need to import it into your SolidJS application.

import { createForm } from 'solid-js-createform'

Then you can create a form.

const form = createForm({
  initialValues: {
    name: '',
    email: '',
    password: ''
  }
})

Now you can use it everywhere in your SolidJS application.

function App() {
  const { register, handleSubmit, errors } = form

  function onSubmit(data) {
    console.log(data)
  }

  return (
    <div>
      <form onSubmit={handleSubmit(onSubmit)}>
        <input {...register('name', 'text')} />
        <input {...register('email', 'email')} />
        <input {...register('password', 'password')} />
        <button type="submit">Submit</button>
      </form>
    </div>
  )
}

Validation

CreateForm uses yup validation schema, so you just need to pass a validation schema to the createForm function.

const form = createForm({
  initialValues: {
    name: '',
    email: '',
    password: ''
  },
  validationSchema: yup.object({
    name: yup.string().required('Name is required'),
    email: yup.string().email('Invalid email').required('Email is required'),
    password: yup
      .string()
      .min(6, 'Password must be at least 6 characters')
      .required('Password is required')
  })
})

API

createForm receives an object with the following properties:

  • initialValues: An object with the initial values of the form.
  • validationSchema: A validation schema to validate the form. By default, createForm uses yup validation schema.

createForm returns an object with the following properties:

  • register: Register a field to the form.

    const form = createForm({
      initialValues: {
        name: '',
        email: '',
        password: ''
      }
    })
    
    const { register, handleSubmit, errors } = form
    
    <input {...register('name', 'text')} />
  • handleSubmit: Handle form submit.

    const form = createForm...
    
    const { register, handleSubmit, errors } = form
    
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register('name', 'text')} />
      <input {...register('email', 'email')} />
      <input {...register('password', 'password')} />
      <button type="submit">Submit</button>
    </form>
  • errors: Get errors of the form.

    const form = createForm...
    const { register, handleSubmit, errors } = form
    
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register('name', 'text')} />
      <span> {errors.name}</span>
    </form>
  • touched: Get touched state of the form.

    const form = createForm...
    const { register, handleSubmit, errors, touched } = form
    
    <form>
      <input {...register('name', 'text')} />
      <span> {touched.name ? errors.name : ''}</span>
    </form>
  • values: Get values of the form.

    const form = createForm...
    const { register, handleSubmit, errors, values } = form
    
    createEffect(() => {
      console.log(values.name)
    })
    
    <form>
      <input {...register('name', 'text')} />
    </form>
  • setTouched: Set the touched state of the form.

    const form = createForm...
    const { register, handleSubmit, errors, setTouched } = form
    
    <form>
      <input {...register('name', 'text')} />
      <button onClick={() => setTouched('name')}>Touch</button>
    </form>
  • setValues: Set the values of the form.

    const form = createForm...
    const { register, handleSubmit, errors, setValues } = form
    
    <form>
      <input {...register('name', 'text')} />
      <button onClick={() => setValues({ name: 'John' })}>Set values</button>
    </form>

    or

    const form = createForm...
    const { register, handleSubmit, errors, setValues } = form
    
    <form>
      <input {...register('name', 'text')} />
      <button onClick={() => setValues('name','John')}>Set values</button>
    </form>
  • setErrors: Set errors of the form.

    const form = createForm...
    const { register, handleSubmit, errors, setErrors } = form
    
    <form>
      <input {...register('name', 'text')} />
      <button onClick={() => setErrors('name', 'Name is required')}>Set errors</button>
    </form>

    or

    const form = createForm...
    const { register, handleSubmit, errors, setErrors } = form
    
    <form>
      <input {...register('name', 'text')} />
      <button onClick={() => setErrors({name:'Name is required'})}>Set errors</button>
    </form>
  • resetForm: Reset the form.

    const form = createForm...
    const { register, handleSubmit, errors, resetForm } = form
    
    <form>
      <input {...register('name', 'text')} />
      <button onClick={() => resetForm()}>Reset form</button>
    </form>
  • resetValues: Reset values of the form.

    const form = createForm...
    const { register, handleSubmit, errors, resetValues } = form
    
    <form>
      <input {...register('name', 'text')} />
      <button onClick={() => resetValues()}>Reset values</button>
    </form>
  • resetErrors: Reset errors of the form.

    const form = createForm...
    const { register, handleSubmit, errors, resetErrors } = form
    
    <form>
      <input {...register('name', 'text')} />
      <button onClick={() => resetErrors()}>Reset errors</button>
    </form>
  • resetTouched: Reset the touched state of the form.

    const form = createForm...
    const { register, handleSubmit, errors, resetTouched } = form
    
    <form>
      <input {...register('name', 'text')} />
      <button onClick={() => resetTouched()}>Reset touched</button>
    </form>

Let us know what you think

Feel free to open an issue if you have any feedback, or if you want to contribute.

Show your support

Give us a star on GitHub if you like this project.