react-native-navigation的迁移库

SearchScreen.js 2.6KB

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