Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prevActiveNotes race condition, and don't add 'active' style to disabled piano keys #46

Merged
merged 3 commits into from
Jun 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Keyboard extends React.Component {
>
{this.getMidiNumbers().map((midiNumber) => {
const { note, isAccidental } = MidiNumbers.getAttributes(midiNumber);
const isActive = this.props.activeNotes.includes(midiNumber);
const isActive = !this.props.disabled && this.props.activeNotes.includes(midiNumber);
return (
<Key
naturalKeyWidth={naturalKeyWidth}
Expand Down
25 changes: 16 additions & 9 deletions src/Piano.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ class Piano extends React.Component {
}

handlePlayNoteInput = (midiNumber) => {
if (this.props.onPlayNoteInput) {
this.props.onPlayNoteInput(midiNumber, { prevActiveNotes: this.state.activeNotes });
}
this.setState((prevState) => {
// Need to be handled inside setState in order to set prevActiveNotes without
// race conditions.
if (this.props.onPlayNoteInput) {
this.props.onPlayNoteInput(midiNumber, { prevActiveNotes: prevState.activeNotes });
}

// Don't append note to activeNotes if it's already present
if (prevState.activeNotes.includes(midiNumber)) {
return null;
Expand All @@ -59,12 +62,16 @@ class Piano extends React.Component {
};

handleStopNoteInput = (midiNumber) => {
if (this.props.onStopNoteInput) {
this.props.onStopNoteInput(midiNumber, { prevActiveNotes: this.state.activeNotes });
}
this.setState((prevState) => ({
activeNotes: prevState.activeNotes.filter((note) => midiNumber !== note),
}));
this.setState((prevState) => {
// Need to be handled inside setState in order to set prevActiveNotes without
// race conditions.
if (this.props.onStopNoteInput) {
this.props.onStopNoteInput(midiNumber, { prevActiveNotes: this.state.activeNotes });
}
return {
activeNotes: prevState.activeNotes.filter((note) => midiNumber !== note),
};
});
};

render() {
Expand Down
4 changes: 0 additions & 4 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@
/*
* Styles for disabled state
*/
.ReactPiano__Key--disabled.ReactPiano__Key {
cursor: progress;
}

.ReactPiano__Key--disabled.ReactPiano__Key--accidental {
background: #ddd;
border: 1px solid #999;
Expand Down