@cat-react/form

中文文档
0.1.2发布于 2018/01/30MITESM + CJS近 30 天下载 5577

A simple yet powerful library which helps creating validated forms in react.

原始使用文档
文档目录6 节
  1. Installation
  2. Getting Started
  3. Example Custom TextInput
  4. Contribution
  5. How to build a release
  6. License

cat-react

@cat-react / form Build Status codecov npm version

A simple yet powerful library which helps creating validated forms in react. This project is inspired by formsy-react.

Installation

npm package

  • Install the dependency @cat-react/form
    (e.g. with yarn add @cat-react/form or npm install @cat-react/form --save)
  • Import the Components with import {Form, Input} from '@cat-react/form';

Getting Started

API DocumentationExamples

Are you looking for a simple way to create validated forms with React?

Congratulations! Your search is over, because @cat-react/form offers you a simple way to create either frontend- or backend-validated forms. Validations can either be processed synchronous or asynchronous and the state of the form is being refreshed in real time.

Take a look at the examples to find out how to create the form of your desire.

<Form>
    <MyInput name="email"
             validations={{
                 isEmail: true,
                 isRequired: true
             }}/>
    <MyInput name="email_confirm"
             validations={{
                 isRequired: true,
                 equalsField: 'email'
             }}
             messages={{
                 isRequired: 'Please confirm your email address.',
                 equalsField: 'The email addresses do not match each other.'
             }}/>
</Form>

Example Custom TextInput

Here you can see an example of an custom TextInput which shows how you can implement your own Inputs:

import React from 'react';
import {Input} from '@cat-react/form'

@Input
export default class BasicInput extends React.Component {
    onChange(event) {
        this.props.setValue(event.target.value);
    }

    getClassName() {
        let className = 'form-control';
        if (!this.props.isPristine()) {
            if (this.props.isValid()) {
                const isWarning = this.props.getMessages().length > 0;
                if (isWarning) {
                    className += ' warning';
                }
            } else {
                className += ' error';
            }
        }
        return className;
    }

    renderMessages() {
        let messages = [];
        if (!this.props.isPristine()) {
            messages = this.props.getMessages();
        }

        if (!messages || messages.length <= 0) {
            return null;
        }

        let className = 'errorText';
        if (this.props.isValid()) {
            className = 'warningText';
        }

        return <ul className={className}>{messages.map((message, i) => <li key={i}>{message}</li>)}</ul>;
    }

    render() {
        return (
            <div className="form-group">
                <label htmlFor={this.props.name}>{this.props.label} {this.props.isRequired() ? '*' : null}</label>
                <input type={this.props.type}
                       className={this.getClassName()}
                       id={this.props.name}
                       aria-describedby={this.props.name}
                       placeholder={this.props.placeholder}
                       value={this.props.getValue()}
                       onChange={this.onChange.bind(this)}
                       onBlur={this.props.touch}/>
                {this.renderMessages()}
            </div>
        );
    }
}

Note: You need the babel-plugin-transform-decorators plugin to use the @Input decorator. If you don't want or cannot add this plugin to you webpack config you need to use the HOC as a function (like the connect() HOC of Redux).

Example: export default Input(BasicInput)

See the Babel Documentation for a detailed guide about how to add decorators to your application.

Contribution

The project requires at least the latest stable version of node and npm. You also need to have yarn installed globally.

Two simple steps to get the things running on your local machine:

  • Fork the repo
  • Execute yarn

You can run the examples with yarn run examples and the tests with yarn test.

How to build a release

  • update the CHANGELOG.md with all changes regarding the new release
  • update the release version in the package.json
  • push the changes
  • build the project locally with npm run build
  • cd into the dist folder and run npm publish --access public
  • draft a new release at Github with the contents of the CHANGELOG.md file

License

MIT License

Copyright (c) 2018 Catalysts GmbH

版本与发布记录

@cat-react/form 共发布 6 个版本,首次发布于 2017/09/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.2latest2018/01/308 年前
0.1.12017/11/228 年前
0.1.02017/10/039 年前
0.0.32017/09/189 年前
0.0.22017/09/189 年前
0.0.12017/09/129 年前

关于 @cat-react/form 的常见问题

@cat-react/form 是什么?

A simple yet powerful library which helps creating validated forms in react.

如何安装 @cat-react/form?

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

@cat-react/form 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cat-react__form 是否存在。

@cat-react/form 支持 ESM 还是 CommonJS?

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

@cat-react/form 可以商用吗?

@cat-react/form 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cat-react/form 的下载量有多少?

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

@cat-react/form 还在维护吗?

最新版本 0.1.2 发布于 2018/01/30(8 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/09/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。