@basenative/forms

中文文档
3.0.0发布于 2026/10/31Apache-2.0TypeScriptES Module构建来源已验证

Signal-based form state with dirty/touched tracking and schema validation adapters

原始使用文档
文档目录8 节
  1. Install
  2. Quick Start
  3. API
  4. createField(initial, options?)
  5. createForm(options)
  6. zodAdapter(schema)
  7. Built-in Validators
  8. License

@basenative/forms

Signal-based form state management with validation and field tracking

Part of the BaseNative ecosystem — a signal-based web runtime over native HTML.

Install

npm install @basenative/forms

Quick Start

import { createForm, createField, required, email, minLength } from '@basenative/forms';

const form = createForm({
  fields: {
    email: createField('', { validators: [required(), email()] }),
    password: createField('', { validators: [required(), minLength(8)] }),
  },
});

// Read field state reactively
import { effect } from '@basenative/runtime';

effect(() => {
  console.log(form.fields.email.value());
  console.log(form.fields.email.errors());
  console.log(form.valid());
});

// Handle submission
form.fields.email.setValue('[email protected]');
form.fields.password.setValue('secret123');

if (form.valid()) {
  const data = form.values();
  await submitToApi(data);
}

API

createField(initial, options?)

Creates a reactive form field. Returns:

  • value — Signal with the current field value.
  • touched — Signal, true after the field has been blurred.
  • dirty — Signal, true after the value has changed.
  • errors — Computed signal with array of validation error messages.
  • valid / invalid — Computed booleans.
  • firstError — Computed signal with the first error message or null.
  • setValue(value) — Updates the value and marks the field dirty.
  • touch() — Marks the field as touched.
  • reset() — Resets value, touched, dirty, and server errors to initial state.
  • setServerErrors(errors) — Injects server-side validation errors.

Options

  • validators — Array of validator functions (see built-in validators below).
  • transform — Function applied to every new value before storing it.

createForm(options)

Creates a form group from a map of fields. Returns:

  • fields — The field map passed in.
  • valid — Computed signal, true when all fields are valid.
  • values() — Returns a plain object of current field values.
  • reset() — Resets all fields.
  • touch() — Marks all fields as touched.

zodAdapter(schema)

Adapts a Zod schema for use as a form-level validator.

Built-in Validators

  • required() — Value must be non-empty.
  • minLength(n) — String must be at least n characters.
  • maxLength(n) — String must be at most n characters.
  • pattern(regex, message?) — Value must match a regular expression.
  • email() — Value must be a valid email address.
  • min(n) — Numeric value must be at least n.
  • max(n) — Numeric value must be at most n.
  • custom(fn) — Runs fn(value) and uses its return value as the error message (return falsy for valid).

License

Apache-2.0

版本与发布记录

@basenative/forms 共发布 11 个版本,首次发布于 2026/03/20;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.0latest2026/10/312 天前
2.4.02026/10/268 天前
2.3.22026/10/2212 天前
2.3.12026/10/2212 天前
2.3.02026/10/2212 天前
2.2.02026/10/2113 天前

关于 @basenative/forms 的常见问题

@basenative/forms 是什么?

带有dirty/touch跟踪和模式验证适配器的基于信号的表单状态

如何安装 @basenative/forms?

在项目目录运行 npm install @basenative/forms,或使用 pnpm add / yarn add / bun add。

@basenative/forms 支持 TypeScript 吗?

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

@basenative/forms 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@basenative/forms 可以商用吗?

@basenative/forms 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@basenative/forms 还在维护吗?

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