Skip to content
This repository was archived by the owner on Oct 3, 2021. It is now read-only.

Commit 94d32ab

Browse files
committed
Composition vs Inheritance
1 parent 6b04446 commit 94d32ab

File tree

1 file changed

+33
-73
lines changed

1 file changed

+33
-73
lines changed

src/index.js

+33-73
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,59 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33

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+
);
1310
}
1411

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+
);
2724
}
2825

29-
const scaleNames = {
30-
c: 'Celsius',
31-
f: 'Fahrenheit'
32-
};
33-
34-
class TemperatureInput extends React.Component {
26+
class SignUpDialog extends React.Component {
3527
constructor(props) {
3628
super(props);
3729
this.handleChange = this.handleChange.bind(this);
30+
this.handleSignUp = this.handleSignUp.bind(this);
31+
this.state = {login: ''};
3832
}
3933

4034
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});
5436
}
55-
}
5637

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}!`);
7140
}
7241

7342
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-
7943
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>
9252
);
9353
}
9454
}
9555

9656
ReactDOM.render(
97-
<Calculator />,
57+
<SignUpDialog />,
9858
document.getElementById('root')
9959
);

0 commit comments

Comments
 (0)