Advertisement
Guest User

Untitled

a guest
May 24th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow
  9. */
  10.  
  11. 'use strict';
  12.  
  13. const React = require('react');
  14. const {
  15. Alert,
  16. Animated,
  17. Button,
  18. StyleSheet,
  19. Text,
  20. View,
  21. TextInput,
  22. AppRegistry,
  23. } = require('react-native');
  24.  
  25. const {
  26. HeaderComponent,
  27. FooterComponent,
  28. ItemComponent,
  29. PlainInput,
  30. SeparatorComponent,
  31. Spindicator,
  32. genItemData,
  33. pressItem,
  34. renderSmallSwitchOption,
  35. renderStackedItem,
  36. } = require('./ListExampleShared');
  37.  
  38. const renderSectionHeader = ({section}) => (
  39. <View style={styles.header}>
  40. <Text style={styles.headerText}>SECTION HEADER: {section.key}</Text>
  41. <SeparatorComponent />
  42. </View>
  43. );
  44.  
  45. const CustomSeparatorComponent = () => (
  46. <View style={[styles.customSeparator]} />
  47. );
  48.  
  49. class SectionListExample extends React.PureComponent<{}, $FlowFixMeState> {
  50. state = {
  51. sectionIndex: '',
  52. itemIndex: '',
  53. };
  54.  
  55. _sectionListRef: Animated.SectionList;
  56. _captureRef = ref => {
  57. this._sectionListRef = ref;
  58. };
  59.  
  60. _scrollToLocation(sectionIndex: number, itemIndex: number) {
  61. this._sectionListRef
  62. .getNode()
  63. .scrollToLocation({sectionIndex, itemIndex, viewPosition: 0});
  64. }
  65.  
  66. scroll = () => {
  67. const {sectionIndex, itemIndex} = this.state;
  68. this._scrollToLocation(parseInt(sectionIndex), parseInt(itemIndex));
  69. };
  70.  
  71. render() {
  72. return (
  73. <>
  74. <TextInput
  75. style={{
  76. marginTop: 40,
  77. height: 40,
  78. borderColor: 'gray',
  79. borderWidth: 1,
  80. }}
  81. onChangeText={sectionIndex => this.setState({sectionIndex})}
  82. value={this.state.sectionIndex}
  83. placeholder="section index"
  84. keyboardType="number-pad"
  85. />
  86. <TextInput
  87. style={{height: 40, borderColor: 'gray', borderWidth: 1}}
  88. onChangeText={itemIndex => this.setState({itemIndex})}
  89. value={this.state.itemIndex}
  90. placeholder="item index"
  91. keyboardType="number-pad"
  92. />
  93. <Text onPress={this.scroll}>scroll</Text>
  94. <Animated.SectionList
  95. ref={this._captureRef}
  96. renderItem={this._renderItemComponent}
  97. style={styles.list}
  98. stickySectionHeadersEnabled={false}
  99. // ItemSeparatorComponent={CustomSeparatorComponent}
  100. renderItem={({item, index, section}) => (
  101. <Text key={index} style={styles.itemStyle}>
  102. {item}
  103. </Text>
  104. )}
  105. renderSectionHeader={({section: {title}}) => (
  106. <Text style={styles.headerStyle}>{title}</Text>
  107. )}
  108. sections={[
  109. {title: 'section0', data: ['item1', 'item2']},
  110. {title: 'section1', data: ['item3', 'item4']},
  111. {title: 'section2', data: ['item5', 'item6']},
  112. {title: 'section3', data: ['item7', 'item8']},
  113. {title: 'section4', data: ['item9', 'item10']},
  114. ]}
  115. keyExtractor={(item, index) => item + index}
  116. />
  117. </>
  118. );
  119. }
  120.  
  121. _renderItemComponent = ({item, separators}) => (
  122. <ItemComponent
  123. item={item}
  124. onPress={this._pressItem}
  125. onHideUnderlay={separators.unhighlight}
  126. onShowUnderlay={separators.highlight}
  127. />
  128. );
  129.  
  130. _pressItem = (key: string) => {
  131. !isNaN(key) && pressItem(this, key);
  132. };
  133. }
  134.  
  135. const styles = StyleSheet.create({
  136. customSeparator: {
  137. backgroundColor: 'blue',
  138. height: 5,
  139. },
  140. header: {
  141. backgroundColor: '#e9eaed',
  142. },
  143. headerText: {
  144. padding: 4,
  145. fontWeight: '600',
  146. },
  147. list: {
  148. backgroundColor: 'white',
  149. },
  150. scrollToRow: {
  151. flexDirection: 'row',
  152. alignItems: 'center',
  153. paddingHorizontal: 8,
  154. },
  155. separatorText: {
  156. color: 'gray',
  157. alignSelf: 'center',
  158. fontSize: 7,
  159. },
  160. itemStyle: {
  161. height: 100,
  162. },
  163. headerStyle: {
  164. fontWeight: 'bold',
  165. height: 25,
  166. },
  167. });
  168.  
  169. AppRegistry.registerComponent('RNTesterApp', () => SectionListExample);
  170.  
  171. module.exports = SectionListExample;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement