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

@blackbox-vision/rff-fields

v0.1.0

Published

RFF Custom Field components

Downloads

9

Readme

RFF Fields npm version License: MIT

React Final Form Custom Fieds

Table of contents

Use Case

You're working with react-final-form and you need common use cases like dependent fields, watch a field for changes, use prefix on fields.

Compatibility

Since this package uses hooks under the hood, you need at least a minimum React version of 16.8.0.

Installation

YARN

yarn add @blackbox-vision/rff-fields

NPM

npm install --save @blackbox-vision/rff-fields

Example Usages

ConditionalField

Use ConditionalField when you want to render fields given a condition:

import React from 'react';
import { Form, Field } from 'react-final-form';
import { ConditionalField } from '@blackbox-vision/rff-fields';

const MyForm = () => (
  <Form onSubmit={onSubmit}>
    {({ handleSubmit, form, submitting, pristine, values }) => (
      <form onSubmit={handleSubmit}>
        <div>
          <label>First Name</label>
          <Field
            name="firstName"
            component="input"
            type="text"
            placeholder="First Name"
          />
        </div>
        <div>
          <label>Transport</label>
          <div>
            <label>
              <Field
                name="reception"
                component="input"
                type="radio"
                value="delivery"
              />{' '}
              Delivery
            </label>
            <label>
              <Field
                name="reception"
                component="input"
                type="radio"
                value="pickup"
              />{' '}
              Pickup
            </label>
          </div>
        </div>
        <Condition when="reception" is="delivery">
          <div>
            <label>Street</label>
            <Field
              name="street"
              component="input"
              type="text"
              placeholder="Street Address"
            />
          </div>
        </Condition>
        <Condition when="reception" is="pickup">
          <div>
            <label>Pickup Time</label>
            <Field name="pickupTime" component="select">
              <option />$
              {pickupTimes.map(time => (
                <option key={time} value={time}>
                  {time}
                </option>
              ))}
            </Field>
          </div>
        </Condition>
        <div>
          <label>Is it a gift?</label>
          <Field name="gift" component="input" type="checkbox" />
        </div>
        <Condition when="gift" is={true}>
          <div>
            <label>Gift Message</label>
            <Field
              name="message"
              component="textarea"
              placeholder="Gift Message"
            />
          </div>
        </Condition>
        <div className="buttons">
          <button type="submit" disabled={submitting}>
            Submit
          </button>
        </div>
      </form>
    )}
  </Form>
);

export default MyForm;

DependentField

Use DependentField when you want to render fields that need to be render after getting its data requirements from another values:

import React from 'react';
import { Form, Field } from 'react-final-form';
import { DependentField } from '@blackbox-vision/rff-fields';

const getStatesForCountry = country => {...}
const countries = [...];

const MyForm = () => (
  <Form onSubmit={onSubmit}>
    {({ handleSubmit, form, submitting, pristine, values }) => (
      <form onSubmit={handleSubmit}>
        <Field name="country" component="select">
          {countries.map(country => (
            <option key={country} value={country}>
              {country}
            </option>
          ))}
        </Field>
        <DependentField
          onChange={async ({ country }) => await getStatesForCountry(country)}
        >
          {states => (
            <Field name="state" component="select">
              {states.map(state => (
                <option key={state} value={state}>
                  {state}
                </option>
              ))}
            </Field>
          )}
        </DependentField>
        <div className="buttons">
          <button type="submit" disabled={submitting}>
            Submit
          </button>
        </div>
      </form>
    )}
  </Form>
);

export default MyForm;

WhenFieldChanges

Use WhenFieldChanges to watch a specific field value and change other field value:

import React from 'react';
import { Form, Field } from 'react-final-form';
import { WhenFieldChanges } from '@blackbox-vision/rff-fields';

const MyForm = () => (
  <Form
    onSubmit={onSubmit}
    render={({ handleSubmit, form, submitting, pristine, values, errors }) => {
      return (
        <form onSubmit={handleSubmit}>
          <WhenFieldChanges
            field="gift"
            becomes={false}
            set="giftWrap"
            to={false}
          />
          <WhenFieldChanges
            field="gift"
            becomes={false}
            set="giftCardMessage"
            to={undefined}
          />
          <div>
            <label>Is this a gift?</label>
            <Field name="gift" component="input" type="checkbox" />
          </div>
          <div>
            <label>Gift wrap?</label>
            <Field
              name="giftWrap"
              component="input"
              type="checkbox"
              disabled={!values.gift}
            />
          </div>
          <div>
            <label>Message</label>
            <Field
              name="giftCardMessage"
              component="textarea"
              disabled={!values.gift}
              placeholder="What do you want the card to say?"
            />
          </div>
          <div className="buttons">
            <button type="submit" disabled={submitting}>
              Submit
            </button>
          </div>
        </form>
      );
    }}
  />
);

export default MyForm;

PrefixedField

Use PrefixedField when you want to render fields with a prefix:

import React from 'react';
import { Form, Field } from 'react-final-form';
import {
  PrefixedField,
  PrefixedFieldProvider,
} from '@blackbox-vision/rff-fields';

const MyForm = () => (
  <Form
    onSubmit={onSubmit}
    render={({ handleSubmit, form, submitting, pristine, values }) => (
      <form onSubmit={handleSubmit}>
        <h3>Shipping</h3>
        <PrefixedFieldProvider prefix="shipping">
          <div>
            <label>Street</label>
            <PrefixedField
              name="street"
              component="input"
              type="text"
              placeholder="Street"
            />
          </div>
          <div>
            <label>City</label>
            <PrefixedField
              name="city"
              component="input"
              type="text"
              placeholder="City"
            />
          </div>
        </PrefixedFieldProvider>
        <h3>Billing</h3>
        <PrefixedFieldProvider prefix="billing">
          <div>
            <label>Street</label>
            <PrefixedField
              name="street"
              component="input"
              type="text"
              placeholder="Street"
            />
          </div>
          <div>
            <label>City</label>
            <PrefixedField
              name="city"
              component="input"
              type="text"
              placeholder="City"
            />
          </div>
        </PrefixedFieldProvider>
        <div className="buttons">
          <button type="submit" disabled={submitting || pristine}>
            Submit
          </button>
        </div>
        <pre>{JSON.stringify(values, 0, 2)}</pre>
      </form>
    )}
  />
);

export default MyForm;

Component APIs

ConditionalField

The ConditionalField component has the following props:

| Properties | Types | Default Value | Description | | ---------- | ------ | ------------- | ---------------------------------------------------------------- | | when | string | - | Property that represents the name of the field to watch | | is | any | - | Property that represents the value needed to reach the condition |

DependentField

The DependentField component has the following props:

| Properties | Types | Default Value | Description | | ---------- | -------- | ------------- | ----------------------------------------------------------------------------- | | onChange | Function | - | Property that represents a function that let you track all form values change | | children | Function | - | Property that represents a children as a function that takes all form values |

WhenFieldChanges

The WhenFieldChanges component has the following props:

| Properties | Types | Default Value | Description | | ---------- | ------ | ------------- | ------------------------------------------------------------------------- | | field | string | - | Property that represents the name of the field to watch | | becomes | any | - | Property that represents the required value for the field we are watching | | set | string | - | Property that represents the name of the field to change its value | | to | any | - | Property that represents the value to set for the field |

Prefix

The Prefix Field API is composed by 2 components PrefixedFieldProvider and PrefixedField:

PrefixedFieldProvider

The PrefixedFieldProvider component has the following props:

| Properties | Types | Default Value | Description | | ---------- | ------ | ------------- | --------------------------------------------------------- | | prefix | string | - | Property that represents the prefix to use for the fields |

PrefixedField

The PrefixedField component has all the supported props from final-form Field component API.

Issues

Please, open an issue following one of the issues templates. We will do our best to fix them.

Contributing

If you want to contribute to this project see contributing for more information.

License

Distributed under the MIT license. See LICENSE for more information.