react-native-navigation的迁移库

SearchScreen.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const React = require('react');
  2. const { Component } = require('react');
  3. const {
  4. StyleSheet,
  5. FlatList,
  6. View,
  7. Button,
  8. Text,
  9. TouchableOpacity,
  10. Platform
  11. } = require('react-native');
  12. const { Navigation } = require('react-native-navigation');
  13. const testIDs = require('../testIDs');
  14. const ITEMS = [];
  15. for(let i = 0; i < 200; i++) {
  16. ITEMS.push({key: `Item ${i}`});
  17. }
  18. class SearchScreen extends Component {
  19. static options() {
  20. return {
  21. topBar: {
  22. title: {
  23. text: 'Search'
  24. },
  25. largeTitle: {
  26. visible: true
  27. },
  28. searchBar: true,
  29. background: {
  30. translucent: true
  31. },
  32. searchBarPlaceholder: 'Start Typing',
  33. hideNavBarOnFocusSearchBar: false
  34. }
  35. };
  36. }
  37. constructor(props) {
  38. super(props);
  39. this.state = {
  40. query: ''
  41. };
  42. Navigation.events().bindComponent(this);
  43. }
  44. filteredData() {
  45. return ITEMS.filter(
  46. item =>
  47. this.state.query.length === 0 || item.key.indexOf(this.state.query) > -1
  48. );
  49. }
  50. highlight(text, query) {
  51. if (query.length > 0 && text.indexOf(query) > -1) {
  52. const before = text.split(query)[0];
  53. const after = text.split(query)[1];
  54. return (
  55. <Text>
  56. <Text>{before}</Text>
  57. <Text style={{ backgroundColor: 'yellow' }}>{query}</Text>
  58. <Text>{after}</Text>
  59. </Text>
  60. );
  61. }
  62. return text;
  63. }
  64. onItemPressed = () => {
  65. Navigation.push(this.props.componentId, {
  66. component: {
  67. name: 'navigation.playground.PushedScreen',
  68. options: {
  69. topBar: {
  70. title: {
  71. text: 'PushedScreen'
  72. },
  73. largeTitle: {
  74. visible: true
  75. },
  76. background: {
  77. translucent: true
  78. }
  79. }
  80. }
  81. },
  82. });
  83. }
  84. render() {
  85. return (
  86. <FlatList
  87. testID={testIDs.SCROLLVIEW_ELEMENT}
  88. data={this.filteredData()}
  89. contentContainerStyle={styles.contentContainer}
  90. renderItem={({ item }) => (
  91. <TouchableOpacity style={styles.row} onPress={this.onItemPressed}>
  92. <Text style={styles.rowText} testID={testIDs.SEARCH_RESULT_ITEM}>
  93. {this.highlight(item.key, this.state.query)}
  94. </Text>
  95. </TouchableOpacity>
  96. )}
  97. />
  98. );
  99. }
  100. searchBarUpdated({text, isFocused}) {
  101. this.setState({ query: text, isFocused });
  102. }
  103. }
  104. module.exports = SearchScreen;
  105. const styles = StyleSheet.create({
  106. contentContainer: {},
  107. row: {
  108. height: 60,
  109. padding: 15,
  110. justifyContent: 'center'
  111. },
  112. rowText: {
  113. fontSize: 18
  114. }
  115. });