Git Product home page Git Product logo

react-fieldset's Introduction

React Fieldset

Declarative field naming and props providing in complex tree-data forms.


What & Why

Naming and providing props to Form components may be quite tedious for large and deeply nested data. Also it's difficult to maintain, if your form changes frequently.

It would be great to use a <Fieldset/> component that will:

  • Simulate data tree and deal with field naming
  • Provide context to child components (Field, ErrorMessage, Custom Components, etc.)
  • Make code easier
  • Not be attached to specific form library, and can be used with different libraries

Make sure you need it!

Fieldset uses react contex and this can affect performance, so make sure your data and fields tree is really so complex that you need a <Fieldset/>. Anyway, try not to use it at first.

✅   You may need it if:

  • You really need a declarative way to create complex forms with nested data
  • You need to speed up development. For example, to build MVP
  • The layout of your form is complex and the fields may not be direct children of the parent and you need to provide them with props

🚫   You don't need it if:

  • Using render-prop pattern can solve your nesting
  • You know that your form usually does not change. In this case, it is better to build it once without <Fieldset/>
  • Your data and layout are as simple as the code block examples in this readme

⚡  Also, since <Fieldset/> provides its context for all connected child components, so avoid passing not memoized functions and objects to Fieldset props whenever possible. <Fieldset/> using React.memo for memoize context, and this will cause each of connected child components to be re-rendered every time Form's context changes.


Usage

import Fieldset, { withFieldset, useFieldset } from 'react-fieldset'

All code sections are written in Formik style, but Fieldset isn't really attached with it and can be used with different libraries.


Form data layering

It means to accumulate name-prefix for nested fields in a form data. For example:

//This code
<Field name="social.facebook" />
<Field name="social.twitter" />

//May look like this
<Fieldset name="social">
  <Field name="facebook" />
  <Field name="twitter" />
</Fieldset>

It also works fine to Arrays and Deep nested fields:

//Array
friends.map((friend, index) => (
<Fieldset key={index} name={`friends.${index}`}>
  <Field name="name"/>
  <Field name="age"/>
</Fieldset>

//Deep nested
<Fieldset name='level1'>
  <Field name='nested-value'/>
  <Fieldset name='level2'>
    <Field name='deep-nested-value'/>
  </Fieldset>
</Fieldset>

It also works fine with ErrorMessage, FastField, or any Custom Components - just connect them withFieldset() or using useFieldset. You can find more information about this below.


Providing props to the form components

All props (except name) will be provided directly to connected component. Also, feel free to use Fieldset both with or without name prop. So, for example, Fieldset can set "required" for all Fields components in one line:

//This code
<Field name="facebook" required={true}/>
<Field name="twitter" required={true}/>

//May look like this
<Fieldset required={true}>
  <Field name="facebook"/>
  <Field name="twitter"/>
</Fieldset>

Examples

Easy Array

This is a small illustration that shows how you can use Fieldset to create your own custom components like EasyArray, which can greatly simplify your code. Let's imagine that he needs to display this data structure:

const data = {
  array: [
    {
      id: 'firstNestedArray',
      nestedArray: [
        { id: 1, foo: 1, boo: 2 },
        { id: 2, foo: 3, boo: 4 },
      ],
    },
    {
      id: 'secondNestedArray',
      nestedArray: [
        { id: 3, foo: 5, boo: 6 },
        { id: 4, foo: 7, boo: 8 },
      ],
    },
  ],
}

Commonly this can be done with:

const UsualForm = ({ values }) => (
  <Form>
    {values.array.map((item, index) =>
      item.nestedArray.map((nestedItem, nestedIndex) => (
        <Fragment key={nestedItem.id}>
          <Field name={`array[${index}]nestedArray[${nestedIndex}].foo`} />
          <Field name={`array[${index}]nestedArray[${nestedIndex}].boo`} />
          <br />
        </Fragment>
      ))
    )}
  </Form>
)

But with EasyArray its looks more simpler:

const FormWithEasyArray = () => (
  <Form>
    <EasyArray name='array'>
      <EasyArray name='nestedArray'>
        <Field name='foo' />
        <Field name='boo' />
      </EasyArray>
    </EasyArray>
  </Form>
)

As working on EasyArray is also quite simple:

import Fieldset, { withFieldset, useFieldset } from 'react-fieldset'

const EasyArray = ({ name, keyProp = 'id', children, ...props }) => {
  const { fullName, fieldsetProps } = useFieldset(name)
  const [{ value }] = useField(fullName)

  return value.map((item, index) => (
    <Fieldset {...fieldsetProps} {...props} key={item[keyProp]} name={`${name}[${index}]`}>
      {children}
    </Fieldset>
  ))
}

export default EasyArray

// Don't forget to connect your Fields
export const Field = withFieldset(_Field)

Connection

To use <Fieldset/> you need to connect all components witch need to receive context. If you use <Field component={CustomInputComponent}> you need to connect <Field>, not <CustomInputComponent>.

You can connect components using withFieldset:

import { withFieldset } from 'react-fieldset'
export Field = withFieldset(_Field)
export ErrorMessage = withFieldset(_ErrorMessage)

Or, you can connect component using useFieldset hook:

import { useFieldset } from 'react-fieldset'

export const Field = props => {
  const { fullName, fieldsetProps, contextName } = useFieldset(props.name)
  return <_Field {...fieldsetProps} {...props} name={fullName} />
}

react-fieldset's People

Contributors

realiarthur avatar

Stargazers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.