npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-form-fields

v1.0.9

Published

[Demo](https://vlad88vlad.github.io/use-form-fields)

Downloads

17

Readme

use-form-fields

Demo

To get it started, add use-form-fields to your project:

npm install --save use-form-fields

or

yarn add use-form-fields --save

Please note that use-form-fields requires react@>=16.8.6 as a peer dependency.

Form Schema

import { useForm } from 'use-form-fields';


const formSchema = {
    ['name_field']: {
        type: `'text' | 'password' | 'email' | 'range' | 'radio' | 'checkbox'` (optional),
        value: `string (optional)`,
        checked: `boolean (optional)`,
        minLen: `number (optional)`,
        maxLen: `number (optional)`,
        validations: `array of function`,
        fieldProps: ` native props of html element`,
        immediatelyValidate: ` boolean (optional)`,
        error: `string (optional`,
    }
}

const { form } = useForm(formSchema);
  

form consists of:

fields
{
    [key: string]: {
        fieldProps: {
            value: string,
            name: string,
            required?: boolean,
            onChange: onChangeType
        },
        error?: string,
        toValid?: () => void,
        radioOptions?: any[],
    }
}
toJSON
function () => return { key: value }
toValidate
 function () => return boolean

Examples

SimpleForm Demo

import { useForm } from 'use-form-fields';


export const SimpleForm = () => {
    const { form: { fields }, form } = useForm({
        test: {},
    });

    const onSubmit = (e) => {
        e.preventDefault();
        if (form.toValidate()) {
            console.log(form.toJSON());
        }
    };

    return (
        <form onSubmit={onSubmit}>
            <input {...fields.test.fieldProps} />
            <button type="submit">submit</button>
        </form>
    );
};

ValidationForm Demo

import { useForm } from 'use-form-fields';


const validateFirstName = (value) => {
    if (value === 'John') {
        return 'John already exist';
    }

    return null;
};
const validateLastName = (value) => {
    if (value === 'Smith') {
        return 'Smith already exist';
    }

    return null;
};

export const ValidationForm = () => {
    const { form: { fields }, form } = useForm({
        firstName: { value: 'John', validations: [validateFirstName] },
        lastName: { validations: [validateLastName] },
        email: { type: 'email', required: true, minLen: 8 },
        password: { type: 'password', required: true, minLen: 8 },
    });

    const onSubmit = (e) => {
        e.preventDefault();
        if (form.toValidate()) {
            console.log(form.toJSON());
        }
    };

    return (
        <form onSubmit={onSubmit}>
            <input {...fields.firstName.fieldProps} />
            <span>{fields.firstName.error}</span>
            <input {...fields.lastName.fieldProps} />
            <span>{fields.lastName.error}</span>
            <input {...fields.email.fieldProps} />
            <span>{fields.email.error}</span>
            <input {...fields.password.fieldProps} />
            <span>{fields.password.error}</span>
            <button type="submit">submit</button>
        </form>
    );
};

LoginForm

import { useForm } from 'use-form-fields';


export const LoginForm = () => {
    const { form: { fields }, form } = useForm({
        email: {
            type: 'email',
        },
        password: {
            type: 'password',
        },
    });

    const onSubmit = (e) => {
        e.preventDefault();
        if (form.toValidate()) {
            console.log(form.toJSON());
        }
    };

    return (
        <form onSubmit={onSubmit}>
            <input {...fields.email.fieldProps} />
            <input {...fields.password.fieldProps} />
        </form>
    );
};

CheckboxForm

import { useForm } from 'use-form-fields';


export const CheckboxForm = () => {
    const { form: { fields }, form } = useForm({
        test: {
            type: 'checkbox',
            checked: false,
        },
    });

    const onSubmit = (e) => {
        e.preventDefault();
        if (form.toValidate()) {
            console.log(form.toJSON());
        }
    };

    return (
        <form onSubmit={onSubmit}>
            <input {...fields.test.fieldProps} />
            <button type="submit">submit</button>
        </form>
    );
};

SelectForm

import { useForm } from 'use-form-fields';


export const SelectForm = () => {
    const { form: { fields }, form } = useForm({
        test: {
            value: '',
        },
    });

    const onSubmit = (e) => {
        e.preventDefault();
        if (form.toValidate()) {
            console.log(form.toJSON());
        }
    };

    return (
        <form onSubmit={onSubmit}>
            <select {...fields.test.fieldProps}>
                <option value="">please select</option>
                <option value="male">male</option>
                <option value="female">female</option>
            </select>
            <button type="submit">submit</button>
        </form>
    );
};

RadioForm

import { useForm } from 'use-form-fields';


export const RadioForm = () => {
    const { form: { fields }, form } = useForm({
        test: {
            type: 'radio',
            value: '',
            options: [
                {
                    option: 'male',
                }, {
                    option: 'female',
                },
            ],
        },
    });

    const onSubmit = (e) => {
        e.preventDefault();
        if (form.toValidate()) {
            console.log(form.toJSON());
        }
    };

    return (
        <form onSubmit={onSubmit}>
            {fields.test.radioOptions?.map((optionField) => (
                <input {...optionField} />
            ))}
            <button type="submit">submit</button>
        </form>
    );
};