Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 018623b

Browse files
23 - Reusable React Input
1 parent 4582a38 commit 018623b

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

‎src/learn/FormsAndInputs.js‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
import React, { Component } from 'react'
22

3+
function MyTextInput(props){
4+
function handleChange(event){
5+
if (props.onChange) props.onChange(event)
6+
}
7+
return (
8+
<p>
9+
<input type='email' value={props.value} name={props.name} ref={props.inputRef} onChange={handleChange} />
10+
</p>
11+
)
12+
}
13+
314
class FormsAndInputs extends Component {
415
constructor(props){
516
super(props)
617
this.state = {
718
fullName: '',
8-
content: ''
19+
content: '',
20+
email: ''
921
}
1022
this.inputFullNameRef = React.createRef()
23+
this.inputEmailRef = React.createRef()
1124
}
1225

1326

@@ -30,7 +43,7 @@ class FormsAndInputs extends Component {
3043

3144
handleFocusClick = (event) => {
3245
event.preventDefault()
33-
this.inputContentRef.focus()
46+
this.inputEmailRef.current.focus()
3447
}
3548
handleClearClick = (event) => {
3649
event.preventDefault()
@@ -44,11 +57,13 @@ class FormsAndInputs extends Component {
4457
// }
4558
render () {
4659
const {fullName} = this.state
60+
const {email} = this.state
4761
return (
4862
<div>
4963
<h1>Forms and Inputs</h1>
5064
<p>Full name is: {fullName}</p>
5165
<form onSubmit={this.handleSubmit}>
66+
<MyTextInput inputRef={this.inputEmailRef} value={email} name='email' onChange={this.handleInputChange}/>
5267
<p><input ref={this.inputFullNameRef} type='text' placeholder='Your Name' value={fullName} name='fullName' onChange={this.handleInputChange} /></p>
5368
<p><textarea ref={node => this.inputContentRef = node} placeholder='Your message' name='content' required={true} onChange={this.handleInputChange}></textarea></p>
5469
<p><button>Send Message</button></p>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /