Advertisement
Guest User

Untitled

a guest
Jun 15th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2. import React, { Component, } from 'react';
  3. import { AppRegistry, Image, StyleSheet, Text, View, ListView, ScrollView, TouchableHighlight, TextInput, StatusBar, Dimensions,} from 'react-native';
  4.  
  5. var styleMultiply = {
  6.  inputRectangel : 0.9,
  7.  inputButton : 0.35,
  8.  registerButton: 0.53,
  9.  marginBetweenButton: 0.02
  10. }
  11.  
  12.  
  13. var LogoShape = React.createClass({
  14.   render(){
  15.     var {width, height} = Dimensions.get('window');
  16.     var minDim = width < height? width : height;
  17.     return (
  18.       <View>
  19.       <Image source={require('./images/cube_shvk.png')}
  20.              style={[{width: minDim*0.63,
  21.                       height: minDim*0.62},
  22.                       styles.cube_shvk]}/>
  23.                       </View>
  24.     );
  25.   }
  26. });
  27.  
  28. var InputField = React.createClass({
  29.  
  30.   getDefaultProps(){
  31.     return {
  32.       autoCapitalize : 'none',
  33.       placeholder : 'Ввод',
  34.       onSubmitEditing : null,
  35.       returnKeyType: 'done',
  36.       refer: ''
  37.     }
  38.   },
  39.   render(){
  40.     var {width, height} = Dimensions.get('window');
  41.     var minDim = width < height? width : height;
  42.     alert(this.props.placeholder + this.props.refer)
  43.     return (
  44.       <View style={[styles.rectangel, {width: width*styleMultiply.inputRectangel}]}>
  45.           <TextInput ref={this.props.refer}
  46.                      style = {styles.inputField}
  47.                      selectTextOnFocus={true}
  48.                      autoCorrect={false}
  49.                      autoCapitalize={this.props.autoCapitalize}
  50.                      placeholder={this.props.placeholder}
  51.                      onSubmitEditing={this.props.onSubmitEditing}
  52.                      returnKeyType={this.props.returnKeyType}/>
  53.       </View>
  54.     );
  55.   }
  56. });
  57.  
  58. var MyButton = React.createClass({
  59.   getDefaultProps(){
  60.     var {width, height} = Dimensions.get('window');
  61.     return {
  62.       width : width*styleMultiply.inputButton,
  63.       marginLeft: 0,
  64.       marginRight: 0,
  65.       text: 'Кнопка',
  66.       onPress: null,
  67.     }
  68.   },
  69.   render(){
  70.     return (
  71.       <TouchableHighlight style={[styles.inputButton,
  72.                                   {width: this.props.width,
  73.                                   marginLeft: this.props.marginLeft,
  74.                                   marginRight: this.props.marginRight}]}
  75.                           onPress={this.props.onPress}
  76.                           activeOpacity={0.9}
  77.                           underlayColor='#3BA67E'>
  78.           <Text style={{color:'black', fontSize: 20}}> {this.props.text}</Text>
  79.       </TouchableHighlight>
  80.     )
  81.   }
  82. });
  83.  
  84. var MainScreen = React.createClass({
  85.   getInitialState(){
  86.     return ({
  87.       registerForm : false,
  88.       userName : '',
  89.       password : '',
  90.       contactInfo : '',
  91.       }
  92.     )
  93.   },
  94.   render(){
  95.     if (this.state.registerForm){
  96.       return this.renderRegisterForm();
  97.     }
  98.     else {
  99.       return this.renderInputForm();
  100.     }
  101.   },
  102.   renderRegisterForm(){
  103.     var {width, height} = Dimensions.get('window');
  104.     return (
  105.       <View style={styles.container}>
  106.           <StatusBar hidden={true}/>
  107.           <Image source={require('./first_background.png')} style={styles.backgroundImage}>
  108.               <LogoShape/>
  109.               <InputField
  110.                           autoCapitalize='words'
  111.                           placeholder='Имя пользователя'
  112.                           onSubmitEditing={this.acceptUsername}
  113.                           returnKeyType='next'/>
  114.               <InputField
  115.                           placeholder='Пароль'
  116.                           onSubmitEditing={this.acceptPassword}
  117.                           returnKeyType='next'/>
  118.               <InputField
  119.                           placeholder='E-mail или телефон'
  120.                           onSubmitEditing={this.acceptContactInfo}/>
  121.               <MyButton marginLeft={16}
  122.                         width={width*styleMultiply.inputRectangel}
  123.                         text='Зарегистрироваться и войти'
  124.                         onPress={this.registrationAndInputPressed}/>
  125.           </Image>
  126.  
  127.       </View>
  128.     )
  129.   },
  130.  
  131.   renderInputForm(){
  132.     var {width, height} = Dimensions.get('window');
  133.     return (
  134.       <View style={styles.container}>
  135.           <StatusBar hidden={true}/>
  136.           <Image  source={require('./images/first_background.png')} style={styles.backgroundImage}>
  137.               <LogoShape/>
  138.               <InputField
  139.                           autoCapitalize='words'
  140.                           placeholder='Имя пользователя'
  141.                           onSubmitEditing={this.acceptUsername}
  142.                           returnKeyType='next'/>
  143.               <InputField refer='textInput'
  144.                           placeholder='Пароль'
  145.                           onSubmitEditing={this.acceptPassword}
  146.                           returnKeyType='next'/>
  147.               <View  style={{flexDirection: 'row'}}>
  148.                   <MyButton marginLeft={16}
  149.                             width={width*styleMultiply.inputButton}
  150.                             text='Вход'
  151.                             onPress={this.inputPressed}/>
  152.                   <MyButton marginRight={16}
  153.                             marginLeft={width*styleMultiply.marginBetweenButton}
  154.                             width={width*styleMultiply.registerButton}
  155.                             text='Регистрация'
  156.                             onPress={()=>{this.setState((state) => {
  157.                               return {
  158.                                 ...state,
  159.                                 registerForm: true,
  160.                                 };
  161.                             })}}/>
  162.  
  163.               </View>
  164.               <TextInput ref='extra' placeholder='extra field'></TextInput>
  165.           </Image>
  166.  
  167.       </View>
  168.     )
  169.   },
  170.  
  171.   inputPressed(){
  172.       alert(' Вход ' + this.state.userName + this.state.password + this.state.contactInfo);
  173.   },
  174.  
  175.   registrationAndInputPressed(){
  176.       alert('Зарегистрирован пользователь: ' + this.state.userName + this.state.password + this.state.contactInfo);
  177.   },
  178.  
  179.   focusNextField(nextField) {
  180.     this.refs.textInput.focus();
  181.   },
  182.  
  183.   acceptUsername(event){
  184.     var text = event.nativeEvent.text;
  185.     this.setState((state) => {
  186.       return {
  187.         ...state,
  188.         userName: text,
  189.         };
  190.     });
  191.     this.focusNextField('2');
  192.   },
  193.  
  194.   acceptPassword(event){
  195.     var text = event.nativeEvent.text;
  196.     this.setState((state) => {
  197.       return {
  198.         ...state,
  199.         password: text,
  200.         };
  201.     });
  202.     if (this.state.registerForm){
  203.       this.focusNextField('3');
  204.     }
  205.   },
  206.  
  207.   acceptContactInfo(event){
  208.     var text = event.nativeEvent.text;
  209.     this.setState((state) => {
  210.       return {
  211.         ...state,
  212.         contactInfo: text,
  213.         };
  214.     });
  215.   }
  216. });
  217.  
  218. const styles = StyleSheet.create({
  219.   inputButton: {
  220.     height : 40,
  221.     borderRadius: 25,
  222.     backgroundColor : '#37BD8A',
  223.     elevation : 10,
  224.     alignSelf : 'flex-start',
  225.     justifyContent: 'center',
  226.     alignItems: 'center',
  227.     paddingBottom: 4,
  228.     paddingRight: 4,
  229.     marginBottom: 10,
  230.     marginLeft: 16,
  231.     marginRight: 16,
  232.   },
  233.   cube_shvk: {
  234.     margin: 30,
  235.     opacity: 0.77,
  236.   },
  237.   rectangel: {
  238.     height : 40,
  239.     borderRadius: 25,
  240.     backgroundColor : 'white',
  241.     elevation : 10,
  242.     alignSelf : 'center',
  243.     justifyContent: 'center',
  244.     alignItems: 'center',
  245.     marginBottom: 10,
  246.     marginLeft: 16,
  247.     marginRight: 16,
  248.   },
  249.   inputField: {
  250.     fontSize : 20,
  251.     marginLeft: 20,
  252.     color: 'black',
  253.     textAlignVertical: 'center',
  254.   },
  255.   backgroundImage:{
  256.     flex: 1,
  257.     alignItems: 'center',
  258.     flexDirection: 'column',
  259.     justifyContent : 'flex-start',
  260.     paddingTop: 36,
  261.   },
  262.   container: {
  263.     flex: 1,
  264.     flexDirection: 'row',
  265.     justifyContent: 'center',
  266.     alignItems: 'center',
  267.     backgroundColor: '#153D70',
  268.   },
  269. });
  270.  
  271. AppRegistry.registerComponent('AwesomeProject', () => MainScreen);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement