Guest User

Untitled

a guest
Jan 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. function bits(n){
  2. var res = [];
  3. while(n){
  4. res.push(n & 1);
  5. n >>= 1;
  6. }
  7. return res;
  8. }
  9.  
  10. class App extends React.Component {
  11. constructor(){
  12. super();
  13. this.state = {buttons: 0};
  14. }
  15.  
  16. render(){
  17. const preventDefault = (e) => { e.preventDefault() }
  18. const handleChange = (e) => {
  19. preventDefault(e);
  20. this.setState({buttons: e.buttons});
  21. };
  22.  
  23. var names = [
  24. 'Left', 'Right', 'Middle', 'Back', 'Forward'
  25. ];
  26.  
  27. // buttons is a bitmask
  28. var pad = "00000";
  29. var buttons = bits(this.state.buttons).concat([0, 0, 0, 0, 0]).slice(0, names.length);
  30.  
  31. var active = buttons.map((x, i) => {
  32. return <div>{names[i]}: {x === 1 ? 'Yes' : 'No'}</div>;
  33. });
  34.  
  35.  
  36. return (
  37. <section
  38. onContextMenu={preventDefault}
  39. onMouseDown={handleChange}
  40. tabIndex={-1}>
  41. <div>
  42. <h4>Buttons: {this.state.buttons} ({buttons.join('')})</h4>
  43. {active}
  44. </div>
  45. </section>
  46. );
  47. }
  48. }
  49.  
  50. React.render(<App />, document.body);
Add Comment
Please, Sign In to add comment