PlaceDeparture.js 2.98 KB
/**
 * Created by yzdd on 2018/3/17.
 */
import React, {Component} from 'react';
import {
  View,
  Text,
  StyleSheet,
  Image,
  TextInput,
  FlatList,
  ScrollView,
  TouchableOpacity
} from 'react-native';
import SearchBox from "../components/SearchBox";
import region from '../utils/region';
import {observer} from 'mobx-react';
import {observable} from 'mobx';
import {width} from "../utils/publiscStyle";

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#FFFFFF"
  },
  placeItem: {
    width,
    height: 44,
    flexDirection: "row",
    alignItems: 'center',
    paddingLeft: 15
  }
});
//出发地
@observer
export default class PlaceDeparture extends Component {
  static navigationOptions = ({navigation}) => {
    return {
      title: navigation.state.params.title
    }
  };

  @observable
  place = "";

  @observable
  placeArray = [];

  componentWillMount() {
    console.log(region)
  }

  changePlace = (text) => {
    this.place = text;
    this.timer = setTimeout(() => {
      let array = [];
      region.forEach((v, i) => {
        let timeArray = [];
        if (v.type === "district" || v.type === "city") {
          if (v.name.indexOf(text) !== -1) {
            timeArray.push(v);
            array.push(timeArray)
          }
        }

      });
      array.forEach((item, index) => {
        if (item[0].type === "city") {
          region.forEach((v, i) => {
            if (v.type === "province" && v.id.toString() === item[0].pid) {
              item.push(v);
            }
          });
        } else {
          region.forEach((v, i) => {
            if (v.type === "city" && v.id.toString() === item[0].pid) {
              item.push(v);
              region.forEach((province, pid) => {
                if (province.type === "province" && province.id.toString() === item[1].pid) {
                  item.push(province);
                }
              });
            }
          });
        }
      });
      this.placeArray.replace(array);
    }, 500);
  };
  //选择地区
  selectPlace = (v) => {
    const {goBack} = this.props.navigation;
    const {type, detailItem} = this.props.navigation.state.params;
    if (type === 1) {
      detailItem.startPlace = v;
    } else {
      detailItem.endPlace = v;
    }
    goBack();
  };

  renderPlaceItem = (v, i) => {
    let s = "";
    v.forEach((item, index) => {
      s += `${item.name} `
    });
    return (
      <TouchableOpacity key={i} style={styles.placeItem} onPress={() => this.selectPlace(v)}>
        <Text>{s}</Text>
      </TouchableOpacity>
    )
  };

  render() {
    return (
      <View style={styles.container}>
        <SearchBox
          placeholder={`输入出发地,如"北京"`}
          icon={true}
          onChangeText={(text) => this.changePlace(text)}
          value={this.place}
          autoFocus
        />
        <ScrollView>
          {
            this.placeArray.map((v, i) => this.renderPlaceItem(v, i))
          }
        </ScrollView>
      </View>
    );
  }
}