11import React , { Component , PropTypes } from 'react' ;
22import { isEmpty } from 'lodash' ;
3- import { Field , reduxForm } from 'redux-form' ;
4- import { Button , Form } from 'reactstrap' ;
3+ import { Field , FieldArray , reduxForm } from 'redux-form' ;
4+ import { Button , Form , FormFeedback , Col , Row } from 'reactstrap' ;
55
66import { InputField , TextArea , SelectField , required } from '../../forms' ;
77
8- class PostForm extends Component {
8+ export const PostFormParts = ( { fields, meta : { error } } ) => (
9+ < div className = "my-3" >
10+ { fields . map ( ( part , index ) =>
11+ < Row key = { index } >
12+ < Col xs = "10" >
13+ < Field
14+ name = { `${ part } .body` }
15+ component = { TextArea }
16+ rows = { 5 }
17+ />
18+ </ Col >
19+ < Col xs = "2" >
20+ < Button color = "danger" onClick = { ( ) => fields . remove ( index ) } > X</ Button >
21+ </ Col >
22+ </ Row >
23+ ) }
24+ { error && < FormFeedback > { error } </ FormFeedback > }
25+ < Button onClick = { ( ) => fields . push ( ) } > Add Part</ Button >
26+ </ div >
27+ ) ;
28+
29+ export class PostForm extends Component {
930 render ( ) {
1031 const { categories, handleSubmit, pristine, reset, submitting } = this . props ;
1132
@@ -18,7 +39,7 @@ class PostForm extends Component {
1839 } ) ) ) ;
1940
2041 return (
21- < Form onSubmit = { handleSubmit } >
42+ < Form onSubmit = { handleSubmit } className = "py-3" >
2243 < Field
2344 name = "title"
2445 label = "Title"
@@ -30,13 +51,11 @@ class PostForm extends Component {
3051 component = { SelectField }
3152 options = { categoriesOptions }
3253 />
33- < Field
34- name = "body"
35- label = "Body"
36- component = { TextArea }
37- rows = "10"
54+ < FieldArray
55+ name = "parts"
56+ component = { PostFormParts }
3857 />
39- < Button disabled = { pristine || submitting } color = "primary" > Submit</ Button >
58+ < Button disabled = { pristine || submitting } color = "primary" className = "mr-2" > Submit</ Button >
4059 < Button disabled = { pristine || submitting } onClick = { reset } > Undo Changes</ Button >
4160 </ Form >
4261 ) ;
@@ -47,7 +66,7 @@ const validate = (values) => {
4766 const errors = required ( values ,
4867 'title' ,
4968 'category.id' ,
50- 'body ' ,
69+ 'parts ' ,
5170 ) ;
5271 return errors ;
5372} ;
0 commit comments