Socket
Book a DemoInstallSign in
Socket

@casthub/form

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

@casthub/form

Vue 3 Utility Hook for Form handling and validation

1.0.3
unpublished
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source
NPM Discord Apache-2.0

Form Handling and Validation Hook for Vue 3

@casthub/form provides Vue 3 Hooks for consuming, validating and managing Forms.

Inspired by vue-hooks-form.

  • Asynchronous validation via async-validator
  • No forced HTML structure/format
  • Error handling
  • TypeScript

Installation

This package is available via NPM:

yarn add @casthub/form

# or

npm install @casthub/form

Usage

A full example App is provided in the example folder.

<template>
    <form @submit.prevent="submit">
        <label for="email">Email Address</label>
        <input
            type="email"
            name="email"
            id="email"
            placeholder="Email Address"
            :disabled="loading"
            @focus="email.clearError"
            v-model="email.value"
        />
        <p class="err" v-if="email.error">{{ email.error.message }}</p>
        <label for="email">Password</label>
        <input
            type="password"
            name="password"
            id="password"
            placeholder="Password"
            :disabled="loading"
            @focus="password.clearError"
            v-model="password.value"
        />
        <p class="err" v-if="password.error">{{ password.error.message }}</p>
        <button
            type="submit"
            :disabled="loading"
        >Login</button>
    </form>
</template>

<script lang="ts" setup>
    import { useForm } from '@casthub/form';

    const { useField, handle, loading } = useForm<{
        email: string;
        password: string;
    }>({
        defaults: {
            email: 'hello@example.com',
        },
    });

    const email = useField('email', {
        type: 'email',
        required: true,
    });
    const password = useField('password', {
        required: true,
    });

    const submit = handle(async ({ email, password }) => {
        alert(`Email: ${email} Password: ${password}`);
    });
</script>

API

useForm

Options

OptionDefaultTypeDescription
defaults{}Record<string, any>Optionally provide defaults for the various fields in this object by key -> value pairs.
validationModesubmit'change'|'submit'NOT IMPLEMENTED YET. Whether to validate input once submitted

Response

OptionTypeDescription
useFieldField<unknown>Documented below.
handle(run: values => Promise<void>) => async (e?: Event) => Promise<void>Registers the asynchronous handler that runs once a form is submitted and successfully validated.
reset() => voidReset the Form to tis default state.
validate() => Promise<boolean>Manually trigger validation and error handling.
clearErrors() => voidClear all errors for all fields.
loadingRef<boolean>Whether the form is currently executing.
destroy() => voidDestroy and clean-up the Form handler. Happens automatically during onBeforeUnmount.

useField

Options

Currently the options object provided to useField is inheritted from async-validator and all options are forwarded as a validation field.

Response

OptionTypeDescription
errorsValidateError[]An array of all Errors set against this Field.
errorValidateError|nullOptimistically picks one, if any, of the Errors against the field.
hasErrorComputedRef<boolean>Whether or not the Field has 1 or more errors.
setError(text: string) => voidManually set the error on this field.
clearError() => voidClears all Errors currently set against this Field.
valueWritableComputedRef<unknown>The value for the field, compatible with v-model.

To-do

  • Add a test suite

Keywords

typescript

FAQs

Package last updated on 28 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.