Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rc-form

Package Overview
Dependencies
Maintainers
7
Versions
94
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rc-form

React High Order Form Component

  • 2.4.10
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
83K
decreased by-47.54%
Maintainers
7
Weekly downloads
 
Created

What is rc-form?

The rc-form package is a form management library for React. It provides a set of utilities to manage form state, validation, and submission, making it easier to handle complex forms in React applications.

What are rc-form's main functionalities?

Form Creation

This feature allows you to create forms with validation rules. The `createForm` function is used to wrap the form component, and `getFieldDecorator` is used to register form fields with validation rules.

```jsx
import React from 'react';
import { createForm } from 'rc-form';

class MyForm extends React.Component {
  handleSubmit = () => {
    this.props.form.validateFields((err, values) => {
      if (!err) {
        console.log('Received values of form: ', values);
      }
    });
  };

  render() {
    const { getFieldDecorator } = this.props.form;
    return (
      <form onSubmit={this.handleSubmit}>
        <div>
          {getFieldDecorator('username', {
            rules: [{ required: true, message: 'Please input your username!' }],
          })(<input type="text" placeholder="Username" />)}
        </div>
        <button type="submit">Submit</button>
      </form>
    );
  }
}

export default createForm()(MyForm);
```

Field Validation

This feature allows you to add validation rules to form fields. In this example, the email field is validated to ensure it is a valid email address and is required.

```jsx
import React from 'react';
import { createForm } from 'rc-form';

class MyForm extends React.Component {
  handleSubmit = () => {
    this.props.form.validateFields((err, values) => {
      if (!err) {
        console.log('Received values of form: ', values);
      }
    });
  };

  render() {
    const { getFieldDecorator } = this.props.form;
    return (
      <form onSubmit={this.handleSubmit}>
        <div>
          {getFieldDecorator('email', {
            rules: [
              { type: 'email', message: 'The input is not valid E-mail!' },
              { required: true, message: 'Please input your E-mail!' }
            ],
          })(<input type="email" placeholder="Email" />)}
        </div>
        <button type="submit">Submit</button>
      </form>
    );
  }
}

export default createForm()(MyForm);
```

Dynamic Form Fields

This feature allows you to dynamically add form fields. The `addField` method updates the state to add new fields, and `getFieldDecorator` is used to register these fields with validation rules.

```jsx
import React from 'react';
import { createForm } from 'rc-form';

class DynamicForm extends React.Component {
  state = { keys: [] };

  addField = () => {
    const { keys } = this.state;
    this.setState({ keys: keys.concat(keys.length) });
  };

  handleSubmit = () => {
    this.props.form.validateFields((err, values) => {
      if (!err) {
        console.log('Received values of form: ', values);
      }
    });
  };

  render() {
    const { getFieldDecorator } = this.props.form;
    const { keys } = this.state;
    return (
      <form onSubmit={this.handleSubmit}>
        {keys.map(k => (
          <div key={k}>
            {getFieldDecorator(`field-${k}`, {
              rules: [{ required: true, message: 'Please input a value!' }],
            })(<input type="text" placeholder="Dynamic Field" />)}
          </div>
        ))}
        <button type="button" onClick={this.addField}>Add Field</button>
        <button type="submit">Submit</button>
      </form>
    );
  }
}

export default createForm()(DynamicForm);
```

Other packages similar to rc-form

Keywords

FAQs

Package last updated on 09 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc