Skip to content

Commit 24523da

Browse files
committed
card component
1 parent 70bd1cb commit 24523da

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

Module02/cardComponent.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const Card = (props) => {
2+
return (
3+
<div style={{margin: '1em'}}>
4+
<img width="75" src={props.avatar_url} />
5+
<div style={{display: 'inline-block', marginLeft: 10}}>
6+
<div style={{fontSize: '1.25em', fontWeight: 'bold'}}>
7+
{props.name}
8+
</div>
9+
<div>
10+
{props.company}
11+
</div>
12+
</div>
13+
</div>
14+
);
15+
}
16+
17+
const CardList = (props) => {
18+
return (
19+
<div>
20+
{props.cards.map(card => <Card key={card.id} {...card} />)}
21+
</div>
22+
);
23+
}
24+
25+
class Form extends React.Component {
26+
state = { userName: '' }
27+
28+
handleSubmit = (event) => {
29+
event.preventDefault();
30+
axios.get(`https://api.github.com/users/${this.state.userName}`)
31+
.then(resp => {
32+
this.props.onSubmit(resp.data);
33+
this.setState({ userName: ''});
34+
});
35+
}
36+
37+
render() {
38+
return (
39+
<form onSubmit={this.handleSubmit}>
40+
<input type="text"
41+
value={this.state.userName}
42+
onChange={(event) => this.setState({ userName: event.target.value })}
43+
placeholder="Github username"
44+
required />
45+
<button type="submit">Add card</button>
46+
</form>
47+
);
48+
}
49+
}
50+
51+
class App extends React.Component {
52+
state = {
53+
cards: []
54+
}
55+
56+
addNewCard = (cardInfo) => {
57+
this.setState( prevState => ({
58+
cards: prevState.cards.concat(cardInfo)
59+
}));
60+
}
61+
62+
render() {
63+
return(
64+
<div>
65+
<Form onSubmit={this.addNewCard}/>
66+
<CardList cards={this.state.cards} />
67+
</div>
68+
);
69+
}
70+
}
71+
72+
ReactDOM.render(<App />,mountNode);

0 commit comments

Comments
 (0)