index.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 TextField = 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. export default TextField;