Input.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import React from 'react';
  2. import { RegisterOptions, useFormContext } from 'react-hook-form';
  3. import * as S from './Input.styled';
  4. export interface InputProps
  5. extends React.InputHTMLAttributes<HTMLInputElement>,
  6. Omit<S.InputProps, 'hasLeftIcon'> {
  7. name?: string;
  8. hookFormOptions?: RegisterOptions;
  9. leftIcon?: string;
  10. rightIcon?: string;
  11. }
  12. const Input: React.FC<InputProps> = ({
  13. name,
  14. hookFormOptions,
  15. leftIcon,
  16. rightIcon,
  17. inputSize = 'L',
  18. ...rest
  19. }) => {
  20. const methods = useFormContext();
  21. return (
  22. <S.Wrapper>
  23. {leftIcon && (
  24. <S.InputIcon
  25. className={leftIcon}
  26. position="left"
  27. inputSize={inputSize}
  28. />
  29. )}
  30. {name ? (
  31. <S.Input
  32. inputSize={inputSize}
  33. {...methods.register(name, { ...hookFormOptions })}
  34. hasLeftIcon={!!leftIcon}
  35. {...rest}
  36. />
  37. ) : (
  38. <S.Input inputSize={inputSize} hasLeftIcon={!!leftIcon} {...rest} />
  39. )}
  40. {rightIcon && (
  41. <S.InputIcon
  42. className={rightIcon}
  43. position="right"
  44. inputSize={inputSize}
  45. />
  46. )}
  47. </S.Wrapper>
  48. );
  49. };
  50. export default Input;