react-native/RNTester/js/InputAccessoryViewExample.js
Rick Hanlon bd32234e6e Add flow types RNTester examples (#22829)
Summary:
This PR adds flow types for the RNTester examples, and updates all of the RNTester examples to match the flow type consistently.

Previously, there was a mix of static class definitions and whether or not pages exported examples or a component. Now we will always export the same way, enforced by flow types

Note: I also fixed most of the $FlowFixMe in changed components
Pull Request resolved: https://github.com/facebook/react-native/pull/22829

Reviewed By: cpojer

Differential Revision: D13563191

Pulled By: rickhanlonii

fbshipit-source-id: b697e3346a863d1b130881592b0522a96c202b63
2018-12-31 08:30:54 -08:00

121 lines
2.5 KiB
JavaScript

/**
* 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.
*
* @flow
* @format
*/
'use strict';
const React = require('React');
const ReactNative = require('react-native');
const {
Alert,
Button,
InputAccessoryView,
ScrollView,
StyleSheet,
Text,
TextInput,
View,
} = ReactNative;
type MessageProps = $ReadOnly<{||}>;
class Message extends React.PureComponent<MessageProps> {
render() {
return (
<View style={styles.textBubbleBackground}>
<Text style={styles.text}>Text Message</Text>
</View>
);
}
}
type TextInputProps = $ReadOnly<{||}>;
type TextInputState = {|text: string|};
class TextInputBar extends React.PureComponent<TextInputProps, TextInputState> {
state = {text: ''};
render() {
return (
<View style={styles.textInputContainer}>
<TextInput
style={styles.textInput}
onChangeText={text => {
this.setState({text});
}}
value={this.state.text}
placeholder={'Type a message...'}
/>
<Button
onPress={() => {
Alert.alert('You tapped the button!');
}}
title="Send"
/>
</View>
);
}
}
const BAR_HEIGHT = 44;
type InputAccessoryProps = $ReadOnly<{||}>;
class InputAccessoryViewExample extends React.Component<InputAccessoryProps> {
render() {
return (
<>
<ScrollView style={styles.fill} keyboardDismissMode="interactive">
{Array(15)
.fill()
.map((_, i) => <Message key={i} />)}
</ScrollView>
<InputAccessoryView backgroundColor="#fffffff7">
<TextInputBar />
</InputAccessoryView>
</>
);
}
}
const styles = StyleSheet.create({
fill: {
flex: 1,
},
textInputContainer: {
flexDirection: 'row',
alignItems: 'center',
borderTopWidth: 1,
borderTopColor: '#eee',
height: BAR_HEIGHT,
},
textInput: {
flex: 1,
paddingLeft: 10,
},
text: {
padding: 10,
color: 'white',
},
textBubbleBackground: {
backgroundColor: '#2f7bf6',
borderRadius: 20,
width: 110,
margin: 20,
},
});
exports.title = '<InputAccessoryView>';
exports.description =
'Example showing how to use an InputAccessoryView to build an iMessage-like sticky text input';
exports.examples = [
{
title: 'Simple view with sticky input',
render: function(): React.Node {
return <InputAccessoryViewExample />;
},
},
];