-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInput.js
102 lines (92 loc) · 2.32 KB
/
Input.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import React, { Component } from 'react'
import { connect } from 'react-redux'
import TextField from 'material-ui/TextField'
import RaisedButton from 'material-ui/RaisedButton'
import SnackBar from 'material-ui/SnackBar'
import Chip from 'material-ui/Chip'
import { submitNumber } from './actions'
const mapStateToProps = ({ inputed ,round, maxround}) => ({
inputed,round, maxround
})
class Input extends Component {
constructor(props) {
super(props)
this.state = {
value: '',
isValid: false,
snack: false
}
}
closeSnack() {
this.setState({
snack: false
})
}
handleChange(event) {
const value = event.target.value
const numValue = parseInt(value,10)
const isValid = (!isNaN(value) && (value.indexOf('.') == -1) && 0 <= numValue && numValue <= 100)
this.setState({
value,
isValid
})
}
handleClick() {
const { dispatch } = this.props
const { value } = this.state
this.setState({
value: '',
isValid: false,
snack: true
})
dispatch(submitNumber(parseInt(value, 10)))
}
handleKeyDown(event) {
const { isValid } = this.state
const { inputed ,round} = this.props
if (isValid && !inputed && (event.key === "Enter" || event.keyCode === 13)) { // Enter
this.handleClick()
}
}
render() {
const { value, snack, isValid} = this.state
const { inputed ,round, maxround} = this.props
console.log(value)
return(
<div>
<Chip
style = {{
float:"right"
}}
>
{round + "/" + maxround +"ラウンド"}
</Chip>
<p>投票する数字を入力してください</p>
<TextField
autoFocus
hintText="0~100までの整数を入力してください"
value = {value}
onChange={this.handleChange.bind(this)}
onKeyDown={this.handleKeyDown.bind(this)}
/>
<RaisedButton
primary={true}
label={
inputed
? "送信済"
: "送信"
}
disabled={inputed || !isValid}
onClick = {this.handleClick.bind(this)}
/>
{/*<SnackBar
open={snack}
message={"送信しました。"}
autoHideDuration={3000}
onRequestClose={this.closeSnack.bind(this)}
/>*/}
</div>
)
}
}
export default connect(mapStateToProps)(Input)