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

react-cnf-form

v0.1.8

Published

React configuration form

Downloads

14

Readme

react-cnf-form

react-cnf-form组件是在 antd form组件的基础上封装; 其目的是为了通过配置化实现form表单功能,简化form表单页的实现 让业务能够方便的拆分为各种组件。实现业务功能的解耦;方便其维护;

简单示例

// options.js 配置项文件
export default [{
    hidden: true,
    name: 'id',
}, {
    // itemType: 'input',   // 默认类型为'input'可以省略;
    name: 'account',
    label: 'account',
    itemProps: {    // 如果不需要 对组件传入参数,该项也可以省略
    }
},
function (render) { // 一般在需要动态获取配置项数据时使用函数渲染
    return render([
        {
            itemType: 'password',
            label: 'password',
            name: 'password',
            required: true,
            rules: [{ required: true, message: 'Please input your password!' }],
            itemProps: {
                maxLength: 20
            }
        }
    ]);
}];
// 在组件中使用 react-cnf-form 
import CnfForm from 'react-cnf-form';
import {Form, Button} from 'antd';
import items from './options';

export function Form() {
    const [formIns] = Form.useForm();
  
    function onFinish(values) {
        // TODO: process values;
    }
    return (
        <CnfForm form={formIns} items={items} onFinish={onFinish}>
            <section className="mt-10 btn-group-container">
                <Button htmlType="submit" type="primary">
                    submit
                </Button>
            </section>
        </CnfForm>    
    );
}

说明

主要思想:

  1. 不影响ant组件功能的所有配置,只提供配置化渲染功能

  2. 增加几个额外必要的配置项来处理结构问题;以下是每条配置项中增加的配置项说明:
    itemType 指定组件类型
    itemRender 一般用来定义不能复用功能的组件(模块),提供该配置,itemType 会忽略;
    beforeContent 用来渲染组件之前的内容
    afterContent 用来渲染组件之后的内容
    itemProps 配置 itemType 指定组件的配置项

  3. 通过提供一个Wrap组件来实现表单项目的分块

interface WrapOption {
    Wrap: ComponentType;
    items: ItemOptions;
    key?: any;
}
  1. CnfForm 组件支持所有antd From组件的配置
export interface CnfFromProps extends FormProps {
    form: FormInstance;
    items: ItemOptions;
}

由于form实例(FormInstance)在表单项中可能会经常遇到这里设置为了必选项;

  1. ItemOption 配置项支持3中类型:对象、渲染函数、WrapOption
type ItemOption = (Option | RenderFn | WrapOption);

type Render = (args: ItemOption | ItemOptions, fIns: FormInstance) => ReactNode[];
type RenderFn = (render: Render, fIns: FormInstance) => ReactNode[];

对象 Option 支持所有antd From.Item 的配置项 渲染函数RenderFn 一般用于需要动态控制配置项的使用使用; WrapOption 对于复杂的表单经常需要分区块展示,这里提供了一个包裹组件配置来实现这种功能

  1. 有些常用组件(文件上传预览模块等)可以通过addItemType添加到组件库
addItemType = (itemTypeName: string, CMP: ComponentType) => undefined;
import {FC} from 'react';
import {addItemType} from 'react-cnf-form';
import {Select} from 'antd';

const CSelect: FC = (props) => <Select {...props} />;

addItemType('customizeSelect', CSelect);
// options.js

[...,
{
    itemType: 'customizeSelect',
    label: 'customizeCMP',
    name: 'customize'
},...]

FAQ

  1. 支持哪些组件?

    Input: (itemType: 'input')可以不用填,默认就是'input'
    InputNumber: (itemType: 'inputNumber')
    Input.TextArea: (itemType: 'textarea')
    Input.Password: (itemType: 'password')
    Cascader: (itemType: 'cascader')
    Checkbox: (itemType: 'checkbox')
    Checkbox.Group: (itemType: 'checkboxGroup')
    Radio: (itemType: 'radio')
    Radio.Group: (itemType: 'radioGroup')
    Rate: (itemType: 'rate')
    Switch: (itemType: 'switch')
    DatePicker: (itemType: 'datePicker')
    DatePicker.RangePicker: (itemType: 'rangePicker')
    TimePicker: (itemType: 'timePicker')
    Select: (itemType: 'select')
    TreeSelect: (itemType: 'treeSelect')
    Tree: (itemType: 'tree')
    Mentions: (itemType: 'mentions')
    Transfer: (itemType: 'transfer')
    Upload: (itemType: 'upload')

  2. 隐藏字段怎么配置?
    支持隐藏字段(Form.Item 本身就支持隐藏字段),配置如下:
    { hidden: true, name: 'id' }

  3. 无边框展示字段,如果不想自定义一个特别的组件可以使用无边框Input:
    支持展示字段(Input 字段可以设置无边框样式),配置如下:
    { name: 'description', itemProps: { disabled: true, bordered: false } }

  4. 如何动态增加一个自定义的组件?
    参加上面说明中的第6条
    注意,如果你需要自定义组件,那么你需要遵守ant-form 自定义Form.Item组件的规范,详细参考antd 的文档

其它关于antd Form.Item 的说明

有些字段其值并不是通过value 获取,如:Switch组件 可以通过Form.ItemvaluePropName来指定;
Form.Item 内的组件如果不是只有一个children ,则需要指定单独的
<Form.Item nostyle ><Input /></Form.Item>包裹;
在当前Form组件内部已经处理;但是如果配置项中使用itemRender 或者其它自定义的 Item项,需要接收value值并正确处理其值的改变;