|
1 | 1 | import React from 'react';
|
2 | 2 | import ReactDOM from 'react-dom';
|
3 | 3 |
|
4 |
| -function BoilingVerdict(props) { |
5 |
| - if (props.celsius >= 100) { |
6 |
| - return <p>The water would boil.</p>; |
7 |
| - } |
8 |
| - return <p>The water would not boil.</p>; |
9 |
| -} |
10 |
| - |
11 |
| -function toCelsius(fahrenheit) { |
12 |
| - return (fahrenheit - 32) * 5 / 9; |
| 4 | +function FancyBorder(props) { |
| 5 | + return ( |
| 6 | + <div className={'FancyBorder FancyBorder-' + props.color}> |
| 7 | + {props.children} |
| 8 | + </div> |
| 9 | + ); |
13 | 10 | }
|
14 | 11 |
|
15 |
| -function toFahrenheit(celsius) { |
16 |
| - return (celsius * 9 / 5) +32; |
17 |
| -} |
18 |
| - |
19 |
| -function tryConvert(temperature, convert) { |
20 |
| - const input = parseFloat(temperature); |
21 |
| - if (Number.isNaN(input)) { |
22 |
| - return ''; |
23 |
| - } |
24 |
| - const output = convert(input); |
25 |
| - const rounded = Math.round(output * 1000) / 1000; |
26 |
| - return rounded.toString(); |
| 12 | +function Dialog(props) { |
| 13 | + return ( |
| 14 | + <FancyBorder color="blue"> |
| 15 | + <h1 className="Dialog-title"> |
| 16 | + {props.title} |
| 17 | + </h1> |
| 18 | + <p className="Dialog-message"> |
| 19 | + {props.message} |
| 20 | + </p> |
| 21 | + {props.children} |
| 22 | + </FancyBorder> |
| 23 | + ); |
27 | 24 | }
|
28 | 25 |
|
29 |
| -const scaleNames = { |
30 |
| - c: 'Celsius', |
31 |
| - f: 'Fahrenheit' |
32 |
| -}; |
33 |
| - |
34 |
| -class TemperatureInput extends React.Component { |
| 26 | +class SignUpDialog extends React.Component { |
35 | 27 | constructor(props) {
|
36 | 28 | super(props);
|
37 | 29 | this.handleChange = this.handleChange.bind(this);
|
| 30 | + this.handleSignUp = this.handleSignUp.bind(this); |
| 31 | + this.state = {login: ''}; |
38 | 32 | }
|
39 | 33 |
|
40 | 34 | handleChange(e) {
|
41 |
| - this.props.onTemperatureChange(e.target.value); |
42 |
| - } |
43 |
| - |
44 |
| - render() { |
45 |
| - const temperature = this.props.temperature; |
46 |
| - const scale = this.props.scale; |
47 |
| - return ( |
48 |
| - <fieldset> |
49 |
| - <legend>Enter temperature in {scaleNames[scale]}:</legend> |
50 |
| - <input value={temperature} |
51 |
| - onChange={this.handleChange} /> |
52 |
| - </fieldset> |
53 |
| - ); |
| 35 | + this.setState({login: e.target.value}); |
54 | 36 | }
|
55 |
| -} |
56 | 37 |
|
57 |
| -class Calculator extends React.Component { |
58 |
| - constructor(props) { |
59 |
| - super(props); |
60 |
| - this.handleCelsiusChange = this.handleCelsiusChange.bind(this); |
61 |
| - this.handleFahrenheitChange = this.handleFahrenheitChange.bind(this); |
62 |
| - this.state = {temperature: '', scale: 'c'}; |
63 |
| - } |
64 |
| - |
65 |
| - handleCelsiusChange(temperature) { |
66 |
| - this.setState({scale: 'c', temperature}); |
67 |
| - } |
68 |
| - |
69 |
| - handleFahrenheitChange(temperature) { |
70 |
| - this.setState({scale: 'f', temperature}); |
| 38 | + handleSignUp() { |
| 39 | + alert(`Welcome aboard, ${this.state.login}!`); |
71 | 40 | }
|
72 | 41 |
|
73 | 42 | render() {
|
74 |
| - const scale = this.state.scale; |
75 |
| - const temperature = this.state.temperature; |
76 |
| - const celsius = scale === 'f' ? tryConvert(temperature, toCelsius) : temperature; |
77 |
| - const fahrenheit = scale === 'c' ? tryConvert(temperature, toFahrenheit) : temperature; |
78 |
| - |
79 | 43 | return (
|
80 |
| - <div> |
81 |
| - <TemperatureInput |
82 |
| - scale="c" |
83 |
| - temperature={celsius} |
84 |
| - onTemperatureChange={this.handleCelsiusChange} /> |
85 |
| - <TemperatureInput |
86 |
| - scale="f" |
87 |
| - temperature={fahrenheit} |
88 |
| - onTemperatureChange={this.handleFahrenheitChange} /> |
89 |
| - <BoilingVerdict |
90 |
| - celsius={parseFloat(celsius)} /> |
91 |
| - </div> |
| 44 | + <Dialog title="Mars Exploration Program" |
| 45 | + message="How should we refer to you?"> |
| 46 | + <input value={this.state.login} |
| 47 | + onChange={this.handleChange} /> |
| 48 | + <button onClick={this.handleSignUp}> |
| 49 | + Sign Me Up! |
| 50 | + </button> |
| 51 | + </Dialog> |
92 | 52 | );
|
93 | 53 | }
|
94 | 54 | }
|
95 | 55 |
|
96 | 56 | ReactDOM.render(
|
97 |
| - <Calculator />, |
| 57 | + <SignUpDialog />, |
98 | 58 | document.getElementById('root')
|
99 | 59 | );
|
0 commit comments