@@ -41,12 +41,13 @@ class NewPost extends React.Component {
4141 constructor ( props ) {
4242 super ( props )
4343 this . state = { editorState : EditorState . createEmpty ( ) , expandedEditor : false , canSubmit : false }
44+ this . onTitleChange = this . onTitleChange . bind ( this )
4445 this . onEditorChange = this . onEditorChange . bind ( this )
4546 this . handleKeyCommand = this . handleKeyCommand . bind ( this )
4647 this . toggleBlockType = this . toggleBlockType . bind ( this )
4748 this . toggleInlineStyle = this . toggleInlineStyle . bind ( this )
4849 this . onClickOutside = this . onClickOutside . bind ( this )
49- this . onNewPostChange = this . onNewPostChange . bind ( this )
50+ this . validateSubmitState = this . validateSubmitState . bind ( this )
5051 }
5152
5253 componentDidMount ( ) {
@@ -59,11 +60,11 @@ class NewPost extends React.Component {
5960 }
6061
6162 componentWillReceiveProps ( nextProps ) {
62- if ( ! ( nextProps . isCreating || nextProps . hasError && ! nextProps . isCreating ) ) {
63+ if ( nextProps . isCreating !== this . props . isCreating && ! nextProps . isCreating && ! nextProps . hasError ) {
6364 this . setState ( { editorState : EditorState . createEmpty ( ) } )
6465 this . refs . title . value = ''
6566 }
66- this . onNewPostChange ( )
67+ this . validateSubmitState ( )
6768 }
6869
6970 onClickOutside ( evt ) {
@@ -125,15 +126,29 @@ class NewPost extends React.Component {
125126
126127 onEditorChange ( editorState ) {
127128 this . setState ( { editorState} )
128- this . onNewPostChange ( )
129+ this . validateSubmitState ( )
130+ if ( this . props . onNewPostChange ) {
131+ // NOTE: uses getPlainText method to avoid newline character for empty content
132+ this . props . onNewPostChange ( this . refs . title . value , editorState . getCurrentContent ( ) . getPlainText ( ) )
133+ }
129134 }
130135
131- onNewPostChange ( ) {
136+ validateSubmitState ( ) {
137+ const { editorState } = this . state
132138 this . setState ( {
133- canSubmit : this . refs . title && ! ! this . refs . title . value . trim ( ) . length && this . state . editorState . getCurrentContent ( ) . hasText ( )
139+ canSubmit : this . refs . title && ! ! this . refs . title . value . trim ( ) . length && editorState . getCurrentContent ( ) . hasText ( )
134140 } )
135141 }
136142
143+ onTitleChange ( ) {
144+ const { editorState } = this . state
145+ this . validateSubmitState ( )
146+ if ( this . props . onNewPostChange ) {
147+ // NOTE: uses getPlainText method to avoid newline character for empty content
148+ this . props . onNewPostChange ( this . refs . title . value , editorState . getCurrentContent ( ) . getPlainText ( ) )
149+ }
150+ }
151+
137152 render ( ) {
138153 const { currentUser, titlePlaceholder, isCreating} = this . props
139154 const { editorState, canSubmit} = this . state
@@ -191,7 +206,7 @@ class NewPost extends React.Component {
191206 ref = "title"
192207 className = "new-post-title"
193208 type = "text"
194- onChange = { this . onNewPostChange }
209+ onChange = { this . onTitleChange }
195210 placeholder = { titlePlaceholder || 'Title of the post' }
196211 />
197212 < div className = "draftjs-editor tc-textarea" >
0 commit comments