React Performance First Form Component

2.7.1 · active · verified Sun Apr 19

rc-field-form is a foundational and performance-optimized React component library designed for building highly customizable forms. It offers a low-level API for form state management, field registration, and validation without dictating specific UI or design patterns, making it adaptable for various React environments, including React Native. The current stable version, 2.7.1, is part of the broader react-component ecosystem. While this specific package has seen less frequent direct updates recently, the underlying project shows continuous development through related components like `@rc-component/form`, which receives frequent patch and minor versions for bug fixes and feature enhancements. Key differentiators include its 'performance first' philosophy, flexible API for integrating custom UI components, and robust validation capabilities powered by `@rc-component/async-validator`.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates a basic login form with input fields, validation, initial values, and submission handling using `rc-field-form` components and the `useForm` hook.

import React from 'react';
import Form, { Field, useForm } from 'rc-field-form';

interface LoginFormValues {
  username?: string;
  password?: string;
  remember?: boolean;
}

const Input = ({ value = '', ...props }: React.InputHTMLAttributes<HTMLInputElement>) => (
  <input value={value} {...props} style={{ border: '1px solid #ccc', padding: '8px', borderRadius: '4px', width: '100%', boxSizing: 'border-box' }} />
);

const DemoForm: React.FC = () => {
  const [form] = useForm<LoginFormValues>();

  const onFinish = (values: LoginFormValues) => {
    console.log('Form submission successful:', values);
    alert(`Submitted: ${JSON.stringify(values, null, 2)}`);
  };

  const onFinishFailed = (errorInfo: any) => {
    console.error('Form submission failed:', errorInfo);
  };

  return (
    <Form
      form={form}
      initialValues={{ username: 'guest', remember: true }}
      onFinish={onFinish}
      onFinishFailed={onFinishFailed}
      style={{ padding: '25px', border: '1px solid #eee', borderRadius: '8px', maxWidth: '450px', margin: '30px auto', boxShadow: '0 2px 10px rgba(0,0,0,0.05)' }}
    >
      <h3 style={{ marginBottom: '20px', color: '#333' }}>Login</h3>
      <div style={{ marginBottom: '15px' }}>
        <label htmlFor="username" style={{ display: 'block', marginBottom: '8px', fontWeight: 'bold' }}>Username:</label>
        <Field name="username" rules={[{ required: true, message: 'Please input your username!' }]}>
          <Input id="username" placeholder="Enter username" />
        </Field>
      </div>
      <div style={{ marginBottom: '15px' }}>
        <label htmlFor="password" style={{ display: 'block', marginBottom: '8px', fontWeight: 'bold' }}>Password:</label>
        <Field name="password" rules={[{ required: true, message: 'Please input your password!' }]}>
          <Input id="password" type="password" placeholder="Enter password" />
        </Field>
      </div>
      <div style={{ marginBottom: '20px', display: 'flex', alignItems: 'center' }}>
        <Field name="remember" valuePropName="checked" trigger="onChange">
          <input type="checkbox" id="remember" style={{ marginRight: '8px' }} />
        </Field>
        <label htmlFor="remember">Remember me</label>
      </div>
      <button type="submit" style={{ padding: '12px 25px', backgroundColor: '#007bff', color: 'white', border: 'none', borderRadius: '4px', cursor: 'pointer', fontSize: '16px' }}>
        Submit
      </button>
    </Form>
  );
};

export default DemoForm;

view raw JSON →