SearchDistributor.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import React, {Component} from 'react';
  2. import {
  3. View,
  4. Text,
  5. } from 'react-native';
  6. import {SearchBar, List,} from '@ant-design/react-native';
  7. import {createAction} from "../../utils";
  8. import {connect} from 'react-redux'
  9. import RefreshFlatList from "react-native-refresh-flatlist";
  10. @connect(home => ({...home}))
  11. class SearchDistributor extends Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. hide: false,
  16. };
  17. }
  18. componentDidMount() {
  19. //this._fetchData()
  20. }
  21. componentWillUnmount() {
  22. this.props.dispatch({
  23. type: 'home/clear'
  24. });
  25. }
  26. _onChange = (value) => {
  27. if (value) {
  28. this.props.dispatch(createAction('home/searchDist')({keyword: value}));
  29. this.setState({value: value, hide: false,})
  30. } else {
  31. this.setState({value: '', hide: true,})
  32. }
  33. }
  34. _searchDataPress = (id, name) => {
  35. const {goBack, state} = this.props.navigation;
  36. //在页面返回,卸载时,将上个页面的方法取到,并回传参数,这样回传的参数会重走render方法
  37. state.params.callback({id: id, name: name});
  38. goBack();
  39. }
  40. _keyExtractor = (item, index) => item.id.toString();
  41. _renderItem = (data) => {
  42. const item = data.item;
  43. const dist = item.name + ' - ' + item.tel + ' - ' + item.notes
  44. return (
  45. <List.Item key={item.id} wrap={true}
  46. thumb={<Text style={{fontFamily: 'iconfont', paddingRight: 3}}>{'\ue645'}</Text>}
  47. onPress={() => this._searchDataPress(item.id, item.name)}>{dist}</List.Item>
  48. );
  49. }
  50. render() {
  51. const {searchDistData, searchRState,} = this.props.home;
  52. return (
  53. <View style={{flex: 1}}>
  54. {/*搜索*/}
  55. <SearchBar placeholder="搜索" showCancelButton
  56. cancelText='搜索'
  57. value={this.state.value}
  58. onCancel={(value) => this._onChange(value)}
  59. onSubmit={(value) => this._onChange(value)}
  60. onChange={(value) => this._onChange(value)}
  61. />
  62. <View style={this.state.hide ? {display: 'none',} : {}}>
  63. <RefreshFlatList
  64. data={searchDistData}
  65. renderItem={this._renderItem}
  66. keyExtractor={this._keyExtractor}
  67. refreshState={searchRState}
  68. ItemSeparatorComponent={() => <View style={{height: 0}}/>}
  69. ListHeaderComponent={() => <View style={{height: 0}}/>}
  70. />
  71. </View>
  72. </View>
  73. );
  74. }
  75. }
  76. export default SearchDistributor;