1
1
import React , { cloneElement , Children , Component } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import Dock from 'react-dock' ;
4
+ import { Action , Dispatch } from 'redux' ;
5
+ import { LiftedState , Monitor } from 'redux-devtools' ;
4
6
import { POSITIONS } from './constants' ;
5
7
import {
6
8
toggleVisibility ,
7
9
changeMonitor ,
8
10
changePosition ,
9
11
changeSize ,
12
+ DockMonitorAction ,
10
13
} from './actions' ;
11
- import reducer from './reducers' ;
14
+ import reducer , { DockMonitorState } from './reducers' ;
12
15
import parseKey from 'parse-key' ;
13
16
14
- export default class DockMonitor extends Component {
17
+ interface KeyObject {
18
+ name : string ;
19
+ ctrl : boolean ;
20
+ meta : boolean ;
21
+ shift : boolean ;
22
+ alt : boolean ;
23
+ sequence : string ;
24
+ }
25
+
26
+ export interface DockMonitorProps < S , A extends Action < unknown > >
27
+ extends LiftedState < S , A , DockMonitorState > {
28
+ defaultPosition : 'left' | 'top' | 'right' | 'bottom' ;
29
+ defaultIsVisible : boolean ;
30
+ defaultSize : number ;
31
+ toggleVisibilityKey : string ;
32
+ changePositionKey : string ;
33
+ changeMonitorKey ?: string ;
34
+ fluid : boolean ;
35
+
36
+ dispatch : Dispatch < DockMonitorAction > ;
37
+
38
+ children :
39
+ | Monitor < S , A , LiftedState < S , A , unknown > , unknown , Action < unknown > >
40
+ | Monitor < S , A , LiftedState < S , A , unknown > , unknown , Action < unknown > > [ ] ;
41
+ }
42
+
43
+ export default class DockMonitor <
44
+ S ,
45
+ A extends Action < unknown >
46
+ > extends Component < DockMonitorProps < S , A > > {
15
47
static update = reducer ;
16
48
17
49
static propTypes = {
@@ -39,10 +71,8 @@ export default class DockMonitor extends Component {
39
71
fluid : true ,
40
72
} ;
41
73
42
- constructor ( props ) {
74
+ constructor ( props : DockMonitorProps < S , A > ) {
43
75
super ( props ) ;
44
- this . handleKeyDown = this . handleKeyDown . bind ( this ) ;
45
- this . handleSizeChange = this . handleSizeChange . bind ( this ) ;
46
76
47
77
const childrenCount = Children . count ( props . children ) ;
48
78
if ( childrenCount === 0 ) {
@@ -71,7 +101,7 @@ export default class DockMonitor extends Component {
71
101
window . removeEventListener ( 'keydown' , this . handleKeyDown ) ;
72
102
}
73
103
74
- matchesKey ( key , event ) {
104
+ matchesKey ( key : KeyObject | undefined , event : KeyboardEvent ) {
75
105
if ( ! key ) {
76
106
return false ;
77
107
}
@@ -87,17 +117,17 @@ export default class DockMonitor extends Component {
87
117
) ;
88
118
}
89
119
90
- handleKeyDown ( e ) {
120
+ handleKeyDown = ( e : KeyboardEvent ) => {
91
121
// Ignore regular keys when focused on a field
92
122
// and no modifiers are active.
93
123
if (
94
124
! e . ctrlKey &&
95
125
! e . metaKey &&
96
126
! e . altKey &&
97
- ( e . target . tagName === 'INPUT' ||
98
- e . target . tagName === 'SELECT' ||
99
- e . target . tagName === 'TEXTAREA' ||
100
- e . target . isContentEditable )
127
+ ( ( e . target ! as { tagName ?: string } ) . tagName === 'INPUT' ||
128
+ ( e . target ! as { tagName ?: string } ) . tagName === 'SELECT' ||
129
+ ( e . target ! as { tagName ?: string } ) . tagName === 'TEXTAREA' ||
130
+ ( e . target ! as { isContentEditable ?: boolean } ) . isContentEditable )
101
131
) {
102
132
return ;
103
133
}
@@ -120,13 +150,20 @@ export default class DockMonitor extends Component {
120
150
e . preventDefault ( ) ;
121
151
this . props . dispatch ( changeMonitor ( ) ) ;
122
152
}
123
- }
153
+ } ;
124
154
125
- handleSizeChange ( requestedSize ) {
155
+ handleSizeChange = ( requestedSize : number ) => {
126
156
this . props . dispatch ( changeSize ( requestedSize ) ) ;
127
- }
157
+ } ;
128
158
129
- renderChild ( child , index , otherProps ) {
159
+ renderChild (
160
+ child : Monitor < S , A , LiftedState < S , A , unknown > , unknown , Action < unknown > > ,
161
+ index : number ,
162
+ otherProps : Omit <
163
+ DockMonitorProps < S , A > ,
164
+ 'monitorState' | 'children' | 'fluid'
165
+ >
166
+ ) {
130
167
const { monitorState } = this . props ;
131
168
const { childMonitorIndex, childMonitorStates } = monitorState ;
132
169
@@ -153,8 +190,23 @@ export default class DockMonitor extends Component {
153
190
onSizeChange = { this . handleSizeChange }
154
191
dimMode = "none"
155
192
>
156
- { Children . map ( children , ( child , index ) =>
157
- this . renderChild ( child , index , rest )
193
+ { Children . map (
194
+ children as
195
+ | Monitor <
196
+ S ,
197
+ A ,
198
+ LiftedState < S , A , unknown > ,
199
+ unknown ,
200
+ Action < unknown >
201
+ >
202
+ | Monitor <
203
+ S ,
204
+ A ,
205
+ LiftedState < S , A , unknown > ,
206
+ unknown ,
207
+ Action < unknown >
208
+ > [ ] ,
209
+ ( child , index ) => this . renderChild ( child , index , rest )
158
210
) }
159
211
</ Dock >
160
212
) ;
0 commit comments