Skip to content
  • Sponsor
  • Notifications You must be signed in to change notification settings
  • Fork 10

Building forms with vue composition API.

License

Notifications You must be signed in to change notification settings

beizhedenglong/vue-hooks-form

Folders and files

NameName
Last commit message
Last commit date

Latest commit

219419c · Nov 13, 2023

History

75 Commits
Jul 19, 2021
Jul 30, 2021
Oct 25, 2020
Oct 17, 2020
Feb 9, 2023
Jan 13, 2023
Aug 19, 2020
Aug 19, 2020
Oct 25, 2020
Jul 30, 2021
Oct 17, 2020
Oct 19, 2020
Jan 17, 2021
Oct 17, 2020
Aug 19, 2020
Sep 26, 2020
Nov 13, 2023
Oct 20, 2020
Sep 20, 2020
Oct 20, 2020
Oct 17, 2020
Feb 8, 2023

Repository files navigation

Vue Hooks Form · license build status Coverage Status

Building forms with Vue composition API.

The API is not stable and might change in the future.

Docs

Visit https://beizhedenglong.github.io/vue-hooks-form/.

Installation

  yarn add vue-hooks-form

Features

  • UI decoupling: Since It does not contain any UI code, It can be easily integrated with other UI libraries.
  • Easy to adoptable: Since form state is inherently local and ephemeral, it can be easily adopted.
  • Easy to use: No fancy stuffs, just reactive values/errors.
  • TypeScript support.

Quickstart

<template>
  <form @submit="onSubmit">
    <label>Username</label>
    <input v-model="username.value" :ref="username.ref" />
    <p v-if="username.error">{{ username.error.message }}</p>
    <label>Password</label>
    <input v-model="password.value" :ref="password.ref" type="password" />
    <p v-if="password.error">{{ password.error.message }}</p>
    <button type="submit">submit</button>
  </form>
</template>

<script>
import { useForm } from 'vue-hooks-form'

export default {
  setup() {
    const { useField, handleSubmit } = useForm({
      defaultValues: {},
    })
    const username = useField('username', {
      rule: { required: true },
    })
    const password = useField('password', {
      rule: {
        required: true,
        min: 6,
        max: 10,
      },
    })
    const onSubmit = (data) => console.log(data)
    return {
      username,
      password,
      onSubmit: handleSubmit(onSubmit),
    }
  },
}
</script>

Live Demo

Edit Vue Hooks Form Demo

API(TODO)

useForm

const {
  values,
  getFieldValues,
  errors,
  validateFields,
  validateField,
  get,
  set,
  useField,
  handleSubmit
} = useForm({
  defaultValues: {},
  shouldUnregister: true,
  validateMode: 'change',
})

useField

const {
 ref,
 value,
 error,
 validate
} = useField(path, options)

Credits

This project was inspired by react-hook-form, formik, and many other form libraries.

Sponsor this project

Sponsor

Packages

No packages published

Contributors 3