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

rc-form-pure

v5.0.2

Published

Declarative forms for React

Downloads

47

Readme

rc-form-pure

Performant, flexible, lightweight and abstract library for creating React forms

Features

  • The convenience of handling dependent fields
  • Configuring fields at the FormItem level, as well as at the level FormBuilder via extraFieldsProps
  • Out of the box, FormItem is a pure component (better performance for large forms)
  • Independent from state managers
  • Can be used in React Native
  • Small size
  • Zero dependencies
  • Written on the Typescripts

Bundle size (and comparison with similar other) - https://bundlephobia.com/result?p=rc-form-pure

Demo

Install

npm i rc-form-pure

Usage

Example

import React from 'react';
import { FormBuilder, FormItem, Validators, ComponentPropTypes, IFieldsToSubmit } from 'rc-form-pure';

const TextField: ComponentPropTypes<{}> = props => {
  const { name, onChange, error, value } = props;

  return (
    <>
      <label>{name}</label>
      <input value={value} onChange={e => onChange(e.target.value)} />
      {error}
    </>
  );
};

const TestFrom = () => {
  const onSubmit = (formData: IFieldsToSubmit, fieldWithError: IFieldsToSubmit | null) => {
    console.log('onSubmit', formData, fieldWithError);
  };

  return (
    <FormBuilder onSubmit={onSubmit} withForm>
      <FormItem name="country" component={TextField} />

      <FormItem
        name="required-field"
        validate={Validators.required}
        errorMessage={'Please fill this field'}
        component={TextField}
      />

      <button>onSubmit</button>
    </FormBuilder>
  );
};

export default TestFrom;

FormBuilder Props

type FormBuilderPropTypes = {
  // Object schema: { currency: 'Your error text' }
  errors?: Record<string, string | null>;

  // Determines if the form tag will be in the DOM
  withForm?: boolean;

  // You can specify a function that returns a custom wrapper of form
  renderForm?: Function;

  // Submit function will get values and errors
  onSubmit: (values: IFieldsToSubmit, fieldsWithError: FieldsWithErrorType) => Promise<any> | void;

  // The top level listener for fields changes Function(allFields, updatedFields) or { [nameField]: (specificfield, allFields) => {}
  onChangeFields?: OnChangeFieldsType;

  // Validate after onBlur or after every change can be overridden in the FormItem props
  validateOnBlur?: boolean;

  // initial values
  initialValues?: Record<FieldNameType, any>;

  // extra props will be mixed with service IField properties
  extraFieldsProps?: Record<FieldNameType, any>;
  or;
  // Pass the base config to all fields
  extraFieldsProps?: Record<'$all', any>;
};

FormItem Props

type FormItemTypes = {
  // The field name in the FormBuilder state
  name: string;

  // Your component
  component?: ComponentPropTypes<any>;

  // The formatter will be called before being saved to the FormBuilder state
  formatter?: (value: any) => any;

  // Override global validateOnBlur for some field
  validateOnBlur?: boolean;

  // function type of (value: any, message: string | string[]) => null | string | string[]
  validate?: ValidateType;

  // The error message that will appear when validation fails
  errorMessage?: ErrorMessageType;

  // Override global initialValue
  initialValue?: any;
};

There are several built-in Validators for convenience

FormItem nested fields

You can nest your fields by easily wrapping FromItem components in each other

<FormItem name="my-nested-group">
  <FormItem name="age" component={TextField} />
  <FormItem name="country" component={TextField} />
</FormItem>

The FormBuilder state will have the following properties:

my - nested - group.age;
my - nested - group.country;

API

useFormApi

import { useFormApi } from 'rc-form-pure';

const {
  setFields,
  setFieldsValue,
  getFieldsValue,
  useWatchFields,
  useWatchValue,
  useInitialValues,
  useSubmit,
} = useFormApi();

// also you can import each hook independently (without useFormApi)
import { useWatchFields, useWatchValue, useInitialValues, useSubmit } from 'rc-form-pure';

setFieldsValue

(updates: Record<string, any>, options: { changeEvent: boolean }) => void;
// changeEvent option has "true" value by default
// setting a new value:
setFieldsValue({ [FIELDS_CONFIG.currency.key]: '$' });

setFields

(updates: Record<string, Partial<IField>>, options: { changeEvent: boolean }) => void;
// changeEvent option has "true" value by default
// resetting value and error:
setFields({
  [FIELDS_CONFIG.currency.key]: { value: '', error: null },
});

getFieldsValue

(fieldKey?: string) => IFieldsToSubmit | PickPropType<IField, 'value'>

// get all values:
const formFields: IFieldsToSubmit = getFieldsValue();

// get the value of a specific field:
const formFields: any = getFieldsValue(FIELDS_CONFIG.currency.key);

useWatchValue

(fieldKey?: string) => Record<string, any> | any

// watch a specific field value
const countryValue = useWatchValue('country');

// watch a specific field value with a default value
const countryValue = useWatchValue('country', 'Russia');

// watch all values
const allValues = useWatchValue();

useInitialValues

(fieldKey?: string) => any;

// get a specific field initial value from FormBuilder
const countryInitialValue = useInitialValues('country');

// for example, you can use with useWatchValue
const countryValue = useWatchValue('country', countryInitialValue);

useWatchFields

(fieldKey?: string) => Array<specificField, allFields> | Array<allFields, updatedFields>

// invoked when a specific field changes
const [countryField, allFields] = useWatchFields('country');

// invoked on any fields changes
const [allFields, updatedFields] = useWatchFields();

useSubmit

(event?: any) => void

const { onSubmit } = useSubmit();

// call it when you want, and it will trigger onSubmit event on the FormBuilder
onSubmit();

formRef

You can get the form external API on the form level via useRef

const formRef = useRef<FormBuilder>(null);

formRef.current?.setFields;
formRef.current?.setFieldsValue;
formRef.current?.getFields;
formRef.current?.getFieldsValue;

Types

ComponentPropTypes

Extend your component prop types

type MyExtraPropTypes = {
  extraProps: boolean;
  disabled: boolean;
  globalFormReadonly: boolean;
};
const TextField: ComponentPropTypes<MyExtraPropTypes> = props => {
  const { name, error, onChange, disabled, globalFormReadonly, value } = props;

  return (
    <div>
      <label>{name}</label>
      <input disabled={disabled || globalFormReadonly} value={value} onChange={e => onChange(e.target.value)} />
      {error}
    </div>
  );
};

ErrorsType

FormBuilder "errors" prop type

const serverErrors: ErrorsType = {
  firstName: 'asd',
};

const [errors, setServerErrors] = useState<ErrorsType>(null);

IFieldsToSubmit

FormBuilder value types

const onSubmit = (formData: IFieldsToSubmit, fieldWithError: IFieldsToSubmit | null) => {
  console.log('onSubmit', formData, fieldWithError);
};

OnChangeFieldsType

FormBuilder "onChangeFields" prop type

const onChangeFields: OnChangeFieldsType = (allFields, updatedFields) => {
  console.log('onChangeFields', allFields, updatedFields);
};

const onChangeFields: OnChangeFieldsType = {
  country: (field, allFields) => {
    console.log('onChangeFields', field, allFields);
  },
};

Built-in Validators

| type | Description | Input type | Default | | ----------------- | ---------------------------------------------- | ---------- | ------- | | required | check that value exist | | - | | email | check valid emails | string | - | | number | check value type | any | - | | len | validate an exact length of a field | number | - | | min | validate a min number | number | - | | composeValidators | compose your validators (custom and built-ins) | function[] | - |