react-native-navigation的迁移库

SearchScreen.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const React = require('react');
  2. const { Component } = require('react');
  3. const {
  4. StyleSheet,
  5. FlatList,
  6. View,
  7. Button,
  8. Text,
  9. Platform
  10. } = require('react-native');
  11. const { Navigation } = require('react-native-navigation');
  12. const testIDs = require('../testIDs');
  13. const ITEMS = [...Array(200).keys()].map(key => ({ key: `Item ${key}` }));
  14. class SearchControllerScreen extends Component {
  15. static get options() {
  16. return {
  17. topBar: {
  18. title: {
  19. text: 'Search'
  20. },
  21. largeTitle: {
  22. visible: true
  23. },
  24. searchBar: true,
  25. searchBarHiddenWhenScrolling: true,
  26. translucent: true,
  27. searchBarPlaceholder: 'Start Typing'
  28. }
  29. };
  30. }
  31. constructor(props) {
  32. super(props);
  33. this.state = {
  34. query: ''
  35. };
  36. Navigation.events().bindComponent(this);
  37. }
  38. filteredData() {
  39. return ITEMS.filter(
  40. item =>
  41. this.state.query.length === 0 || item.key.indexOf(this.state.query) > -1
  42. );
  43. }
  44. highlight(text, query) {
  45. if (query.length > 0 && text.indexOf(query) > -1) {
  46. const before = text.split(query)[0];
  47. const after = text.split(query)[1];
  48. return (
  49. <Text>
  50. <Text>{before}</Text>
  51. <Text style={{ backgroundColor: 'yellow' }}>{query}</Text>
  52. <Text>{after}</Text>
  53. </Text>
  54. );
  55. }
  56. return text;
  57. }
  58. render() {
  59. return (
  60. <FlatList
  61. testID={testIDs.SCROLLVIEW_ELEMENT}
  62. data={this.filteredData()}
  63. contentContainerStyle={styles.contentContainer}
  64. renderItem={({ item }) => (
  65. <View style={styles.row}>
  66. <Text style={styles.rowText} testID={testIDs.SEARCH_RESULT_ITEM}>
  67. {this.highlight(item.key, this.state.query)}
  68. </Text>
  69. </View>
  70. )}
  71. />
  72. );
  73. }
  74. searchBarUpdated({text, isFocused}) {
  75. this.setState({ query: text, isFocused });
  76. }
  77. }
  78. module.exports = SearchControllerScreen;
  79. const styles = StyleSheet.create({
  80. contentContainer: {},
  81. row: {
  82. height: 50,
  83. padding: 20,
  84. justifyContent: 'center'
  85. },
  86. rowText: {
  87. fontSize: 18
  88. }
  89. });