2
0

CustomInput.jsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. export default class CustomInput extends Component {
  4. static propTypes = {
  5. type: PropTypes.string,
  6. inputType: PropTypes.string,
  7. name: PropTypes.string,
  8. value: PropTypes.string,
  9. label: PropTypes.string,
  10. placeholder: PropTypes.string,
  11. customInputEvents: PropTypes.object,
  12. };
  13. static defaultProps = {
  14. type: "text",
  15. inputType: "text",
  16. name: "",
  17. value: "",
  18. label: "",
  19. placeholder: "",
  20. customInputEvents: {},
  21. };
  22. constructor(props) {
  23. super(props);
  24. this.state = {
  25. customInputEvents: props.customInputEvents,
  26. errors: "",
  27. value: props.value,
  28. };
  29. if (this.state.customInputEvents.onBlur) {
  30. const oldOnBlur = this.state.customInputEvents.onBlur;
  31. this.state.customInputEvents.onBlur = () => {
  32. this.onBlurHandler();
  33. oldOnBlur();
  34. };
  35. } else this.state.customInputEvents.onBlur = this.onBlurHandler;
  36. if (this.state.customInputEvents.onChange) {
  37. const oldOnChange = this.state.customInputEvents.onChange;
  38. this.state.customInputEvents.onChange = (event) => {
  39. this.onChangeHandler(event);
  40. oldOnChange(event);
  41. };
  42. } else this.state.customInputEvents.onChange = this.onChangeHandler;
  43. }
  44. onBlurHandler = () => {
  45. this.validateInput();
  46. };
  47. onChangeHandler = (event) => {
  48. this.setState({
  49. value: event.target.value,
  50. });
  51. };
  52. listErrors = () => {
  53. let errors = this.state.errors;
  54. if (errors.length > 0) {
  55. errors = errors.map((error) => {
  56. return (<li>{ error }</li>);
  57. });
  58. return (
  59. <ul className="validation-errors">
  60. { errors }
  61. </ul>
  62. );
  63. } return "";
  64. };
  65. validateInput = () => {
  66. const value = this.state.value;
  67. const type = this.props.type;
  68. const errors = [];
  69. if (type === "email") {
  70. if (value.indexOf("@") === -1) {
  71. errors.push(`${ this.props.label } must have at least one @.`);
  72. } else if (value.lastIndexOf("@") !== value.indexOf("@")) {
  73. errors.push(`${ this.props.label } must not have more than one @.`);
  74. }
  75. } else if (type === "password") {
  76. if (value.length < 4) {
  77. errors.push(`${ this.props.label } must be at least 4 characters long.`);
  78. } else if (value.length > 10) {
  79. errors.push(`${ this.props.label } can't be more than 10 characters long.`);
  80. }
  81. }
  82. this.setState({ errors });
  83. };
  84. render() {
  85. return (
  86. <label htmlFor={ this.props.name }>
  87. <span>{ this.props.label }</span>
  88. <input
  89. placeholder={ this.props.placeholder }
  90. type={ this.props.inputType }
  91. name={ this.props.name }
  92. value={ this.state.value }
  93. className={ (this.state.errors.length > 0) ? "has-validation-errors" : "" }
  94. { ...this.state.customInputEvents }
  95. />
  96. { this.listErrors() }
  97. </label>
  98. );
  99. }
  100. }