File size: 2,042 Bytes
cf0f72e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | import React, { Component, PropTypes } from 'react';
class Note extends Component {
constructor(props){
super(props);
this.state = {
noteIsOn: false
};
}
handleNoteOn(){
this.setState({
noteIsOn: true
});
this.props.onNoteOn();
}
handleNoteOff(){
this.setState({
noteIsOn: false
});
this.props.onNoteOff();
}
onMouseEnter(){
const {
mouseIsDown
} = this.props;
if(!mouseIsDown){
return;
}
this.handleNoteOn();
}
onMouseLeave(){
const {
mouseIsDown
} = this.props;
if(!mouseIsDown){
return;
}
this.handleNoteOff();
}
onMouseDown(){
this.handleNoteOn();
}
onMouseUp(){
this.handleNoteOff();
}
componentWillReceiveProps(nextProps){
if(!this.props.noteIsDown && nextProps.noteIsDown){
this.handleNoteOn();
}
if(this.props.noteIsDown && !nextProps.noteIsDown){
this.handleNoteOff();
}
}
render(){
const {
type,
isWhiteKey,
x,
height,
width
} = this.props;
const {
noteIsOn
} = this.state;
return (
<rect
onMouseUp={ e => this.onMouseUp() }
onMouseDown={ e => this.onMouseDown() }
onMouseEnter={ e => this.onMouseEnter() }
onMouseLeave={ e => this.onMouseLeave() }
stroke={ isWhiteKey ? '#555555' : '979797' }
fill={ noteIsOn ? '#e19758' : isWhiteKey ? '#FFFFF7' : '4B4B4B' }
x={x}
y="0"
width={ isWhiteKey ? width : Math.floor(width / 2) }
height={ isWhiteKey ? height : Math.floor(height * 0.7 )}
/>
);
}
}
Note.propTypes = {
mouseIsDown: PropTypes.bool,
onNoteOn: PropTypes.func,
onNoteOff: PropTypes.func,
isWhiteKey: PropTypes.bool,
width: PropTypes.number,
height: PropTypes.number,
x: PropTypes.number,
noteIsDown: PropTypes.bool,
};
Note.defaultProps = {
x: 0,
width: 80,
height: 400,
onNoteOn: () => {},
onNoteOff: () => {}
};
export default Note;
|