Skip to content

Instantly share code, notes, and snippets.

@jarakys
Created November 11, 2018 21:19
Show Gist options
  • Select an option

  • Save jarakys/ca32fbd2b3c27180ce0d3a94fae25e75 to your computer and use it in GitHub Desktop.

Select an option

Save jarakys/ca32fbd2b3c27180ce0d3a94fae25e75 to your computer and use it in GitHub Desktop.
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, {Component} from 'react';
import {Platform, StyleSheet,TouchableHighlight, Text,TextInput,FlatList,Keyboard, View,KeyboardAvoidingView} from 'react-native';
import ChatItem from './src/ChatItem'
export default class App extends Component {
constructor()
{
super();
this.state={
text:'',
refresh:false,
messages:[
{
authorName:"asd",
text: "hello"
},
{
authorName:"aaa",
text: "azaaza"
},
{
authorName:'sdsds',
text: "1234567891234567891234567891234567891234567891234567891234567891234567891234567899123456789123456789123456789123456789123456789123456789123456789.0"
}
]
}
}
_onSendData()
{
const messages = this.state.messages;
const newMessage ={
text:this.state.text,
authorName:"qqq"
};
messages.unshift(newMessage);
const refresh = !this.state.refresh
this.setState({refresh});
this.textInput.clear();
Keyboard.dismiss();
}
renderMessageItem({item}){
return <ChatItem message ={item}></ChatItem>
}
keyExtractor = (item,index)=> index.toString();
render() {
return (
<View style={styles.container}>
<FlatList
data={this.state.messages}
extraData={this.state.refresh}
renderItem={this.renderMessageItem}
keyExtractor={this.keyExtractor}
inverted
>
</FlatList>
<KeyboardAvoidingView >
<View style={styles.inputLayout}>
<TextInput
style={styles.textBox}
multiline
onChangeText={(text)=>this.setState({text})}
ref={input=>{this.textInput=input;}}
>
</TextInput>
<TouchableHighlight
underlayColor='#0066ff'
style={styles.sendBtn}
onPress={this._onSendData.bind(this)}
>
<Text>Send</Text>
</TouchableHighlight>
</View>
</KeyboardAvoidingView>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
},
inputLayout: {
flexDirection:'row',
padding:10,
justifyContent:'space-between'
},
textBox: {
flex: 1,
borderColor:'black',
borderWidth:1,
borderRadius:5,
fontSize:14,
paddingHorizontal:10,
paddingVertical:5
},
sendBtn:
{
justifyContent:'center',
backgroundColor:'#66ccff',
borderRadius:5,
marginLeft: 10,
paddingRight:15,
paddingLeft:15
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment