Skip to content

Commit

Permalink
Button onClick and conditional rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
lonerz committed Jun 10, 2020
1 parent d96f7ae commit 55957dd
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
28 changes: 27 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import CardEditor from './CardEditor';
import CardViewer from './CardViewer';

class App extends React.Component {
constructor(props) {
Expand All @@ -9,11 +10,36 @@ class App extends React.Component {
{ front: 'front1', back: 'back1' },
{ front: 'front2', back: 'back2' },
],
editor: true,
};
}

addCard = card => {
const cards = this.state.cards.slice().concat(card);
this.setState({ cards });
};

deleteCard = index => {
const cards = this.state.cards.slice();
cards.splice(index, 1);
this.setState({ cards });
};

switchMode = () => this.setState({ editor: !this.state.editor });

render() {
return <CardEditor cards={this.state.cards} />;
if (this.state.editor) {
return (
<CardEditor
addCard={this.addCard}
cards={this.state.cards}
deleteCard={this.deleteCard}
switchMode={this.switchMode}
/>
);
} else {
return <CardViewer switchMode={this.switchMode} />;
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/CardEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ class CardEditor extends React.Component {
this.state = { front: '', back: '' };
}

addCard = () => {
this.props.addCard(this.state);
this.setState({ front: '', back: '' });
};

deleteCard = index => this.props.deleteCard(index);

handleChange = event =>
this.setState({ [event.target.name]: event.target.value });

Expand All @@ -17,7 +24,7 @@ class CardEditor extends React.Component {
<td>{card.front}</td>
<td>{card.back}</td>
<td>
<button>Delete card</button>
<button onClick={() => this.deleteCard(index)}>Delete card</button>
</td>
</tr>
);
Expand Down Expand Up @@ -49,7 +56,9 @@ class CardEditor extends React.Component {
placeholder="Back of card"
value={this.state.back}
/>
<button>Add card</button>
<button onClick={this.addCard}>Add card</button>
<hr />
<button onClick={this.props.switchMode}>Go to card viewer</button>
</div>
);
}
Expand Down
15 changes: 15 additions & 0 deletions src/CardViewer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';

class CardViewer extends React.Component {
render() {
return (
<div>
<h2>Card Viewer</h2>
<hr />
<button onClick={this.props.switchMode}>Go to card editor</button>
</div>
);
}
}

export default CardViewer;

0 comments on commit 55957dd

Please sign in to comment.