Input.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import React, { PureComponent } from 'react';
  2. import { ValidationEvents, ValidationRule } from 'app/types';
  3. import { validate, hasValidationEvent } from 'app/core/utils/validate';
  4. export enum InputStatus {
  5. Invalid = 'invalid',
  6. Valid = 'valid',
  7. }
  8. export enum InputTypes {
  9. Text = 'text',
  10. Number = 'number',
  11. Password = 'password',
  12. Email = 'email',
  13. }
  14. export enum EventsWithValidation {
  15. onBlur = 'onBlur',
  16. onFocus = 'onFocus',
  17. onChange = 'onChange',
  18. }
  19. interface Props extends React.HTMLProps<HTMLInputElement> {
  20. validationEvents?: ValidationEvents;
  21. hideErrorMessage?: boolean;
  22. // Override event props and append status as argument
  23. onBlur?: (event: React.FocusEvent<HTMLInputElement>, status?: InputStatus) => void;
  24. onFocus?: (event: React.FocusEvent<HTMLInputElement>, status?: InputStatus) => void;
  25. onChange?: (event: React.FormEvent<HTMLInputElement>, status?: InputStatus) => void;
  26. }
  27. export class Input extends PureComponent<Props> {
  28. state = {
  29. error: null,
  30. };
  31. get status() {
  32. return this.state.error ? InputStatus.Invalid : InputStatus.Valid;
  33. }
  34. get isInvalid() {
  35. return this.status === InputStatus.Invalid;
  36. }
  37. validatorAsync = (validationRules: ValidationRule[]) => {
  38. return evt => {
  39. const errors = validate(evt.currentTarget.value, validationRules);
  40. this.setState(prevState => {
  41. return {
  42. ...prevState,
  43. error: errors ? errors[0] : null,
  44. };
  45. });
  46. };
  47. };
  48. populateEventPropsWithStatus = (restProps, validationEvents: ValidationEvents) => {
  49. const inputElementProps = { ...restProps };
  50. Object.keys(EventsWithValidation).forEach((eventName: EventsWithValidation) => {
  51. if (hasValidationEvent(eventName, validationEvents) || restProps[eventName]) {
  52. inputElementProps[eventName] = async evt => {
  53. if (hasValidationEvent(eventName, validationEvents)) {
  54. await this.validatorAsync(validationEvents[eventName]).apply(this, [evt]);
  55. }
  56. if (restProps[eventName]) {
  57. restProps[eventName].apply(null, [evt, this.status]);
  58. }
  59. };
  60. }
  61. });
  62. return inputElementProps;
  63. };
  64. render() {
  65. const { validationEvents, className, hideErrorMessage, ...restProps } = this.props;
  66. const { error } = this.state;
  67. const inputClassName = 'gf-form-input' + (this.isInvalid ? ' invalid' : '');
  68. const inputElementProps = this.populateEventPropsWithStatus(restProps, validationEvents);
  69. return (
  70. <div className="our-custom-wrapper-class">
  71. <input {...inputElementProps} className={inputClassName} />
  72. {error && !hideErrorMessage && <span>{error}</span>}
  73. </div>
  74. );
  75. }
  76. }