-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathKeyboardExtensionExample.windows.tsx
189 lines (178 loc) · 5.28 KB
/
KeyboardExtensionExample.windows.tsx
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
* @format
*/
import React from 'react';
import {View, StyleSheet, Text, TextInput} from 'react-native';
import {
HandledEventPhase,
IHandledKeyboardEvent,
IKeyboardEvent,
supportKeyboard,
} from 'react-native-windows';
const ViewWindows = supportKeyboard(View);
const styles = StyleSheet.create({
border: {
borderStyle: 'dotted',
borderColor: 'black',
},
keyComponentRoot: {
borderWidth: 2,
flexDirection: 'row',
marginVertical: 5,
backgroundColor: 'whitesmoke',
justifyContent: 'space-around',
},
keyEnterVisualizer: {
margin: 5,
alignItems: 'center',
minWidth: 100,
minHeight: 30,
},
textInput: {
height: 32,
width: 100,
},
blackbox: {height: 30, width: 30, borderColor: 'black', borderWidth: 3},
});
interface IKeyboardableComponentState {
lastKeyDown: string | null;
lastKeyUp: string | null;
lastKeyDownCapture: string | null;
lastKeyUpCapture: string | null;
lastKeyDownCode: string | null;
lastKeyUpCode: string | null;
lastKeyDownCaptureCode: string | null;
lastKeyUpCaptureCode: string | null;
}
const handledNativeKeyboardEvents: IHandledKeyboardEvent[] = [
{code: 'KeyA', handledEventPhase: HandledEventPhase.Capturing},
{code: 'KeyB'},
{code: 'Digit1', handledEventPhase: HandledEventPhase.Bubbling},
{code: 'Tab', handledEventPhase: HandledEventPhase.Capturing},
];
class ViewWindowsKeyboardExample extends React.Component<
{},
IKeyboardableComponentState
> {
public constructor(props: {}) {
super(props);
this.state = {
lastKeyDown: null,
lastKeyUp: null,
lastKeyDownCapture: null,
lastKeyUpCapture: null,
lastKeyDownCode: null,
lastKeyUpCode: null,
lastKeyDownCaptureCode: null,
lastKeyUpCaptureCode: null,
};
}
public render(): JSX.Element {
return (
<ViewWindows>
<ViewWindows
style={styles.keyComponentRoot}
onKeyDownCapture={this._onKeyDownCapture}
onKeyUpCapture={this._onKeyUpCapture}
onKeyUp={this._onKeyUp}
onKeyDown={this._onKeyDown}
keyDownEvents={handledNativeKeyboardEvents}
keyUpEvents={handledNativeKeyboardEvents}>
<ViewWindows style={styles.keyEnterVisualizer}>
<Text>OnKeyDown Key and Code</Text>
<Text>
{this.state.lastKeyDown !== null ? this.state.lastKeyDown : ' '}
</Text>
<Text>
{this.state.lastKeyDownCode !== null
? this.state.lastKeyDownCode
: ' '}
</Text>
<Text>OnKeyDownCapture Key and Code</Text>
<Text>
{this.state.lastKeyDownCapture !== null
? this.state.lastKeyDownCapture
: ' '}
</Text>
<Text>
{this.state.lastKeyDownCaptureCode !== null
? this.state.lastKeyDownCaptureCode
: ' '}
</Text>
</ViewWindows>
<ViewWindows style={styles.keyEnterVisualizer}>
<Text>OnKeyUp Key and Code</Text>
<Text>
{this.state.lastKeyUp !== null ? this.state.lastKeyUp : ' '}
</Text>
<Text>
{this.state.lastKeyUpCode !== null
? this.state.lastKeyUpCode
: ' '}
</Text>
<Text>OnKeyUpCapture Key and Code</Text>
<Text>
{this.state.lastKeyUpCapture !== null
? this.state.lastKeyUpCapture
: ' '}
</Text>
<Text>
{this.state.lastKeyUpCaptureCode !== null
? this.state.lastKeyUpCaptureCode
: ' '}
</Text>
</ViewWindows>
<ViewWindows style={styles.keyEnterVisualizer}>
<TextInput placeholder="I got focus" style={styles.textInput} />
</ViewWindows>
</ViewWindows>
</ViewWindows>
);
}
private readonly _onKeyUp = (ev: IKeyboardEvent) => {
this.setState({
lastKeyUp: ev.nativeEvent.key,
lastKeyDown: null,
lastKeyUpCode: ev.nativeEvent.code,
lastKeyDownCode: null,
});
};
private readonly _onKeyDown = (ev: IKeyboardEvent) => {
this.setState({
lastKeyDown: ev.nativeEvent.key,
lastKeyUp: null,
lastKeyDownCode: ev.nativeEvent.code,
lastKeyUpCode: null,
});
};
private readonly _onKeyUpCapture = (ev: IKeyboardEvent) => {
this.setState({
lastKeyUpCapture: ev.nativeEvent.key,
lastKeyDownCapture: null,
lastKeyUpCaptureCode: ev.nativeEvent.code,
lastKeyDownCaptureCode: null,
});
};
private readonly _onKeyDownCapture = (ev: IKeyboardEvent) => {
this.setState({
lastKeyDownCapture: ev.nativeEvent.key,
lastKeyUpCapture: null,
lastKeyDownCaptureCode: ev.nativeEvent.code,
lastKeyUpCaptureCode: null,
});
};
}
export const displayName = (_undefined?: string) => {};
export const title = 'Keyboard extension Example';
export const category = 'Basic';
export const description = 'Demo of keyboard properties.';
export const examples = [
{
title: 'Keyboard extension example',
render(): JSX.Element {
return <ViewWindowsKeyboardExample />;
},
},
];