Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. input.jsx
  2. ---------------
  3.  
  4. import React, { Component } from 'react';
  5.  
  6. const Input = ({ name, label, value, error, onChange }) => {
  7. return (
  8. <div className="form-group">
  9. <label htmlFor={name}>{label}</label>
  10. <input
  11. // autoFocus
  12. // ref={this.username}
  13. value={value}
  14. onChange={onChange}
  15. id={name}
  16. name={name}
  17. type="text"
  18. className="form-control"
  19. />
  20.  
  21. {error && <div className="alert alert-danger">{error}</div>}
  22. </div>
  23. );
  24. }
  25.  
  26. export default Input;
  27.  
  28. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  29.  
  30. loginForm.jsx
  31. ------------------
  32.  
  33. import React, { Component } from 'react';
  34. import Joi from 'joi-browser';
  35. import Input from "./common/input";
  36.  
  37. class LoginForm extends Component {
  38. state = {
  39. account: { username: '', password: '' },
  40. errors: {}
  41. };
  42.  
  43. schema={
  44. username: Joi.string().required().label('Username'),
  45. password: Joi.string().required().label('Password')
  46.  
  47. };
  48.  
  49. validate = () => {
  50. const options= {abortEarly:false};
  51. const {error} = Joi.validate(this.state.account, this.schema, options);
  52.  
  53. if(!error) return null;
  54.  
  55. const errors={};
  56. for (let item of error.details)
  57. errors[item.path[0]]= item.message;
  58.  
  59. return errors;
  60.  
  61. // console.log(result);
  62. // const errors = {};
  63.  
  64. // const { account } = this.state;
  65. // if (account.username.trim() === '')
  66. // errors.username = 'Username is required.';
  67. // if (account.password.trim() === '')
  68. // errors.password = 'Password is required.';
  69.  
  70. // return Object.keys(errors).length === 0 ? null : errors;
  71. };
  72.  
  73. handleSubmit = e => {
  74. e.preventDefault();
  75.  
  76. const errors = this.validate();
  77. this.setState({ errors: errors || {} });
  78. if (errors) return;
  79.  
  80. //Call the server
  81. console.log('Submitted');
  82. };
  83.  
  84. validateProperty= ({name, value}) =>{
  85.  
  86. const obj= {[name]: value};
  87. const schema = {[name] : this.schema[name]};
  88. const {error} = Joi.validate(obj,schema);
  89. return error ? error.details[0].message :null;
  90. // if(!error) return null;
  91. // return error.details[0].message;
  92.  
  93.  
  94. // if(name === 'username'){
  95. // if(value.trim() === '') return 'Username is required.';
  96. // }
  97. // //...
  98. // if(name === 'password'){
  99. // if(value.trim() === '') return 'Password is required.';
  100. // }
  101.  
  102. };
  103.  
  104. handleChange = ({ currentTarget: input }) => {
  105.  
  106. const errors={...this.state.errors};
  107. const errorMessage = this.validateProperty(input) ;
  108. if(errorMessage) errors[input.name] = errorMessage;
  109. else
  110. delete errors[input.name];
  111.  
  112.  
  113. const account = { ...this.state.account };
  114. account[input.name] = input.value;
  115. this.setState({ account, errors });
  116. }
  117.  
  118. render() {
  119. const { account, errors } = this.state;
  120. return (
  121. <div>
  122. <h1>Login</h1>
  123. <form onSubmit={this.handleSubmit} >
  124.  
  125. <Input
  126. name="username"
  127. value={account.username}
  128. label="Username"
  129. onChange={this.handleChange}
  130. error={errors.username}
  131. />
  132.  
  133. <Input
  134. name="password"
  135. value={account.password}
  136. label="Password"
  137. onChange={this.handleChange}
  138. error={errors.password}
  139. />
  140.  
  141.  
  142.  
  143. <button className="btn btn-primary">Login</button>
  144.  
  145. </form>
  146.  
  147. </div>
  148. );
  149. }
  150. }
  151.  
  152. export default LoginForm;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement