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

@kyump/final-form-generator

v1.1.1

Published

tool used to build form with react final form

Downloads

15

Readme

Final Form Generator

Final Form Generator is a simple hook for computing props for react-final-form component.

Installation

You need some depedencies in order to use this hook:

Then you can run:

npm install @kyump/final-form-generator

Usage

import React from 'react';
import * as Yup from 'yup';
import {Form, Field} from 'react-final-form';
import useFormGenerator from '@kyump/final-form-generator';

const fields = [
    {
        type: 'text',
        name: 'text',
        label: 'text',
    },
    {
        type: 'email',
        name: 'email',
        label: 'email',
    },
];

const renderInput = ({field, children = [], index}) => {
    switch (field.type) {
        case 'email':
            return (
                <Field
                key={index}
                    name={field.name}
                    component="input"
                    type="email"
                    placeholder={field.label}
                />
            );
        case 'text':
            return (
                <Field
                    key={index}
                    name={field.name}
                    component="input"
                    type="text"
                    placeholder={field.label}
                />
            );
        default:
            return <span key={key}>{`${field.type} not handled`}</span>;
    }
};

export const defaultValidation = (type?: string) => {
    switch (type) {
        case 'email':
            return Yup.string()
                .email()
                .required();
        default:
            return Yup.string()
                .trim()
                .required();
    }
};

const ExampleForm = () => {
	const {dom, validate, decorators, loading} = useFormGenerator({
		defaultValidation,
		fields,
		renderInput,
	});
	return loading ? (
		'Loading'
	) : (
		<Form
			decorators={decorators}
			onSubmit={values => alert(values)}
			validate={validate}
			render={({
				handleSubmit,
				form,
				submitting,
				pristine,
				invalid,
				values,
				errors,
			}) => (
				<form onSubmit={handleSubmit}>
					<div>
						{dom}
						<button type="submit" disabled={invalid || submitting || pristine}>
							Submit
						</button>
					</div>
					<pre>{JSON.stringify(values, 0, 2)}</pre>
					<pre>{JSON.stringify(errors, 0, 2)}</pre>
				</form>
			)}
		/>
	);
};

export default ExampleForm;

API

UseFinalFormGeneratorProps :

  • defaultValidation (required): Function the take type of the field and return its default yup validation
  • fields (required): Array of fields
  • renderInput (required): Function used to render elements of the form. It takes an object with 3 props field (BasicFieldInterface), its children (?React$Node[]) and the index of the field in the fields array
  • customValidationSchema?: Yup base schema is you want to use noSortEdges for example.
const customValidationSchema = Yup.object().shape({
    customer: Yup.object().shape({}, ['phone', 'email']),
});

Here, if you put validation on field email or phone it will be add to this shape.

  • preValidate: Function than can be called before yup validation. It has the form values as param and should return an object with error message ({[string]: string})

UseFinalFormGeneratorResult :

  • decorators: Decorators for decorators props of react-final-form component
  • dom: Inputs of the form (React$Node[])
  • validate: Validation function for validate props of react-final-form component

License

MIT