@firecms/formex

中文文档
3.5.2发布于 2026/11/11TypeScriptESM + CJS构建来源已验证近 30 天下载 6376

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.

原始使用文档
文档目录13 节
  1. Features
  2. Installation
  3. Quick Start
  4. Step 1: Create your form controller
  5. Step 2: Use the <Field /> component
  6. Handling Submissions
  7. API Reference
  8. useCreateFormex
  9. <Field />
  10. Customization
  11. Using with UI Libraries
  12. Custom Validation
  13. Conclusion

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.

版本与发布记录

@firecms/formex 共发布 386 个版本,首次发布于 2024/02/13;过去 12 个月发布了 79 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.5.2latest2026/11/114 天前
3.5.2-canary.a0001bdcanary2026/11/114 天前
3.5.2-canary.a36570e2026/10/2718 天前
3.5.2-canary.09e12812026/10/2718 天前
3.5.2-canary.c5764a32026/10/131 个月前
3.5.2-canary.16bf7bb2026/10/131 个月前

其他发布通道:pre → 3.0.0-canary.297,canary → 3.5.2-canary.a0001bd。

关于 @firecms/formex 的常见问题

@firecms/formex 是什么?

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.

如何安装 @firecms/formex?

在项目目录运行 npm install @firecms/formex,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18.3.1 || >=19.0.0)、react-dom(>=18.3.1 || >=19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@firecms/formex 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@firecms/formex 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@firecms/formex 可以商用吗?

@firecms/formex 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@firecms/formex 的下载量有多少?

根据 npm 官方下载统计,@firecms/formex 在 2026/10/15 – 2026/11/13 这 30 天内被下载 6,376 次,日均约 213 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@firecms/formex 还在维护吗?

最新版本 3.5.2 发布于 2026/11/11(4 天前);至今共发布 386 个版本,其中过去 12 个月发布了 79 个;首次发布于 2024/02/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。