/** * Sample React Native App * https://github.com/facebook/react-native * * @format * @flow */ import React, { Component } from "react"; import { Platform, StyleSheet, Text, View, Image, TouchableOpacity, ImageBackground, TextInput, Button, StatusBar, Modal, ScrollView, TouchableHighlight, DeviceEventEmitter, findNodeHandle, UIManager } from "react-native"; import CitysData from "../../data/citys.json"; type Props = {}; export default class GradeSelectionModal extends Component { state = { modalVisible: false, click_index: this.props.grade_index, item_width: 0, item_height: 0, grade_data: [ "一年级", "二年级", "三年级", "四年级", "五年级", "六年级", "七年级", "八年级", "九年级" ] }; render() { return ( { this.setState({ modalVisible: false }); }} > 我所在的年级 {this.centerView()} this.commit()} > 确定 ); } centerView() { return ( this.onLayout(event)} > {this.centerItem()} ); } onLayout(event) { // alert(event.nativeEvent.layout.width); // alert(event.nativeEvent.layout.width / 3); this.setState({ item_width: event.nativeEvent.layout.width / 3.5, item_height: event.nativeEvent.layout.height / 3 }); } centerItem() { let arr = []; for (let i = 0; i < this.state.grade_data.length; i++) { let index = i; let color = ""; if (this.state.click_index == i) { color = "rgba(88, 168, 250, 1)"; } else { color = "rgba(61, 192, 252, 0.15)"; } arr[i] = ( this.clickGrade(index)} > {this.state.grade_data[i]} ); } return arr; } commit() { this.props.commitGrade( this.state.grade_data[this.state.click_index], this.state.click_index ); this.setModalVisible(false); } setModalVisible(visible) { this.setState({ modalVisible: visible }); } clickGrade(index) { this.setState({ click_index: index }); } } const styles = StyleSheet.create({ item_text: { flex: 1, textAlign: "center", textAlignVertical: "center", color: "rgba(58, 58, 58, 1)", fontSize: 16 } });