Wednesday, June 13, 2018

Modelling Dynamic Forms as React Component

Leave a Comment

How to model dynamic forms as a React Component?

For example I want to create a form shown in an image below:

  1. How can I model this as a React component?
  2. How can I add dynamicity to that component? For example, clicking on "+ Add" button creates another empty textbox and puts it right below the other already rendered textboxes (as shown in an image below).

Can someone help me with the code for the Form below?

enter image description here

3 Answers

Answers 1

In tags I see redux so I can suggest redux-form. Here you have an example of dynamic forms with redux-form.

Answers 2

I can help you with a reduced way

      import React , {Component} from 'react'   import { connect }from 'react-redux'    class main extends Component{     render(){       return(         <div>                 <BaselineMath/>                 <Algorithms />             </div>       )     }   } const mapStateToProps = ({}) => {     return{}   }   export default connect (mapStateToProps,{})(main)        class BaselineMath extends Component{         constructor(props){             super(props);             this.state={rows:[1]}         }         _getRows{             return this.state.rows.map((res,key)=>{                 return <input placeholder="etc..."/>             })         }         onClickAdd(){             let rows = this.state.rows             rows.push(1)             this.setState({                 rows             })         }         render(){             return(             <div>                 <Button onClick={this.onClickAdd.bind(this)}>ADD row</Button>                 {this._getRows()}             </div>             )         }     }      export default (BaselineMath)       class Algorithms extends Component{         constructor(props){             super(props);             this.state={rows:[1]}         }         _getRows{             return this.state.rows.map((res,key)=>{                 return <input placeholder="etc..."/>             })         }         onClickAdd(){             let rows = this.state.rows             rows.push(1)             this.setState({                 rows             })         }         render(){             return(             <div>                 <Button onClick={this.onClickAdd.bind(this)}>ADD row</Button>                 {this._getRows()}             </div>             )         }     }      export default (Algorithms) 

you can do the algorithm with anything you want

Answers 3

The difference is in the fact, that beyond the state of form values, we also need to handle the state of form shape/structure.

If you render the inputs by traversing some state object, that is representing the shape of the form, than new input is just a new entry in this state object. You can easily add or remove input fields on the form by managing that state object. E.g. you can write something like this (pseudo react code):

// inputs state of math and algorithms const state = { math: [obj1, obj2], algorithms: [obj1, obj2] } // obj ~= {id, value} // render math inputs const mathMarkup = state.math.map(obj => <input value={obj.value} onChange={...} />) // add handler for math field const addMath = () => setState(prevState => ({ math: [...prevState.math, newObj]})) 

Here is the example of such form - codesandbox. It's not 100% as on your screen, but the idea should be understandable. Since there are some unclear requirements on your form, I implemented only first two sections, so you can grasp the idea. And, there are no styles :shrug:

Also, you can extract renderXyz methods to separate components, and improve state shape to meet your needs.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment