index.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import React, { forwardRef } from 'react';
  2. import { Input, TextArea } from './styled';
  3. type Props = {
  4. id?: string;
  5. name?: string;
  6. onChange?: (val: string) => void;
  7. onBlur?: () => void;
  8. value?: string | number;
  9. defaultValue?: string | number;
  10. placeholder?: string;
  11. disabled?: boolean;
  12. error?: boolean;
  13. shouldFitContainer?: boolean;
  14. variant?: 'standard' | 'multiline';
  15. };
  16. type Ref = any;
  17. const InputBox = forwardRef<Ref, Props>(
  18. (
  19. { variant = 'standard', onChange, disabled = false, ...rest }: Props,
  20. ref
  21. ) => {
  22. const handleChange = (
  23. e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
  24. ): void => {
  25. if (onChange && !disabled) {
  26. onChange(e.target.value);
  27. }
  28. };
  29. return variant === 'standard' ? (
  30. <Input
  31. data-testid="input"
  32. ref={ref}
  33. disabled={disabled}
  34. onChange={handleChange}
  35. {...rest}
  36. />
  37. ) : (
  38. <TextArea
  39. ref={ref}
  40. disabled={disabled}
  41. onChange={handleChange}
  42. {...rest}
  43. />
  44. );
  45. }
  46. );
  47. InputBox.defaultProps = {
  48. id: '',
  49. name: '',
  50. onChange: () => {
  51. // do something
  52. },
  53. onBlur: () => {
  54. // do something
  55. },
  56. value: '',
  57. defaultValue: undefined,
  58. placeholder: '',
  59. disabled: false,
  60. error: false,
  61. shouldFitContainer: false,
  62. variant: 'standard',
  63. };
  64. export default InputBox;