0.1.4-alpha.0 • Published 3 years ago

faumally v0.1.4-alpha.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Faumally

Install

pnpm add faumally xstate

Usage

import Faumally from "faumally";

type Form = {
    age: number;
    email: string;
    address: string;
    lastName: string;
    firstName: string;
}

const createUser = (user: any) => Promise.resolve();

const config = {required: true}

const {submit, handlers, subscribe} = Faumally<Form>({
    schema: {
        email: config,
        lastName: config,
        firstName: config,
        address: 'string',
        age: {
            ...config,
            initialValue: 0
        },
    },
    onSubmit(data) {
        return createUser(data);
    }
})

subscribe(({data, error, errors, values, submit, isSubmitting, attemptedSaveOrSubmit}
) => {
    // do stuff anytime form state changes
    ...
})

Return type


namesummary
handlersobject containing event handlers corresponding to each item in the provided schema
subscribesubscription function for changes to form state
servicethe internal XState service behinde the form
onChangegeneric form field edit event handler
onBlurgeneric form field edit done event handler
validatevalidate specific form field
generateHandlersHandlers would not be generated in situation where schema is async e.g from an API request. You'd need to update and generate the handlers manually
setset values dynamically. Avaliable options are: data, error, errors, values and schema

Example

const form = document.querySelector('form');
const email = form.querySelector('.email');
const age = form.querySelector('.age');
...

age.addEventListener('input', ({target: {value}}) => {
    handlers.age.onChange(value);
    // or
    onChange('age', value)
})

form.addEventListener('submit', e => {
    e.preventDefault();
    submit();
})

subscribe(({data, error, errors, values, isSubmitting, attemptedSaveOrSubmit}
) => {
    if (isSubmitting) alert('submitting form')

    if (attemptedSaveOrSubmit) {
        alert('Please fill all required fields')
    }
})