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

@freenow/uniform

v1.2.1

Published

Component for creating dynamic forms

Downloads

15

Readme

Uniform Build Status codecov

Description

This library allows you to generate forms based on provided JSON configuration.

Features

  • Use your own components on the Field level (https://jaredpalmer.com/formik/docs/api/field#component).
  • Flexible validation in config (using Yup).
  • Conditional display logic.
  • Custom options in Form level.

Tutorial

Installation

  1. Install library.
  2. Import package import formGenerator from '@freenow/uniform';.
  3. Generate form and include in your component:
    const Form = formGenerator(formConfig, components);
    
    ...
    
    <Form
        onSubmit={/*your submit function*/}
    />

Form configuration

The formConfig is an object that contains all field definitions. Let's look at simple example:

const formConfig = {
    fields: [
        {
            component: 'TextInput',
            defaultValue: 'default',
            label: 'Text input with default value',
            name: 'default',
        },
    ],
    name: 'test',
};

So the config consist of fields array and name (Unique form name). Each field element can have following properties: component, defaultValue, label, name, displayConfigs, validationConfig.

Validations

The validationConfig is implemented using Yup library. In our case validation checks that the field is string and required. You can chain validation by adding elements in methods array.

const formConfig = {
    fields: [
        {
            component: 'TextInput',
            label: 'Text input with validation logic',
            name: 'validation',
            validationConfig: {
                methods: [
                    {
                        message: 'Required',
                        type: 'required',
                    },
                ],
                type: 'string',
            },
        },
    ],
    name: 'test',
};

Display logic

The displayConfigs is implemented using Yup library. In the example below we only show the "display" field when field with the name "default" has value "show". Feel free to depend on multiple fields values by adding additional element to displayConfig.

const formConfig = {
    fields: [
        {
            component: 'TextInput',
            defaultValue: 'default',
            label: 'Text input with default value',
            name: 'default',
        },
        {
            component: 'TextInput',
            displayConfigs: [
                {
                    methods: [
                        {
                            compareValue: ['show'],
                            type: 'oneOf',
                        },
                    ],
                    type: 'string',
                    valueKey: 'default',
                },
            ],
            label: 'Text input with display logic',
            name: 'display',
        },
    ],
    name: 'test',
};

Component mapping

The component property refers to render function of each field and specified in the second paramer in generator. Let's have a look at this:

const FormContainer = ({ children, ...rest }) => (
    <form {...rest}>{children}</form>
);
FormContainer.propTypes = {
    children: PropTypes.node.isRequired,
};

const TextInput = ({ field, setFieldValue }) => (
    <input
        {...field}
        onChange={e => setFieldValue(field.name, e.target.value)}
        id={field && field.name}
    />
);
TextInput.propTypes = {
    field: PropTypes.object.isRequired,
    setFieldValue: PropTypes.func.isRequired,
};

const SubmitButton = props => <button {...props} type='submit' />;

const components = {
    FormContainer,
    SubmitButton,
    TextInput,
};

The components object is a mapping between names and the actual components. SubmitButton and FormContainer are required to create. And TextInput will be linked to component: 'TextInput' in the form config. To create custom components for your field please follow Formik docs on the Field element.

Tests

By default, mandatory minimum test coverage of the library is set to 80%.

Contributing

Authors and acknowledgment

Maintainers