react-native-navigation的迁移库

SearchScreen.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 SearchControllerScreen extends Component {
  19. static get 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. }
  34. };
  35. }
  36. constructor(props) {
  37. super(props);
  38. this.state = {
  39. query: ''
  40. };
  41. Navigation.events().bindComponent(this);
  42. }
  43. filteredData() {
  44. return ITEMS.filter(
  45. item =>
  46. this.state.query.length === 0 || item.key.indexOf(this.state.query) > -1
  47. );
  48. }
  49. highlight(text, query) {
  50. if (query.length > 0 && text.indexOf(query) > -1) {
  51. const before = text.split(query)[0];
  52. const after = text.split(query)[1];
  53. return (
  54. <Text>
  55. <Text>{before}</Text>
  56. <Text style={{ backgroundColor: 'yellow' }}>{query}</Text>
  57. <Text>{after}</Text>
  58. </Text>
  59. );
  60. }
  61. return text;
  62. }
  63. onItemPressed = () => {
  64. Navigation.push(this.props.componentId, {
  65. component: {
  66. name: 'navigation.playground.PushedScreen',
  67. options: {
  68. topBar: {
  69. title: {
  70. text: 'PushedScreen'
  71. },
  72. largeTitle: {
  73. visible: true
  74. },
  75. background: {
  76. translucent: true
  77. }
  78. }
  79. }
  80. },
  81. });
  82. }
  83. render() {
  84. return (
  85. <FlatList
  86. testID={testIDs.SCROLLVIEW_ELEMENT}
  87. data={this.filteredData()}
  88. contentContainerStyle={styles.contentContainer}
  89. renderItem={({ item }) => (
  90. <TouchableOpacity style={styles.row} onPress={this.onItemPressed}>
  91. <Text style={styles.rowText} testID={testIDs.SEARCH_RESULT_ITEM}>
  92. {this.highlight(item.key, this.state.query)}
  93. </Text>
  94. </TouchableOpacity>
  95. )}
  96. />
  97. );
  98. }
  99. searchBarUpdated({text, isFocused}) {
  100. this.setState({ query: text, isFocused });
  101. }
  102. }
  103. module.exports = SearchControllerScreen;
  104. const styles = StyleSheet.create({
  105. contentContainer: {},
  106. row: {
  107. height: 60,
  108. padding: 15,
  109. justifyContent: 'center'
  110. },
  111. rowText: {
  112. fontSize: 18
  113. }
  114. });