Giter Club home page Giter Club logo

redux-form-gen's Introduction

redux-form-gen

CircleCI build Codacy grade Codacy coverage NPM Version NPM Downloads styled with prettier

A pluggable form generator for redux-form.

✅ No dependency on styling frameworks (bootstrap, etc)

✅ Pluggable - Add your own custom field types

✅ Uses a plain JSON object to define the form - can be sent from the server

✅ Supports conditional logic using JSON

Installation

yarn add @isobar-us/redux-form-gen

or

npm install --save @isobar-us/redux-form-gen

Documentation

Examples

🏖 Code Sandboxes 🏖

Simple Usage

import {reduxForm, Form} from 'redux-form';
import {FormGenerator, injectGenProps} from '@isobar-us/redux-form-gen';

const fields = [
  {
    type: 'text',
    label: 'First Name',
    required: true,
    questionId: 'firstName'
  },
  {
    type: 'text',
    label: 'Last Name',
    required: true,
    questionId: 'lastName'
  }
];

// pass your fields into <FormGenerator />
const MyFields = ({handleSubmit}) => (
  <Form onSubmit={handleSubmit}>
    <FormGenerator fields={fields} />
    <button type='submit'>Submit</button>
  </Form>
);

// wrap reduxForm in injectGenProps to take care of validation and initialValues
const MyForm = injectGenProps(
  reduxForm({
    form: 'exampleForm'
  })(MyFields)
);

// make sure to pass fields into the form wrapped with injectGenProps()
const MyPage = () => (
  ...
  <MyForm fields={fields} />
);

Defining your own field types

import {reduxForm, Field, Form} from 'redux-form';
import {FormGenerator, GenericRequiredLabel, injectGenProps} from '@isobar-us/redux-form-gen';

const SelectField => () => {
  // ... your custom select field implementation
};

// defining your own field type definition.
const selectType = ({field}) => ({
  _genFieldComponent: Field,
  _genLabelComponent: GenericRequiredLabel,
  name: field.questionId,
  component: SelectField,
  options: field.options
});

// mapping the type string (key) to the type definition (value)
const customFieldTypes = {
  select: selectType
};

// using your new field type
const fields = [
  {
    type: 'select', // matches the key in `customFieldTypes`
    questionId: 'test',
    label: 'Who would win in a fight?',
    options: [
      {label: 'Horse-sized duck', value: 'horse-sized_duck'},
      {label: '100 duck-sized horses', value: '100_duck-sized_horses'}
    ]
  }
]

// pass your fields and customFieldTypes into <FormGenerator />
const MyFields = ({handleSubmit}) => (
  <Form onSubmit={handleSubmit}>
    <FormGenerator fields={fields} customFieldTypes={customFieldTypes} />
    <button type='submit'>Submit</button>
  </Form>
);

// wrap reduxForm in injectGenProps to take care of validation and initialValues
const MyForm = injectGenProps(
  reduxForm({
    form: 'myForm'
  })(MyFields)
);

// make sure to pass fields and customFieldTypes into the form wrapped with injectGenProps()
const MyPage = () => (
  ...
  <MyForm fields={fields} customFieldTypes={customFieldTypes} />
);

Default Field Types

GenericProps

  • type: string - the type of the field. you can add more type using customFieldTypes prop on the <FormGenerator />.
  • label: string - the label for the field
  • childFields: [FieldType] - an array of child fields. If the parent field is invisible, childFields will also be invisible. useful for the section and group types.
  • conditionalVisible: ConditionalObject - the evaluated ConditionalObject controls whether a field and it's childFields are visible

GenericFieldProps

Extends GenericProps

  • questionId: - the name property for a field. supports dot-notation
  • required: boolean - mark the field as required
  • disabled: boolean - mark the field as disabled (also skips required validation)
  • conditionalRequired: ConditionalObject - the evaluated ConditionalObject controls whether a field is required
  • conditionalDisabled: ConditionalObject - the evaluated ConditionalObject controls whether a field is disabled (also skips required validation)

Type: text

Extends GenericFieldProps. Renders a native <input type="text" /> component.

Type: textarea

Extends GenericFieldProps. Renders a native <textarea> component.

Type: radio

Extends GenericFieldProps. Renders a native <input type="radio" /> component.

Type: select

Extends GenericFieldProps. Renders a native <select> and <option> component.

  • options: [ { label: string, value: string } ] - an array of <option>s to render.

Type: array

Extends GenericFieldProps. Uses ReduxForm FieldArray component, and renders each item, as an arrayItem type.

  • item: (FieldType: arrayItem) - the arrayItem type that the array will use to render each item.
  • addLabel - the label for the Add button for adding a new item to the array.

Type: arrayItem

Extends GenericProps

  • label: string - supports templates for {index} and {indexOverTotal} ex: label: "Item {index}"

Type: group

Extends GenericProps. Renders a extra label for grouping fields.

Type: section

Extends GenericProps. Renders a header for grouping fields.

Custom Field Type Options

Property Type Description
_genFieldComponent Component This is the redux-form Field, Fields, or FieldArray component that this should use to render
_genComponent Component the Component used if not using _genFieldComponent
_genLabelComponent Component the Component used to render the field label. defaults to GenericRequiredLabel
_genChildren array used to override the default childFields when iterating only (not rendering)
_genDefaultValue any used when calculating the initialValues with getDefaultValues() for a reduxForm component
_genIsFilled func fn({data, field, lookupTable, customFieldTypes}) => bool
_genIsValid func fn({data, field, lookupTable, customFieldTypes}) => bool
_genSectionErrors func fn({errors, data, field, lookupTable, customFieldTypes}) => void set in errors
_genTraverseChildren func fn({iterator, data, lookupTable}) => something.map((field) => iterator({field, data, lookupTable}))
_genSkipChildren bool skip rendering of childFields
_genSkipCache bool skip clear/restore functionality from FormGenerator cache
_genHidden bool skip rendering of this field and all it's children.
... ... ...
any other props for <Field> component any name, names, component etc...

Note: Any props with the _gen prefix are omitted when rendering the _genFieldComponent

Known Bugs

  • built in SelectField can only take strings as option values, since they get converted to strings on the <option> tag

redux-form-gen's People

Contributors

bmv437 avatar gschwa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

redux-form-gen's Issues

lookupTable not getting passed into evalCond in GenCondEval

Are you submitting a bug report or a feature request?

Bug

What is the current behavior?

Currently if conditional object refers to a remote field object and that field type has a _genIsFilled defined, there is no way to lookup that field's type since the lookupTable isn't being passed into evalCond() inside GenCondEval.

What is the expected behavior?

When a conditional object that refers to remote field is evaluated, and that remote field defines a _genIsFilled, that _genIsFilled function is used to evaluate the filled state of the remote field.

Sandbox Link

Example of the broken functionality:
https://codesandbox.io/s/8nwykm65ll

What's your environment?

v0.9.1

Custom Array Form

Hi, I am trying to make a custom array and arrayItem, because in my work I am required to use adminlte and I wanted to try this library, which forced me to make all the custom components, but I can not understand how to make an Array and an Array item, I have days and I do not have a clear idea. Might you help me? more specifically with GenField

isSectionValid function should be renamed to getSectionErrors

Are you submitting a bug report or a feature request?

Bug report

What is the current behavior?

isSectionValid function returns an object.

What is the expected behavior?

Naming convention dictates that anything starting with is should return a boolean. Should instead be named getSectionErrors to better represent what it returns.

Sandbox Link

What's your environment?

redux-form-gen: 0.7.2

Other information

Conditional Value is possible?

Hi, I would like to be able to make value changes, for example in the select that depends on the response of another value. As a conditionalValue, it listens to changes in another value and performs a filter on its value. it is possible, or in some way listen to the referenced value to perform the filter.
I was thinking of reading the value according to the conditional and perform the filter within each select, but it would mean connecting each select to redux, is there any way to get the values without doing this?
hope you can help me. thanks for u time

Create a distinction between childFields and innerFields

Are you submitting a bug report or a feature request?

Idea for a new feature

What is the current behavior?

Currently the generator can't tell the difference between childFields and innerFields.

  • Some fields are comprised of multiple innerFields, which is different than the existing parent <-> childFields relationship
  • innerFields are part of the current field, while childFields are not.
  • A shallow validation should validate innerFields and not childFields
  • A deep validation should validate innerFields in addition to childFields
  • We currently use the fields prop in combination with _genChildren to talk about innerFields. An example would be the dateUnknown field.

This comes into play with the new mapFieldChildren() function which was introduced in #13
For validation iterators (ex: isSectionValidIterator()) that are executed with deep: false, it will skip innerFields (which is currently achieved with _genChildren).

Should we support innerFields as first class citizens in the generator?

DateOrUnknownField fails on render

Are you submitting a bug report or a feature request?

Bug Report

What is the current behavior?

Custom DateOrUnknownField.js fails on render when you go to Storybook > All Field Types

What is the expected behavior?

Custom DateOrUnknownField.js should be rendered without errors.

Sandbox Link

Not able to create a sandbox because this bug occurs in un-released code.

What's your environment?

Un-released code.
Was able to reproduce on commit dd47355

Other information

Uncaught TypeError: Cannot read property 'fields' of undefined

const {_field: {fields}} = this.props;
line 15 in DateOrUnknownField.js

Is this the right way ?

Hello, I am trying to implement custom components for material ui and redux form.

Here is a sample where I was wondering if you could see if something from redux-form-gen was wrongly used.
I commented the parts where I have questions but if you see something that could have been done cleaner I would be really enthusiast to hear it.
https://codesandbox.io/s/yj6x97x99v

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.