3
3
*/
4
4
5
5
import * as React from 'react' ;
6
- import { getComponentDisplayName } from '../utils' ;
7
- import { unsafeCreateUniqueId } from '../../renderer/utils' ;
8
- import { FocusPathContext } from './FocusPathContext' ;
6
+ import { getComponentDisplayName } from '../utils' ;
7
+ import { unsafeCreateUniqueId } from '../../renderer/utils' ;
8
+ import { FocusPathContext } from './FocusPathContext' ;
9
9
import FocusSpatialMap from './FocusSpatialMap' ;
10
10
11
11
type RequiredProps = { } ;
12
12
13
13
// See why we do `| void`
14
14
// https://flow.org/en/docs/react/hoc/#toc-injecting-props-with-a-higher-order-component
15
15
type InjectedProps = {
16
- focusPath : string | void
16
+ focusPath : string | void ,
17
+ } ;
18
+
19
+ type State = {
20
+ currentFocusPath : ?string ,
17
21
} ;
18
22
19
23
/**
@@ -24,13 +28,14 @@ type InjectedProps = {
24
28
function withNavigation < Props : RequiredProps > (
25
29
WrappedComponent: React.ComponentType< Props >
26
30
): React.ComponentType< $Diff < Props , InjectedProps > > {
27
- return class extends React . Component < Props > {
31
+ return class extends React . Component < Props , State > {
28
32
static WrappedComponent = WrappedComponent ;
29
33
static displayName = `withNavigation(${ getComponentDisplayName (
30
34
WrappedComponent
31
35
) } )`;
32
36
33
37
rootFocusPath : string ;
38
+ focusPathList : mixed ;
34
39
35
40
constructor ( ) {
36
41
super ( ...arguments ) ;
@@ -39,56 +44,52 @@ function withNavigation<Props: RequiredProps>(
39
44
40
45
this . focusPathList = [ ] ;
41
46
this . state = {
42
- currentFocusPath : false
47
+ currentFocusPath : null ,
43
48
} ;
44
- window . addEventListener ( 'keydown' , ( e ) => this . handleKeyDown ( e ) ) ;
45
- }
46
-
47
- setFocus = ( currentFocusPath ) => {
48
- this . setState ( { currentFocusPath } ) ;
49
+ window . addEventListener ( 'keydown' , e => this . handleKeyDown ( e ) ) ;
49
50
}
50
51
51
- setFocusNext ( ) {
52
+ setFocus = currentFocusPath => {
53
+ this . setState ( { currentFocusPath} ) ;
54
+ } ;
52
55
53
- }
56
+ setFocusNext ( ) { }
54
57
55
- handleKeyDown = ( e ) = > {
56
- const { currentFocusPath } = this . state ;
58
+ handleKeyDown = e => {
59
+ const { currentFocusPath } = this.state;
57
60
// arrow up/down button should select next/previous list element
58
61
if (e.keyCode === 38) {
59
62
// console.log(FocusSpatialMap.indexOf(currentFocusPath))
60
63
// this.setState( prevState => ({
61
-
62
64
// }))
63
65
} else if (e.keyCode === 40) {
64
66
// console.log(FocusSpatialMap.indexOf(currentFocusPath))
65
67
const idx = FocusSpatialMap . indexOf ( currentFocusPath ) + 1 ;
66
68
const next = idx + 1 ;
67
69
const nextFocusPath = FocusSpatialMap [ next ] ;
68
70
this . setState ( {
69
- currentFocusPath : nextFocusPath
71
+ currentFocusPath : nextFocusPath ,
70
72
} ) ;
71
73
// this.setState( prevState => ({
72
74
// cursor: prevState.cursor + 1
73
75
// }))
74
76
}
75
- }
77
+ } ;
76
78
77
79
render ( ) {
78
- const { currentFocusPath } = this . state ;
80
+ const { currentFocusPath } = this.state;
79
81
return (
80
- < FocusPathContext . Provider
82
+ < FocusPathContext . Provider
81
83
value = { {
82
84
// rootFocusPath: this.rootFocusPath,
83
85
currentFocusPath : currentFocusPath ,
84
- setFocus : this . setFocus
85
- } }
86
- >
86
+ setFocus : this . setFocus ,
87
+ } } >
87
88
< WrappedComponent { ...this . props } />
88
89
</ FocusPathContext . Provider >
89
90
);
90
91
}
91
92
} ;
92
93
}
93
94
94
- export default withNavigation ;
95
+ export default withNavigation ;
0 commit comments