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 🙏

© 2025 – Pkg Stats / Ryan Hefner

antx

v5.8.0

Published

Ant Design Form Simplified

Readme

Link in bio to widgets, your online home screen. ➫ 🔗 kee.so


Ant Design Form 简化版,以最简便的方式搭建表单。

npm version npm downloads npm bundle size GitHub npm peer dependency version npm peer dependency version

English · 简体中文


介绍

antx 提供一套 antd 混合表单组件的集合:

1. 告别繁琐的 <Form.Item>rules
直接在表单组件 (如 Input) 上混写 Form.Item props 与组件 props (完整 TypeScript 支持),显著简化代码。

2. 字符串 rules (仅增强,原 rules 写法同样支持)
提供 string 形式 rules,例如 rules={['required', 'max=10'']}rules={[{ required: true }, { max: 10 }]}

3. 未新增任何 props
所有 props 均为 antd 组件原有 props,未新增任何其它 props,减少心智负担。

同时 antx 还提供了 2 个助手组件 (WrapperColWatch) ,以及一个工具函数 create() 用于轻松拓展已有表单组件。

安装

pnpm add antx
# or
yarn add antx
# or
npm i antx

使用

import { Button, Form } from 'antd';
import { Input, Select, WrapperCol } from 'antx';

const App = () => {
  return (
    <Form labelCol={{ span: 8 }} wrapperCol={{ span: 16 }}>
      <Input label="Name" name="name" rules={['required', 'string']} />
      <Select
        label="Gender"
        name="gender"
        rules={['required', 'number']}
        options={[
          { value: 1, label: 'Male' },
          { value: 2, label: 'Female' },
        ]}
      />
      <InputNumber
        label="Age"
        name="age"
        rules={['required', 'number', 'min=0']}
      />
      <WrapperCol>
        <Button type="primary" htmlType="submit">
          Submit
        </Button>
      </WrapperCol>
    </Form>
  );
};

export default App;

Edit antx

API

1. 混合组件

  1. AutoComplete
  2. Cascader
  3. Checkbox
  4. DatePicker
  5. Input
  6. InputNumber
  7. Mentions
  8. Radio
  9. Rate
  10. Select
  11. Slider
  12. Switch
  13. TimePicker
  14. Transfer
  15. TreeSelect
  16. Upload
  17. CheckboxGroup Checkbox.Group
  18. DateRange DatePicker.RangePicker
  19. TextArea Input.TextArea
  20. Search Input.Search
  21. Password Input.Password
  22. RadioGroup Radio.Group
  23. TimeRange TimePicker.RangePicker
  24. Dragger Upload.Dragger

对于以上所有混合组件,props classNamestylenametooltip 将传给 Form.Item

如需传给内部表单组件,请使用 selfClassselfStyleselfNameselfTooltip

2. 助手组件

  1. Watch: 用于监听表单字段变化,可实现仅局部 re-render,更精细、性能更好

| Props | 说明 | 类型 | 默认值 | | ----------- | --------------------------------------------------------- | ------------------------------------------------------------ | ------- | | name | 需监听的字段 | NamePath | - | | list | 需监听的字段列表 (与 name 互斥) | NamePath[] | - | | children | Render props 形式。获取被监听的值 (或列表) ,返回 UI | (next: any, prev: any, form: FormInstance) => ReactNode | - | | onlyValid | 被监听的值非 undefined 时,才触发 children 渲染 | boolean | false | | onChange | 获取被监听的值 (或列表) ,处理副作用 (与 children 互斥) | (next: any, prev: any, form: FormInstance) => void | - |

// Watch 示例
import { Watch } from 'antx';

<Form>
  <Input label="歌曲" name="song" />
  <Input label="歌手" name="artist" />

  <Watch name="song">
    {(song) => {
      return <div>歌曲:{song}</div>;
    }}
  </Watch>

  <Watch list={['song', 'artist']}>
    {([song, artist]) => {
      return (
        <div>
          歌曲:{song},歌手:{artist}
        </div>
      );
    }}
  </Watch>
</Form>;
  1. WrapperCol: 简化布局代码,props 与 Form.Item 完全一致,用于 UI 需与输入框对齐的情况
// WrapperCol 示例
import { WrapperCol } from 'antx';

<Form>
  <Input label="歌曲" name="song" />
  <WrapperCol>这是一条与输入框对齐的提示</WrapperCol>
</Form>;

3. create() 工具函数

  • create(): 将已有表单组件,包装为支持 Form.Item props 混写的组件,轻松拓展现有组件
import { create } from 'antx';

// 拓展前
<Form>
  <Form.Item label="歌曲" name="song" rules={{ required: true }}>
    <MyCustomInput />
  </Form.Item>
</Form>;

// 使用 create() 拓展
const MyCustomInputPlus = create(MyCustomInput);

// 拓展后
<Form>
  <MyCustomInputPlus label="歌曲" name="song" rules={['required']} />
</Form>;

4. 字符串 rules

| 字符串 | 对应 | 说明 | | --------------- | -------------------------------------- | ------------ | | 'required' | { required: true } | | | 'required=xx' | { required: true, message: 'xx' } | | | 'string' | { type: 'string', whitespace: true } | | | 'pureString' | { type: 'string' } | | | 'number' | { type: 'number' } | | | 'array' | { type: 'array' } | | | 'boolean' | { type: 'boolean' } | | | 'url' | { type: 'url' } | | | 'email' | { type: 'email' } | | | 'len=20' | { len: 20 } | len === 20 | | 'max=100' | { max: 100 } | max <= 100 | | 'min=10' | { min: 10 } | min >= 10 |

// 字符串 rules 示例
<Form>
  <Input label="歌曲" name="song" rules={['required', 'min=0', 'max=50']} />
</Form>

对比

Ant Plus 与 Ant Design 表单代码对比:

Comparison

协议

MIT License (c) nanxiaobei