You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
840 B

  1. import { forwardRef, useEffect, useImperativeHandle, useRef, InputHTMLAttributes } from 'react';
  2. export default forwardRef(function TextInput(
  3. { type = 'text', className = '', isFocused = false, ...props }: InputHTMLAttributes<HTMLInputElement> & { isFocused?: boolean },
  4. ref
  5. ) {
  6. const localRef = useRef<HTMLInputElement>(null);
  7. useImperativeHandle(ref, () => ({
  8. focus: () => localRef.current?.focus(),
  9. }));
  10. useEffect(() => {
  11. if (isFocused) {
  12. localRef.current?.focus();
  13. }
  14. }, []);
  15. return (
  16. <input
  17. {...props}
  18. type={type}
  19. className={
  20. 'border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm ' +
  21. className
  22. }
  23. ref={localRef}
  24. />
  25. );
  26. });