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 { connect } from 'react-redux' ;
4+ import { Field , FieldArray , formValueSelector , reduxForm } from 'redux-form' ;
5+ import { Button , Form , FormFeedback , Col , Row } from 'reactstrap' ;
56
67import { InputField , TextArea , SelectField , required } from '../../forms' ;
78
8- class PostForm extends Component {
9+ export const PostFormPart = ( { values, part} ) => {
10+ switch ( values . type ) {
11+ case 'image' :
12+ return (
13+ < div >
14+ < img src = { values . imageUrl } />
15+ < div > { values . description } </ div >
16+ < Field
17+ name = { `${ part } .imageUrl` }
18+ placeholder = { 'Image url...' }
19+ component = { InputField }
20+ /> < Field
21+ name = { `${ part } .description` }
22+ placeholder = { 'Image description...' }
23+ component = { InputField }
24+ />
25+ </ div >
26+ ) ;
27+ case 'text' :
28+ return (
29+ < Field
30+ name = { `${ part } .text` }
31+ placeholder = { 'Text...' }
32+ rows = { 5 }
33+ component = { TextArea }
34+ />
35+ ) ;
36+ } ;
37+ } ;
38+
39+ export const PostFormParts = ( { parts, fields, meta : { error } } ) => (
40+ < div className = "my-3" >
41+ { fields . map ( ( part , index ) =>
42+ < div >
43+ < hr />
44+ < Row key = { index } >
45+ < Col xs = "10" >
46+ < PostFormPart values = { parts [ index ] } part = { part } />
47+ </ Col >
48+ < Col xs = "2" >
49+ < Button color = "danger" onClick = { ( ) => fields . remove ( index ) } > X</ Button >
50+ </ Col >
51+ </ Row >
52+ </ div >
53+ ) }
54+ { error && < FormFeedback > { error } </ FormFeedback > }
55+ < Button onClick = { ( ) => fields . push ( { type : 'text' } ) } className = "mr-2" > Add Text</ Button >
56+ < Button onClick = { ( ) => fields . push ( { type : 'image' } ) } > Add Image</ Button >
57+ </ div >
58+ ) ;
59+
60+ export class PostForm extends Component {
961 render ( ) {
10- const { categories, handleSubmit, pristine, reset, submitting } = this . props ;
62+ const { parts , categories, handleSubmit, pristine, reset, submitting } = this . props ;
1163
1264 const categoriesOptions = [ {
1365 id : '' ,
@@ -18,7 +70,7 @@ class PostForm extends Component {
1870 } ) ) ) ;
1971
2072 return (
21- < Form onSubmit = { handleSubmit } >
73+ < Form onSubmit = { handleSubmit } className = "py-3" >
2274 < Field
2375 name = "title"
2476 label = "Title"
@@ -30,13 +82,12 @@ class PostForm extends Component {
3082 component = { SelectField }
3183 options = { categoriesOptions }
3284 />
33- < Field
34- name = "body"
35- label = "Body"
36- component = { TextArea }
37- rows = "10"
85+ < FieldArray
86+ name = "parts"
87+ parts = { parts }
88+ component = { PostFormParts }
3889 />
39- < Button disabled = { pristine || submitting } color = "primary" > Submit</ Button >
90+ < Button disabled = { pristine || submitting } color = "primary" className = "mr-2" > Submit</ Button >
4091 < Button disabled = { pristine || submitting } onClick = { reset } > Undo Changes</ Button >
4192 </ Form >
4293 ) ;
@@ -47,13 +98,20 @@ const validate = (values) => {
4798 const errors = required ( values ,
4899 'title' ,
49100 'category.id' ,
50- 'body' ,
51101 ) ;
52102 return errors ;
53103} ;
54104
105+ const selector = formValueSelector ( 'post' ) ;
106+
107+ export const mapStateToProps = ( state ) => ( {
108+ parts : selector ( state , 'parts' ) ,
109+ } ) ;
110+
55111export default reduxForm ( {
56112 enableReinitialize : true ,
57113 form : 'post' ,
58114 validate,
59- } ) ( PostForm ) ;
115+ } ) (
116+ connect ( mapStateToProps ) ( PostForm )
117+ ) ;
0 commit comments