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

@firecms/formex

v3.0.0-beta.6

Published

Formex is a lightweight, flexible library designed to simplify form handling within React applications. By leveraging React's powerful context and hooks features, Formex allows for efficient form state management with minimal boilerplate code.

Downloads

1,980

Readme

Formex - React Form Library

Formex is a lightweight, flexible library designed to simplify form handling within React applications. By leveraging React's powerful context and hooks features, Formex allows for efficient form state management with minimal boilerplate code.

Features

  • Lightweight and easy to integrate
  • Supports custom field components
  • Built-in validation handling
  • Provides both field-level and form-level state management

Installation

To install Formex, you can use either npm or yarn:

npm install @firecms/formex

# or if you're using yarn

yarn add @firecms/formex

Quick Start

To get started with Formex, you first need to create your form context and form controller using the useCreateFormex hook. Then, you can structure your form using the <Field /> components provided by Formex.

Step 1: Create your form controller

import React from 'react';
import { useCreateFormex } from 'formex-library';

const MyForm = () => {
    const formController = useCreateFormex({
        initialValues: {
            name: '',
            email: '',
        },
        // Optionally add a validation function
        // validation: values => {
        //   const errors = {};
        //   if (!values.name) errors.name = 'Name is required';
        //   return errors;
        // },
        onSubmit: (values) => {
            console.log('Form Submitted:', values);
        },
    });

    return (
        <form onSubmit={formController.handleSubmit}>
            {/* Field components go here */}
        </form>
    );
};

Step 2: Use the <Field /> component

import { Field } from 'formex-library';

// Inside your form component
<Field name="name">
  {({ field }) => (
    <input
      {...field}
      placeholder="Your name"
    />
  )}
</Field>

<Field name="email">
  {({ field }) => (
    <input
      {...field}
      type="email"
      placeholder="Your email"
    />
  )}
</Field>

<button type="submit">Submit</button>

Handling Submissions

Wrap your form inputs and submit button within a form element and pass the submitForm method from your form controller to the form's onSubmit event:

<form onSubmit={formController.handleSubmit}>
    {/* Fields and submit button */}
</form>

API Reference

useCreateFormex

Hook to create a form controller.

Parameters

  • initialValues: An object with your form's initial values.
  • initialErrors (optional): An object for any initial validation errors.
  • validation (optional): A function for validating form data.
  • validateOnChange (optional): If true, validates fields whenever they change.
  • onSubmit: A function that fires when the form is submitted.

<Field />

A component used to render individual form fields.

Props

  • name: The name of the form field.
  • as (optional): The component or HTML tag that should be rendered. Defaults to "input".
  • children: A function that returns the field input component. Receives field props as its parameter.

Example

<Field name="username">
  {({ field }) => <input {...field} />}
</Field>

Customization

Formex is designed to be flexible. You can create custom field components, use any validation library, or integrate with UI component libraries.

Using with UI Libraries

import { Field } from 'formex-library';
import { TextField } from 'some-ui-library';

<Field name="username">
  {({ field }) => (
    <TextField {...field} label="Username" />
  )}
</Field>

Custom Validation

Leverage the validation function in useCreateFormex to integrate any validation logic or library.

const validate = values => {
  const errors = {};
  if (!values.email.includes('@')) {
    errors.email = 'Invalid email';
  }
  return errors;
};

Conclusion

Formex provides a simple yet powerful way to manage forms in React applications. It reduces the amount of boilerplate code needed and offers flexibility to work with custom components and validation strategies. Whether you are building simple or complex forms, Formex can help streamline your form management process.

For further examples and advanced usage, refer to the Formex documentation or source code.