2.1.10 • Published 3 years ago

frr-form v2.1.10

Weekly downloads
1,079
License
-
Repository
github
Last release
3 years ago

Configurable React + TS Form

Motivation

It seems like 90% of a frontend developer's life is implementing forms. Forms, forms, forms. They're boring and styling them is a pain. You usually end up with way too much code for something that seems so boilerplate. For this reason, I've been using a configurable typesafe form instead. You provide the styling & layout once via a context hook and BOOM, you can crank out tons of forms throughout your app with just a configuration array. NO STYLING. NO JSX. Of course there are limitiations, but for your standard day-to-day forms, this library should be suitable.

Getting started:

Yarn
 yarn add frr-form
NPM
 yarn add frr-form

Example

import * as React from 'react'
import { Lens } from 'monocle-ts'
import { FormThemeContext, configureFormTheme } from 'frr-form/lib/theme/theme'
import { FormField, Form } from 'frr-form/lib/components/Form'
import { FormFieldType } from 'frr-form/lib/components/types'

type Person = {
  name: string
  hairColor: string
  age: number
  height: number
  description: string
  email: string
  website: string
}

const mkLens = Lens.fromPath<Person>()

export const personFormFields: Array<FormField<Person, any>> = [
  {
    type: FormFieldType.FormSection,
    title: 'Information',
    fields: [
      [
        {
          label: 'Name',
          type: FormFieldType.TextInput,
          lens: mkLens(['name']),
          required: true,
        },

        {
          label: 'Hair color',
          type: FormFieldType.TextInput,
          lens: mkLens(['hairColor']),
          required: true,
        },
      ],
      [
        {
          label: 'Age',
          type: FormFieldType.TextNumber,
          lens: mkLens(['age']),
          required: true,
        },
        {
          label: 'Height',
          type: FormFieldType.TextNumber,
          lens: mkLens(['height']),
          required: true,
        },
      ],
      [
        {
          label: 'Description',
          type: FormFieldType.TextInput,
          lens: mkLens(['description']),
          required: true,
        },
      ],
      [
        {
          label: 'Email',
          type: FormFieldType.TextInput,
          lens: mkLens(['email']),
          required: true,
        },
        {
          label: 'Website',
          type: FormFieldType.TextInput,
          lens: mkLens(['website']),
          required: true,
        },
      ],
    ],
  },
]

const personData = {
  name: 'Luke',
  hairColor: 'brown',
  age: 23,
  height: 194,
  description: 'average height',
  email: 'luke@google.com',
  website: 'www.foronered.com',
}

const FormTheme = configureFormTheme({})

export const App = () => (
  <FormThemeContext.Provider value={FormTheme}>
    <Form<Person>
      formFields={personFormFields}
      data={personData}
      onChange={(p) => {
        // add update function
      }}
    />
  </FormThemeContext.Provider>
)

Development

Build library

To build the library, run the build script.

  1. Install packages: yarn
  2. Build library with types: yarn build

Develop in watch-mode

Follow these steps to run the library build in watch mode using the local frr-web package:

  1. (Follow the instructions in the README of the frr-web package to install it, build it locally and create a symlink)
  2. Link the local frr-web package: yarn link-frr
  3. Build the types first: yarn build
  4. Start the build in watch mode (babel): yarn babel:watch (The script cleans the peerDependencies, links the frr-web package and starts the babel transpiler in watch mode)

To rebuild the types the following actions are required (for the why see IMPORTANT NOTE below):

  1. (Quit watch mode: ctrl c).
  2. Run: yarn build-types
  3. Start babel again: yarn babel:watch

Use package in linked (watch-)mode

You might want to link this library to the consuming application and keep it in watch mode to develop in parallel.

  • Create a symlink: yarn link (This you have to do only once)
  • Run build with babel: yarn babel:watch

IMPORTANT NOTE Types are not transpiled by Babel. As a consequence, changes of types require a rebuild of the types with the TypeScript compiler in order for consuming applications to receive them.

As the TypeScript compiler requires all dependencies including peerDependencies, we first have to install those as well. Unfortunately libraries like React or Style-Components cannot handle duplicate installations of the same package in one application and will crash in the browser during rendering.

That is why we have to clean the node_modules from all peerDependencies before using it. And that is also why we cannot really use the TypeScript compiler to develop in watch-mode with linked modules.

Pitfalls

Building types fails with the error message:

The inferred type of 'MainSectionWrapper' cannot be named without a reference to 'frr-web/node_modules/@types/styled-components'. This is likely not portable. A type annotation is necessary.

Solution: Do not export styled components directly from a file. The error above is thrown because of the following statement:

export const MainSectionWrapper = styled.div`
  flex-grow: 1;
`

Remove export and the compiler will pass

const MainSectionWrapper = styled.div`
  flex-grow: 1;
`
2.1.9

3 years ago

2.1.10

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.4

3 years ago

2.1.3

3 years ago

2.1.6

3 years ago

2.1.5

3 years ago

2.1.8

3 years ago

2.1.7

3 years ago

2.1.0

3 years ago

2.0.24

3 years ago

2.0.25

3 years ago

2.0.23

3 years ago

2.0.22

3 years ago

2.0.20

3 years ago

2.0.21

3 years ago

2.0.19

3 years ago

2.0.16

3 years ago

2.0.17

3 years ago

2.0.18

3 years ago

2.0.15

3 years ago

2.0.13

3 years ago

2.0.14

3 years ago

2.0.9

3 years ago

2.0.11

3 years ago

2.0.12

3 years ago

2.0.10

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.8

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.38

3 years ago

1.1.39

3 years ago

1.1.41

3 years ago

1.1.40

3 years ago

1.1.37

3 years ago

1.1.36

3 years ago

1.1.34

3 years ago

1.1.33

3 years ago

1.1.35

3 years ago

1.1.32

3 years ago

1.1.31

3 years ago

1.1.29

3 years ago

1.1.30

3 years ago

1.1.28

3 years ago

1.1.27

3 years ago

1.1.26

3 years ago

1.1.25

3 years ago

1.1.24

3 years ago

1.1.23

3 years ago

1.1.19

3 years ago

1.1.18

3 years ago

1.1.22

3 years ago

1.1.21

3 years ago

1.1.20

3 years ago

1.1.17

3 years ago

1.1.16

3 years ago

1.1.15

3 years ago

1.1.13

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.10

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.34

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.29

3 years ago

1.0.30

3 years ago

1.0.26

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.0

4 years ago