Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Add toggling edit state
Browse files Browse the repository at this point in the history
  • Loading branch information
sicktastic committed Nov 27, 2017
1 parent 679dec1 commit e30f16a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
24 changes: 18 additions & 6 deletions reactjs/rsvp_app/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,40 @@ class App extends Component {
guests: [
{
name: "Treasure",
isConfirmed: false
isConfirmed: false,
isEditing: false
},
{
name: "Nick",
isConfirmed: true
isConfirmed: true,
isEditing: false
},
{
name: "Matt",
isConfirmed: true
isConfirmed: true,
isEditing: true
}
]
}

toggleConfirmationAt = indexToChange =>
toggleGuestPropertyAt = (property, indexToChange) =>
this.setState({
guests: this.state.guests.map((guest, index) => {
if (index === indexToChange) {
return {
...guest,
isConfirmed: !guest.isConfirmed
isConfirmed: !guest[property]
};
}
return guest;
})
});

toggleConfirmationAt = index =>
this.toggleGuestPropertyAt("isConfirmed", index);

toggleEditingAt = index =>
this.toggleGuestPropertyAt("isEditing", index);

getTotalInvited = () => this.state.guests.length;
// getAttendingGuests = () =>
Expand Down Expand Up @@ -74,7 +82,11 @@ class App extends Component {
</tbody>
</table>

<GuestList guests={this.state.guests} />
<GuestList
guests={this.state.guests}
toggleConfirmationAt={this.toggleConfirmationAt}
toggleEditingAt={this.toggleEditingAt}
/>

</div>
</div>
Expand Down
12 changes: 9 additions & 3 deletions reactjs/rsvp_app/src/Guest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ const Guest = props =>
<li>
<span>{props.name}</span>
<label>
<input type="checkbox" checked={props.isConfirmed} /> Confirmed
<input
type="checkbox"
checked={props.isConfirmed}
onChange={props.handleConfirmation} /> Confirmed
</label>
<button>edit</button>
<button onClick={props.handleToggleEditing}>edit</button>
<button>remove</button>
</li>

Guest.propTypes = {
name: PropTypes.string.isRequired,
isConfirmed: PropTypes.bool.isRequired
isConfirmed: PropTypes.bool.isRequired,
isEditing: PropTypes.bool.isRequired,
handleConfirmation: PropTypes.func.isRequired,
handleToggleEditing: PropTypes.func.isRequired
}

export default Guest
12 changes: 10 additions & 2 deletions reactjs/rsvp_app/src/GuestList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ import Guest from './Guest'
const GuestList = props =>
<ul>
{props.guests.map((guest, index) =>
<Guest key={index} name={guest.name} isConfirmed={guest.isConfirmed} />
<Guest
key={index}
name={guest.name}
isConfirmed={guest.isConfirmed}
isEditing={guest.isEditing}
handleConfirmation={() => props.toggleConfirmationAt(index)}
handleToggleEditing={() => props.toggleEditingAt(index)} />
)}
</ul>;

GuestList.propTypes = {
guests: PropTypes.array.isRequired
guests: PropTypes.array.isRequired,
toggleConfirmationAt: PropTypes.func.isRequired,
toggleEditingAt: PropTypes.func.isRequired
}

export default GuestList
4 changes: 2 additions & 2 deletions reactjs/rsvp_app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5114,7 +5114,7 @@ react-dev-utils@^4.2.1:
strip-ansi "3.0.1"
text-table "0.2.0"

[email protected]:
react-dom@^16.1.1:
version "16.1.1"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.1.1.tgz#b2e331b6d752faf1a2d31399969399a41d8d45f8"
dependencies:
Expand Down Expand Up @@ -5170,7 +5170,7 @@ [email protected]:
optionalDependencies:
fsevents "1.1.2"

[email protected]:
react@^16.1.1:
version "16.1.1"
resolved "https://registry.yarnpkg.com/react/-/react-16.1.1.tgz#d5c4ef795507e3012282dd51261ff9c0e824fe1f"
dependencies:
Expand Down

0 comments on commit e30f16a

Please sign in to comment.