Input.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.target.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. evt.persist(); // Needed for async. https://reactjs.org/docs/events.html#event-pooling
  54. if (hasValidationEvent(eventName, validationEvents)) {
  55. await this.validatorAsync(validationEvents[eventName]).apply(this, [evt]);
  56. }
  57. if (restProps[eventName]) {
  58. restProps[eventName].apply(null, [evt, this.status]);
  59. }
  60. };
  61. }
  62. });
  63. return inputElementProps;
  64. };
  65. render() {
  66. const { validationEvents, className, hideErrorMessage, ...restProps } = this.props;
  67. const { error } = this.state;
  68. const inputClassName = 'gf-form-input' + (this.isInvalid ? ' invalid' : '') + ' ' + className;
  69. const inputElementProps = this.populateEventPropsWithStatus(restProps, validationEvents);
  70. return (
  71. <div className="our-custom-wrapper-class">
  72. <input {...inputElementProps} className={inputClassName} />
  73. {error && !hideErrorMessage && <span>{error}</span>}
  74. </div>
  75. );
  76. }
  77. }