forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboardAvoidingViewExample.js
More file actions
120 lines (109 loc) · 2.89 KB
/
KeyboardAvoidingViewExample.js
File metadata and controls
120 lines (109 loc) · 2.89 KB
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
const React = require('react');
const {
KeyboardAvoidingView,
Modal,
SegmentedControlIOS,
StyleSheet,
Text,
TextInput,
TouchableHighlight,
View,
} = require('react-native');
const RNTesterBlock = require('../../components/RNTesterBlock');
const RNTesterPage = require('../../components/RNTesterPage');
type Props = $ReadOnly<{||}>;
type State = {|
behavior: string,
modalOpen: boolean,
|};
class KeyboardAvoidingViewExample extends React.Component<Props, State> {
state = {
behavior: 'padding',
modalOpen: false,
};
onSegmentChange = (segment: String) => {
this.setState({behavior: segment.toLowerCase()});
};
renderExample = () => {
return (
<View style={styles.outerContainer}>
<Modal animationType="fade" visible={this.state.modalOpen}>
<KeyboardAvoidingView
behavior={this.state.behavior}
style={styles.container}>
<SegmentedControlIOS
onValueChange={this.onSegmentChange}
selectedIndex={this.state.behavior === 'padding' ? 0 : 1}
style={styles.segment}
values={['Padding', 'Position']}
/>
<TextInput placeholder="<TextInput />" style={styles.textInput} />
</KeyboardAvoidingView>
<TouchableHighlight
onPress={() => this.setState({modalOpen: false})}
style={styles.closeButton}>
<Text>Close</Text>
</TouchableHighlight>
</Modal>
<TouchableHighlight onPress={() => this.setState({modalOpen: true})}>
<Text>Open Example</Text>
</TouchableHighlight>
</View>
);
};
render() {
return (
<RNTesterPage title="Keyboard Avoiding View">
<RNTesterBlock title="Keyboard-avoiding views move out of the way of the keyboard.">
{this.renderExample()}
</RNTesterBlock>
</RNTesterPage>
);
}
}
const styles = StyleSheet.create({
outerContainer: {
flex: 1,
},
container: {
flex: 1,
justifyContent: 'center',
paddingHorizontal: 20,
paddingTop: 20,
},
textInput: {
borderRadius: 5,
borderWidth: 1,
height: 44,
paddingHorizontal: 10,
},
segment: {
marginBottom: 10,
},
closeButton: {
position: 'absolute',
top: 40,
left: 10,
padding: 10,
},
});
exports.title = '<KeyboardAvoidingView>';
exports.description =
'Base component for views that automatically adjust their height or position to move out of the way of the keyboard.';
exports.examples = [
{
title: 'Simple keyboard view',
render: function(): React.Element<typeof KeyboardAvoidingViewExample> {
return <KeyboardAvoidingViewExample />;
},
},
];