11import React , { Component , PropTypes } from 'react' ;
22import Gamepad from 'html5-gamepad' ;
33
4- import Slide from './slide' ;
4+ import Basics from './basics' ;
5+
6+ const slides = [ Basics ] ;
57
68const gamepad = new Gamepad ( ) ;
79
810export default class Slides extends Component {
911
1012 static propTypes = {
13+ index : PropTypes . number ,
1114 onDone : PropTypes . func ,
1215 } ;
1316
17+ restartLoop = ( ) => {
18+ setTimeout ( ( ) => {
19+ this . startUpdate ( ) ;
20+ } , 300 ) ;
21+ }
22+
1423 startUpdate = ( ) => {
1524 gamepad . update ( ) ;
1625 if ( gamepad . button ( 0 , 'y' ) ) {
1726 this . props . onDone ( ) ;
1827 return ;
1928 }
29+
30+ if ( gamepad . button ( 0 , 'button 14' ) ) {
31+ this . handlePrev ( ) ;
32+ return ;
33+ }
34+
35+ if ( gamepad . button ( 0 , 'button 15' ) ) {
36+ this . handleNext ( ) ;
37+ return ;
38+ }
39+
2040 this . animationFrame = requestAnimationFrame ( this . startUpdate ) ;
2141 }
2242
2343 handleKeyPress = ( e ) => {
2444 if ( e . keyCode === 27 ) {
2545 this . props . onDone ( ) ;
2646 }
47+
48+ if ( e . keyCode === 37 ) {
49+ this . handlePrev ( ) ;
50+ }
51+
52+ if ( e . keyCode === 39 ) {
53+ this . handleNext ( ) ;
54+ }
55+ }
56+
57+ handleNext ( ) {
58+ const { currentSlide } = this . state ;
59+ const { index } = this . props ;
60+
61+ if ( currentSlide + 1 === slides [ index ] . slides . length ) {
62+ this . props . onDone ( ) ;
63+ } else {
64+ this . setState ( {
65+ currentSlide : currentSlide + 1 ,
66+ } , ( ) => {
67+ this . restartLoop ( ) ;
68+ } ) ;
69+ }
70+ }
71+
72+ handlePrev ( ) {
73+ const { currentSlide } = this . state ;
74+
75+ if ( currentSlide !== 0 ) {
76+ this . setState ( {
77+ currentSlide : currentSlide - 1 ,
78+ } , ( ) => {
79+ this . restartLoop ( ) ;
80+ } ) ;
81+ }
2782 }
2883
2984 constructor ( props ) {
3085 super ( props ) ;
86+
87+ this . state = {
88+ currentSlide : 0 ,
89+ } ;
3190 }
3291
3392 componentDidMount ( ) {
@@ -37,6 +96,7 @@ export default class Slides extends Component {
3796 }
3897
3998 componentWillUnmount ( ) {
99+ window . removeEventListener ( 'keyup' , this . handleKeyPress ) ;
40100 window . removeEventListener ( 'keypress' , this . handleKeyPress ) ;
41101 cancelAnimationFrame ( this . animationFrame ) ;
42102 }
@@ -54,7 +114,7 @@ export default class Slides extends Component {
54114 render ( ) {
55115 return (
56116 < div style = { this . getWrapperStyles ( ) } >
57- < Slide > test </ Slide >
117+ { slides [ this . props . index ] . slides [ this . state . currentSlide ] }
58118 </ div >
59119 ) ;
60120 }
0 commit comments