项目作者: beizhedenglong

项目描述 :
Building forms with vue composition API.
高级语言: TypeScript
项目地址: git://github.com/beizhedenglong/vue-hooks-form.git
创建时间: 2020-08-19T13:32:57Z
项目社区:https://github.com/beizhedenglong/vue-hooks-form

开源协议:MIT License

下载


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

  1. 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

  1. <template>
  2. <form @submit="onSubmit">
  3. <label>Username</label>
  4. <input v-model="username.value" :ref="username.ref" />
  5. <p v-if="username.error">{{ username.error.message }}</p>
  6. <label>Password</label>
  7. <input v-model="password.value" :ref="password.ref" type="password" />
  8. <p v-if="password.error">{{ password.error.message }}</p>
  9. <button type="submit">submit</button>
  10. </form>
  11. </template>
  12. <script>
  13. import { useForm } from 'vue-hooks-form'
  14. export default {
  15. setup() {
  16. const { useField, handleSubmit } = useForm({
  17. defaultValues: {},
  18. })
  19. const username = useField('username', {
  20. rule: { required: true },
  21. })
  22. const password = useField('password', {
  23. rule: {
  24. required: true,
  25. min: 6,
  26. max: 10,
  27. },
  28. })
  29. const onSubmit = (data) => console.log(data)
  30. return {
  31. username,
  32. password,
  33. onSubmit: handleSubmit(onSubmit),
  34. }
  35. },
  36. }
  37. </script>

Live Demo

Edit Vue Hooks Form Demo

API(TODO)

useForm

  1. const {
  2. values,
  3. getFieldValues,
  4. errors,
  5. validateFields,
  6. validateField,
  7. get,
  8. set,
  9. useField,
  10. handleSubmit
  11. } = useForm({
  12. defaultValues: {},
  13. shouldUnregister: true,
  14. validateMode: 'change',
  15. })

useField

  1. const {
  2. ref,
  3. value,
  4. error,
  5. validate
  6. } = useField(path, options)

Credits

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