Skip to content

Commit 1cfb591

Browse files
18 - Clear Form
1 parent a101fe9 commit 1cfb591

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/reactify-ui/src/posts/PostCreate.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ class PostCreate extends Component {
77
super(props)
88
this.handleSubmit = this.handleSubmit.bind(this)
99
this.handleInputChange = this.handleInputChange.bind(this)
10+
this.clearForm = this.clearForm.bind(this)
11+
this.postTitleRef = React.createRef()
12+
this.postContentRef = React.createRef()
1013
this.state = {
1114
draft: false,
1215
title: null,
@@ -38,6 +41,7 @@ class PostCreate extends Component {
3841
if (thisComp.props.newPostItemCreated){
3942
thisComp.props.newPostItemCreated(responseData)
4043
}
44+
thisComp.clearForm()
4145
}).catch(function(error){
4246
console.log("error", error)
4347
alert("An error occured, please try again later.")
@@ -74,25 +78,48 @@ class PostCreate extends Component {
7478
})
7579
}
7680

81+
clearForm(event){
82+
if (event){
83+
event.preventDefault()
84+
}
85+
this.postCreateForm.reset()
86+
}
87+
88+
89+
clearFormRefs(){
90+
this.postTitleRef.current=''
91+
this.postContentRef.current=''
92+
}
93+
94+
7795
componentDidMount(){
7896
this.setState({
7997
draft: false,
8098
title: null,
8199
content: null,
82100
publish: null,
83101
})
102+
this.postTitleRef.current.focus()
84103
}
85104

86105
render(){
87106
return (
88-
<form onSubmit={this.handleSubmit}>
107+
<form onSubmit={this.handleSubmit} ref={(el) => this.postCreateForm = el}>
89108
<div className='form-group'>
90109
<label for='title'>Post title</label>
91-
<input type='text' id='title' name='title' className='form-control' placeholder='Blog post title' onChange={this.handleInputChange} required='required'/>
110+
<input
111+
type='text'
112+
id='title'
113+
name='title'
114+
className='form-control'
115+
placeholder='Blog post title'
116+
ref = {this.postTitleRef}
117+
onChange={this.handleInputChange}
118+
required='required'/>
92119
</div>
93120
<div className='form-group'>
94121
<label for='content'>Content</label>
95-
<textarea id='content' name='content' className='form-control' placeholder='Post content' onChange={this.handleInputChange} required='required'/>
122+
<textarea id='content' ref = {this.postContentRef} name='content' className='form-control' placeholder='Post content' onChange={this.handleInputChange} required='required'/>
96123

97124
</div>
98125
<div className='form-group'>
@@ -105,6 +132,7 @@ class PostCreate extends Component {
105132
<input type='date' id='publish' name='publish' className='form-control' onChange={this.handleInputChange} required='required'/>
106133
</div>
107134
<button className='btn btn-primary'>Save</button>
135+
<button className='btn btn-secondary' onClick={this.clearForm}>Cancel</button>
108136
</form>
109137
)
110138
}

src/staticfiles/js/reactify-django.ui.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)