Advertisement
Guest User

source file

a guest
Apr 16th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Sample React Native App
  3.  * https://github.com/facebook/react-native
  4.  * @flow
  5.  */
  6.  
  7. import React, {Component} from 'react';
  8. import {
  9.   AppRegistry,
  10.   StyleSheet,
  11.   Text,
  12.   TextInput,
  13.   View,
  14.   Alert
  15. } from 'react-native';
  16. import {Button, List, ListItem} from 'react-native-elements';
  17. export default class element_form extends Component {
  18.   constructor(props) {
  19.     super(props);
  20.     this.state = {
  21.       userName: '',
  22.       userPassword: '',
  23.       userNameStyle: styles.inputFormStyle,
  24.       userPasswordStyle: styles.inputFormStyle,
  25.       userNameAutoFocus: true,
  26.       userNameFocus: true,
  27.       userPasswordFocus: false
  28.     }
  29.  
  30.   }
  31.   _registerButtonClicked() {}
  32.   _loginButtonClicked() {
  33.     if (this.state.userName.length == 0) {
  34.       Alert.alert("System", "Have you key the username ?");
  35.       this
  36.         .refs
  37.         .form2
  38.         .refs
  39.         .form2Component
  40.         .focus();
  41.       this.setState({userNameStyle: styles.inputFormStyleError});
  42.       return false;
  43.     } else {
  44.       this.setState({userNameStyle: styles.inputFormStyle});
  45.     }
  46.     if (this.state.userPassword.length == 0) {
  47.       Alert.alert("System", "Have you key the password ?");
  48.       this
  49.         .refs
  50.         .form2
  51.         .refs
  52.         .form2Component
  53.         .focus();
  54.       this.setState({userPasswordStyle: styles.inputFormStyleError})
  55.  
  56.       return false;
  57.     } else {
  58.       this.setState({userPasswordStyle: styles.inputFormStyle})
  59.     }
  60.     //test sample focus Ajax Request register Ajax Request
  61.     var myRequest = new Request('http://172.20.10.2/rn/test.php');
  62.     fetch(myRequest).then(function (response) {
  63.       if (response.status == 200)
  64.         return response.json();
  65.       else
  66.         Alert.alert('System', 'Something went wrong on api server!');
  67.         }
  68.       )
  69.       .then(function (responseJson) {
  70.         me.setState({"welcomeText": responseJson.message});
  71.       })
  72.       .catch(function (error) {
  73.         Alert.alert("System", "error:" + error);
  74.       });
  75.   }
  76.   _resetButtonClicked() {
  77.     //remove focus  not nessary but undocumented.. hmm
  78.  
  79.     this
  80.       .refs
  81.       .form2
  82.       .refs
  83.       .form2Component
  84.       .blur();
  85.  
  86.     console.log(this.refs);
  87.     // console.log(this.refs.form2.form2Component);
  88.     this.setState({userName: ''});
  89.     this.setState({userPassword: ''});
  90.  
  91.     this
  92.       .refs
  93.       .form1
  94.       .refs
  95.       .form1Component
  96.       .focus();
  97.  
  98.   }
  99.   nextFormComponent(event) {
  100.     console.log(this);
  101.     switch (event) {
  102.       case "userPassword":
  103.         this
  104.           .refs
  105.           .form2
  106.           .form2Component
  107.           .focus();
  108.         this.setState({userNameFocus: false, userPasswordFocus: true});
  109.         break;
  110.       case "submit":
  111.         // do nothing ..
  112.         break;
  113.       default:
  114.         // Alert.alert("system", "Unknown Request")
  115.     }
  116.   }
  117.   /**
  118.    * Bind Data . the reason using standard   textInputOnChangeText={(text) => this.setState({text})} not working
  119.    * @param {string} id
  120.    * @param {string} value
  121.    */
  122.   bindData(id, value) {
  123.     // seem pretty odd here cannot work this.setState({idx: value});
  124.     switch (id) {
  125.       case "userName":
  126.         this.setState({"userName": value});
  127.         break;
  128.       case "userPassword":
  129.         this.setState({"userPassword": value});
  130.     }
  131.   }
  132.   render() {
  133.     let leftIconRow1 = {
  134.       name: "user",
  135.       type: "font-awesome",
  136.       color: 'blue'
  137.     }
  138.     let leftIconRow2 = {
  139.       name: "lock",
  140.       type: "font-awesome",
  141.       color: 'blue'
  142.     }
  143.     return (
  144.       <View>
  145.         <Text style={styles.welcome}>
  146.           Login Basic Apps
  147.         </Text>
  148.         <List>
  149.           <ListItem title="row1"/>
  150.           <ListItem title="row2"/>
  151.           <ListItem
  152.             leftIcon={leftIconRow1}
  153.             title="Username"
  154.             textInput={true}
  155.             ref="form1"
  156.             textInputRef="form1Component"
  157.             textInputValue={this.state.userName}
  158.             textInputSecureTextEntry={false}
  159.             textInputOnChangeText={this
  160.             .bindData
  161.             .bind(this, 'userName')}
  162.             textInputAutoFocus={this.state.userNameAutoFocus}
  163.             textInputKeyboardType="default"
  164.             textInputPlaceHolder="Type your Username Here"
  165.             textInputContainerStyle={styles.inputFormStyle}
  166.             textInputOnSubmitEditing={this
  167.             .nextFormComponent
  168.             .bind(this, 'userPassword')}
  169.             textInputReturnKeyType="next"/>
  170.           <ListItem
  171.             title="Password"
  172.             leftIcon={leftIconRow2}
  173.             ref="form2"
  174.             textInput={true}
  175.             textInputRef="form2Component"
  176.             textInputSecureTextEntry={true}
  177.             textInputValue={this.state.userPassword}
  178.             textInputOnChangeText={this
  179.             .bindData
  180.             .bind(this, 'userPassword')}
  181.             textInputFocus={this.state.userPasswordFocus}
  182.             textInputKeyboardType="default"
  183.             textInputPlaceHolder="Type Your Password Here"
  184.             textInputContainerStyle={styles.inputFormStyle}
  185.             textInputOnSubmitEditing={this
  186.             .nextFormComponent
  187.             .bind(this, 'submit')}
  188.             textInputReturnKeyType="done"/>
  189.         </List>
  190.         <Text></Text>
  191.         <Button
  192.           title="L o g i n"
  193.           icon={{
  194.           name: 'sign-in',
  195.           type: 'font-awesome'
  196.         }}
  197.           color="#000000"
  198.           backgroundColor="#2dc1f5"
  199.           onPress={this
  200.           ._loginButtonClicked
  201.           .bind(this)}/>
  202.         <Text></Text>
  203.         <Button
  204.           title="R e g i s t e r"
  205.           icon={{
  206.           name: 'file-o',
  207.           type: 'font-awesome'
  208.         }}
  209.           color="#000000"
  210.           backgroundColor="#51cccc"
  211.           onPress={this
  212.           ._loginButtonClicked
  213.           .bind(this)}/>
  214.         <Text></Text>
  215.         <Button
  216.           title="R e s e t"
  217.           backgroundColor="#3d997a"
  218.           icon={{
  219.           name: 'refresh',
  220.           type: 'font-awesome'
  221.         }}
  222.           onPress={this
  223.           ._resetButtonClicked
  224.           .bind(this)}/>
  225.         <Text></Text>
  226.         <Button
  227.           title="F a c e b o o k"
  228.           backgroundColor="#286666"
  229.           icon={{
  230.           name: 'facebook',
  231.           type: 'font-awesome'
  232.         }}
  233.           onPress={this
  234.           ._resetButtonClicked
  235.           .bind(this)}/>
  236.       </View>
  237.     );
  238.   }
  239.  
  240. };
  241. const styles = StyleSheet.create({
  242.   inputFormStyle: {
  243.     flex: 1
  244.   },
  245.   inputFormStyleError: {
  246.     flex: 1,
  247.     backgroundColor: 'rgba(255, 0, 0, 0.4)'
  248.   },
  249.   inputFormStyleCannotEdit: {
  250.     flex: 1,
  251.     backgroundColor: 'rgba(24,24,24,1)'
  252.   }
  253. });
  254. AppRegistry.registerComponent('element_form', () => element_form);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement