mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-18 04:13:51 +08:00
Open sourced KeyboardAvoidingView
Summary: KeyboardAvoidingView is a component we built internally to solve the common problem of views that need to move out of the way of the virtual keyboard. KeyboardAvoidingView can automatically adjust either its position or bottom padding based on the position of the keyboard. Reviewed By: javache Differential Revision: D3398238 fbshipit-source-id: 493f2d2dec76667996250c011a1c5b7a14f245eb
This commit is contained in:
committed by
Facebook Github Bot 8
parent
d64368b9e2
commit
8b78846a95
111
Examples/UIExplorer/KeyboardAvoidingViewExample.js
Normal file
111
Examples/UIExplorer/KeyboardAvoidingViewExample.js
Normal file
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* Copyright (c) 2013-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* @providesModule KeyboardAvoidingViewExample
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
const React = require('React');
|
||||
const ReactNative = require('react-native');
|
||||
const {
|
||||
KeyboardAvoidingView,
|
||||
Modal,
|
||||
SegmentedControlIOS,
|
||||
StyleSheet,
|
||||
Text,
|
||||
TextInput,
|
||||
TouchableHighlight,
|
||||
View,
|
||||
} = ReactNative;
|
||||
|
||||
const UIExplorerBlock = require('./UIExplorerBlock');
|
||||
const UIExplorerPage = require('./UIExplorerPage');
|
||||
|
||||
const KeyboardAvoidingViewExample = React.createClass({
|
||||
statics: {
|
||||
title: '<KeyboardAvoidingView>',
|
||||
description: 'Base component for views that automatically adjust their height or position to move out of the way of the keyboard.',
|
||||
},
|
||||
|
||||
getInitialState() {
|
||||
return {
|
||||
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 (
|
||||
<UIExplorerPage title="Keyboard Avoiding View">
|
||||
<UIExplorerBlock title="Keyboard-avoiding views move out of the way of the keyboard.">
|
||||
{this.renderExample()}
|
||||
</UIExplorerBlock>
|
||||
</UIExplorerPage>
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
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: 30,
|
||||
left: 10,
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = KeyboardAvoidingViewExample;
|
||||
@@ -1,4 +1,11 @@
|
||||
/**
|
||||
* Copyright (c) 2013-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* The examples provided by Facebook are for non-commercial testing and
|
||||
* evaluation purposes only.
|
||||
*
|
||||
|
||||
@@ -40,6 +40,10 @@ const ComponentExamples: Array<UIExplorerExample> = [
|
||||
key: 'ImageExample',
|
||||
module: require('./ImageExample'),
|
||||
},
|
||||
{
|
||||
key: 'KeyboardAvoidingViewExample',
|
||||
module: require('./KeyboardAvoidingViewExample'),
|
||||
},
|
||||
{
|
||||
key: 'LayoutEventsExample',
|
||||
module: require('./LayoutEventsExample'),
|
||||
|
||||
Reference in New Issue
Block a user