Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import {
  3. AppRegistry,
  4. StyleSheet,
  5. Text,
  6. View,
  7. TouchableOpacity,
  8. PixelRatio,
  9. Platform,
  10. ListView,
  11. NativeModules
  12. } from 'react-native';
  13.  
  14. import {AutoGrowingTextInput} from 'react-native-autogrow-textinput';
  15. import {BlurView} from 'react-native-blur';
  16. import {KeyboardAccessoryView, KeyboardUtils} from 'react-native-keyboard-input';
  17. import InvertibleScrollView from 'react-native-invertible-scroll-view';
  18. import './demoKeyboards';
  19.  
  20. const testData = [...Array(100).keys()].map(i => { return {key: i, text: `test${i}`}})
  21. const IsIOS = Platform.OS === 'ios';
  22. const TrackInteractive = true;
  23.  
  24. class AwesomeProject extends Component {
  25. constructor(props) {
  26. super(props);
  27. this.keyboardAccessoryViewContent = this.keyboardAccessoryViewContent.bind(this);
  28. this.onKeyboardItemSelected = this.onKeyboardItemSelected.bind(this);
  29. this.resetKeyboardView = this.resetKeyboardView.bind(this);
  30. this.renderRow = this.renderRow.bind(this);
  31.  
  32. this.dataSource = new ListView.DataSource({
  33. rowHasChanged: (r1, r2) => {
  34. return r1 !== r2;
  35. }
  36. });
  37.  
  38. this.state = {
  39. customKeyboard: {
  40. component: undefined,
  41. initialProps: undefined,
  42. },
  43. receivedKeyboardData: undefined,
  44. };
  45. }
  46.  
  47. onKeyboardItemSelected(keyboardId, params) {
  48. const receivedKeyboardData = `onItemSelected from "${keyboardId}"\nreceived params: ${JSON.stringify(params)}`;
  49. this.setState({receivedKeyboardData});
  50. }
  51.  
  52. getToolbarButtons() {
  53. return [
  54. {
  55. text: 'show1',
  56. onPress: () => this.showKeyboardView('KeyboardView', 'FIRST - 1 (passed prop)'),
  57. },
  58. {
  59. text: 'show2',
  60. onPress: () => this.showKeyboardView('AnotherKeyboardView', 'SECOND - 2 (passed prop)'),
  61. },
  62. {
  63. text: 'reset',
  64. onPress: () => this.resetKeyboardView(),
  65. },
  66. ];
  67. }
  68.  
  69. resetKeyboardView() {
  70. this.setState({customKeyboard: {}});
  71. }
  72.  
  73. showKeyboardView(component, title) {
  74. this.setState({
  75. customKeyboard: {
  76. component,
  77. initialProps: {title},
  78. },
  79. });
  80. }
  81.  
  82. keyboardAccessoryViewContent() {
  83. const InnerContainerComponent = (IsIOS && BlurView) ? BlurView : View;
  84. return (
  85. <InnerContainerComponent blurType="xlight" style={styles.blurContainer}>
  86. <View style={{borderTopWidth: StyleSheet.hairlineWidth, borderColor: '#bbb'}}/>
  87. <View style={styles.inputContainer}>
  88. <AutoGrowingTextInput
  89. maxHeight={200}
  90. style={styles.textInput}
  91. ref={(r) => {
  92. this.textInputRef = r;
  93. }}
  94. placeholder={'Message'}
  95. underlineColorAndroid="transparent"
  96. onFocus={() => this.resetKeyboardView()}
  97. />
  98. <TouchableOpacity style={styles.sendButton} onPress={() => KeyboardUtils.dismiss()}>
  99. <Text>Action</Text>
  100. </TouchableOpacity>
  101. </View>
  102. <View style={{flexDirection: 'row'}}>
  103. {
  104. this.getToolbarButtons().map((button, index) =>
  105. <TouchableOpacity onPress={button.onPress} style={{paddingLeft: 15, paddingBottom: 10}} key={index}>
  106. <Text>{button.text}</Text>
  107. </TouchableOpacity>)
  108. }
  109. </View>
  110. </InnerContainerComponent>
  111. );
  112. }
  113.  
  114. renderRow(rowData) {
  115. return (
  116. <View style={{minHeight: 50, width: 100, backgroundColor: 'blue', borderRadius: 50, marginBottom: 20, alignItems: 'center'}}>
  117. <Text style={{color: 'white'}}>{rowData.text}</Text>
  118. </View>
  119. )
  120. }
  121.  
  122. render() {
  123.  
  124. const initialInputBarHeight = 80;
  125.  
  126. return (
  127. <View style={styles.container}>
  128. <ListView
  129. dataSource={this.dataSource.cloneWithRows(testData)}
  130. renderRow={this.renderRow}
  131. renderScrollComponent={props => <InvertibleScrollView {...props} testID={'list'} inverted={true}/>}
  132. keyboardDismissMode={TrackInteractive ? 'interactive' : 'none'}
  133. contentOffset={{y: -initialInputBarHeight}}
  134. />
  135.  
  136. <KeyboardAccessoryView
  137. renderContent={this.keyboardAccessoryViewContent}
  138. onHeightChanged={height => this.setState({keyboardAccessoryViewHeight: height})}
  139. trackInteractive={TrackInteractive}
  140. kbInputRef={this.textInputRef}
  141. kbComponent={this.state.customKeyboard.component}
  142. kbInitialProps={this.state.customKeyboard.initialProps}
  143. onItemSelected={this.onKeyboardItemSelected}
  144. onKeyboardResigned={this.resetKeyboardView}
  145. iOSScrollBehavior={Platform.OS === 'ios' ? NativeModules.KeyboardTrackingViewManager.KeyboardTrackingScrollBehaviorScrollToBottomInvertedOnly : null}
  146. revealKeyboardInteractive
  147. />
  148. </View>
  149. );
  150.  
  151. }
  152. }
  153.  
  154. const styles = StyleSheet.create({
  155. container: {
  156. flex: 1,
  157. backgroundColor: '#F5FCFF',
  158. },
  159. welcome: {
  160. fontSize: 20,
  161. textAlign: 'center',
  162. margin: 10,
  163. paddingTop: 50,
  164. paddingBottom: 50,
  165. },
  166. inputContainer: {
  167. flexDirection: 'row',
  168. alignItems: 'center',
  169. justifyContent: 'space-between',
  170. },
  171. blurContainer: {
  172. ...Platform.select({
  173. ios: {
  174. flex: 1,
  175. },
  176. }),
  177. },
  178. textInput: {
  179. flex: 1,
  180. marginLeft: 10,
  181. marginTop: 10,
  182. marginBottom: 10,
  183. paddingLeft: 10,
  184. paddingTop: 2,
  185. paddingBottom: 5,
  186. fontSize: 16,
  187. backgroundColor: 'white',
  188. borderWidth: 0.5 / PixelRatio.get(),
  189. borderRadius: 18,
  190. },
  191. sendButton: {
  192. paddingRight: 15,
  193. paddingLeft: 15,
  194. },
  195. });
  196.  
  197. AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement