All Projects → eyedea-io → Smashing Form

eyedea-io / Smashing Form

MobX powered forms in React

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Smashing Form

react-final-form-listeners
A collection of components to listen to 🏁 React Final Form fields
Stars: ✭ 91 (+133.33%)
Mutual labels:  forms, form
Country Fns
🌏 Useful country data for forms and stuff.
Stars: ✭ 35 (-10.26%)
Mutual labels:  form, forms
react-hubspot
A collection of React hooks for interacting with Hubspot APIs
Stars: ✭ 20 (-48.72%)
Mutual labels:  forms, form
react-cool-form
😎 📋 React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+530.77%)
Mutual labels:  forms, form
Unform
Performance-focused API for React forms 🚀
Stars: ✭ 4,340 (+11028.21%)
Mutual labels:  form, forms
vue-use-form
✅ A Vue.js composition API function to validate forms
Stars: ✭ 97 (+148.72%)
Mutual labels:  forms, form
ContactEtc
Laraval package to instantly add a customisable contact form to your site.
Stars: ✭ 21 (-46.15%)
Mutual labels:  forms, form
SuluFormBundle
Form Bundle for handling Dynamic and Symfony Forms in https://sulu.io
Stars: ✭ 51 (+30.77%)
Mutual labels:  forms, form
React Hook Form
📋 React Hooks for form state management and validation (Web + React Native)
Stars: ✭ 24,831 (+63569.23%)
Mutual labels:  form, forms
Formik Persist
💾 Persist and rehydrate a Formik form to localStorage
Stars: ✭ 345 (+784.62%)
Mutual labels:  form, forms
form-data-json
A zero dependency, cross browser library to easily get or set/manipulate form input values as/from a json object.
Stars: ✭ 37 (-5.13%)
Mutual labels:  forms, form
Formik
Build forms in React, without the tears 😭
Stars: ✭ 29,047 (+74379.49%)
Mutual labels:  form, forms
Forms
Tracking our progress moving all city paper and pdf forms online.
Stars: ✭ 14 (-64.1%)
Mutual labels:  forms, form
Verticalstepperform
Vertical Stepper Form Library for Android. It follows Google Material Design guidelines.
Stars: ✭ 868 (+2125.64%)
Mutual labels:  form, forms
contact-officials
Form definitions powering Resistbot's electronic deliveries to elected officials in the United States.
Stars: ✭ 29 (-25.64%)
Mutual labels:  forms, form
form-js
View and visually edit JSON-based forms.
Stars: ✭ 125 (+220.51%)
Mutual labels:  forms, form
formurai
Lightweight and powerfull library for declarative form validation
Stars: ✭ 49 (+25.64%)
Mutual labels:  forms, form
react-search
This package will help you create a pretty good and beautiful search. And other related features
Stars: ✭ 17 (-56.41%)
Mutual labels:  forms, form
grav-plugin-form
Grav Form Plugin
Stars: ✭ 48 (+23.08%)
Mutual labels:  forms, form
Form2js
Javascript library for collecting form data
Stars: ✭ 630 (+1515.38%)
Mutual labels:  form, forms

Smashing Form

npm (scoped) npm bundle size (scoped)

  • ⚡ Fast input rerenders - doesn't rerender whole form
  • 🥓 Well cooked api
  • 👌 Form validation based on yup
  • It's lightweight

Examples

Install

npm install --save @smashing/form mobx mobx-react-lite

Usage

import * as React from 'react'
import {useForm} from '@smashing/form'

const CustomTextInput = props => <input type="text" {...props} />

export const MyForm = () => {
  // Use `useForm` hook. `initialValues` is the only required value.
  const {Form, Field, form} = useForm({
    initialValues: {
      email: '',
      password: '',
    },
    onSubmit: values => console.log(values),
  })

  // Wrap your inputs with `Form` returned by `useForm`
  return (
    <Form>
      {/* Each input should be wrapped in `Field` returned by `useForm` */}
      <Field component={CustomTextInput} name="email" />
      {/* "input" is default component used by field */}
      <Field name="password" />
      <button type="submit">Submit</button>
    </Form>
  )
}

Options

const {} = useForm({
  initialValues,
  validationSchema,
  onSubmit,
  validateOnBlur,
  validateOnChange,
  validateOnSubmit,
})

initialValues - required

Object containing initial values of form. Can contain nested state.

validationSchema - default: undefined

Read yup docs to learn more.

onSubmit - default: undefined

Form submit handler

validateOnBlur - default: false

Control if data should be validated on input blur

validateOnChange - default: false

Control if data should be validated on input change

validateOnSubmit - default: true

Control if data should be validated on form submit

Validation

You can use yup to validate form data.

import * as React from 'react'
import * as yup from 'yup'
import {useForm} from '@smashing/form'

const TextInput = props => <input type="text" {...props} />

const validationSchema = yup.object().shape({
  email: yup
    .string()
    .email('Email is not valid.')
    .max(64, 'Email is too long.')
    .required('This field is required.'),
  password: yup
    .string()
    .min(6, 'Password is too short.')
    .max(64, 'Password is too long.')
    .required('This field is required.'),
})

export const MyForm = () => {
  const {Form, Field, form} = useForm({
    initialValues: {
      email: '',
      password: '',
    },
    validationSchema,
    onSubmit: values => console.log(values),
  })

  return (
    <Form>
      <Field component={TextInput} name="email" />
      <Field component={TextInput} name="password" />
      <button type="submit">Submit</button>
    </Form>
  )
}

Accessing form state

You can access whole state and form actions using form property returned from useForm.

const {form} = useForm({})

It contains the following state:

{
  "isSubmitting": false,
  "isValidating": false,
  "isDirty": false,
  "isValid": true,
  "submitCount": 0,
  "validateOnChange": false,
  "validateOnBlur": false,
  "validateOnSubmit": true,
  "values": {},
  "initialValues": {},
  "errors": {},
  "touched": {}
}

Accessing form state in nested components

You can access form state using context. FormContext and useFormContext are exported from package.

import {useFormContext, FormContext} from '@smashing/form'

const NestedComponent = () => {
  const {form} = useFormContext()
  // OR const {form} = React.useContext(FormContext)
}

FormContext/useFormContext can be used only inside Form component returned from useForm.

Handling nested data and arrays

const MyForm = () => {
  const {Form, Field} = useForm({
    initialValues: {
      email: '',
      social: {
        twitter: '',
        facebook: '',
      },
      friends: [{name: 'John'}, {name: 'Jane'}],
    },
  })

  return (
    <Form>
      <Field component={TextInput} name="email" />
      <Field component={TextInput} name="social.twitter" />
      <Field component={TextInput} name="social.facebook" />
      <Field component={TextInput} name="friends.0.name" />
      <Field component={TextInput} name="friends.1.name" />
    </Form>
  )
}

Manipulating array items:

const {form} = useForm({
  initialValues: {
    friends: ['John', 'Jane'],
  },
})

// Add new item
form.values.friends.push('')
// Remove first item
form.values.friends.splice(0, 1)

Form actions

You can operate on form state using form object returned by useForm:

const {form} = useForm({
  initialValue: {
    email: '',
    password: '',
  },
})

form.setFieldValue('email', '[email protected]')

reset: (nextState?: {values: {}, errors: {}, touched: {}}): void

Rest form values, errors and touch states to initial state:

form.reset()

Optionally nextState object can be passed:

form.reset({
  values: {email: '[email protected]'},
  errors: {password: 'Password is required'},
  touched: {
    email: true,
    password: true,
  },
})

submit: (): void

Trigger onSubmit handler.

validate: (field?: string): void

Validate all inputs:

form.validate()

Validate single input:

form.validate('email')

setIsSubmitting: (value: boolean): void

Start or finish submission process:

form.setIsSubmitting(true)

setValues: (values: Values): void

Set all input values:

form.setValues({
  email: '',
  password: '',
})

setErrors: (errors: FormErrors): void

Set all validation errors:

form.setErrors({
  email: 'Email is invalid',
  password: 'Password is to short',
})

setTouched: (values: FormTouched): void

Set all inputs touch state:

form.setTouched({
  email: true,
  password: false,
})

setFieldValue: (field: string, value: any): void

Set single field value:

form.setFieldValue('email', '[email protected]')

setFieldError: (field: string, message?: string): void

Set single field error message:

form.setFieldError('email', 'Email is invalid')

setFieldTouched: (field: string, isTouched: boolean): void

Set single field touch state:

form.setFieldTouched('email', true)

License

MIT © EYEDEA AS

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].