-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter12.html
More file actions
90 lines (86 loc) · 2.06 KB
/
Chapter12.html
File metadata and controls
90 lines (86 loc) · 2.06 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Chapter 12 Refs!</title>
<script src="https://unpkg.com/react@15.6.1/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15.6.1/dist/react-dom.js"></script>
<style>
#container {
padding: 50px;
background-color: #FFF;
}
.colorSquare {
height: 242px;
width: 242px;
margin-bottom: 15px;
box-shadow: 0px 0px 25px 0px #333;
}
.colorArea input {
padding: 10px;
font-size: 16px;
border: 2px solid #CCC;
}
.colorArea button {
padding: 10px;
font-size: 16px;
margin: 10px;
background-color: #666;
color: #FFF;
border: 2px solid #666;
}
.colorArea button:hover {
background-color: #111;
border-color: #111;
cursor: pointer;
}
.colorArea p {
font-family: sans-serif;
font-size: 16px;
height: 16px;
}
</style>
</head>
<body>
<div id="container"></div>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<!-- Your custom script here -->
<script type="text/babel">
class Colorizer extends React.Component {
constructor(props) {
super(props);
this.state = {bgColor: ''};
}
render() {
const squareStyle = {
backgroundColor: this.state.bgColor
};
return <div className="colorArea">
<div style={squareStyle} className="colorSquare"></div>
<p>{this.state.bgColor}</p>
<form onSubmit={this.setNewColor}>
<input
ref={el=>this._input = el} /* use ref to capture the input element after mounting so it can be referenced later */
placeholder="Enter a color value">
</input>
<button type="submit">go</button>
</form>
</div>;
}
setNewColor = e => {
const color = this._input.value; //setState() is async. so capture the current input value locally before clearing it.
this.setState({bgColor: color});
// Use the ref feature to manipulate the <input> element
this._input.value = '';
this._input.focus();
e.preventDefault();
}
}
const destination = document.querySelector("#container");
ReactDOM.render(
<div>
<Colorizer/>
</div>, destination);
</script>
</body>
</html>