RenamePopup.tsx 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { useEffect, useRef } from 'react';
  2. import useOutsideClick from '../../_shared/useOutsideClick';
  3. export const RenamePopup = ({
  4. value,
  5. onChange,
  6. onClose,
  7. className = '',
  8. }: {
  9. value: string;
  10. onChange: (newTitle: string) => void;
  11. onClose: () => void;
  12. className?: string;
  13. }) => {
  14. const ref = useRef<HTMLDivElement>(null);
  15. const inputRef = useRef<HTMLInputElement>(null);
  16. useOutsideClick(ref, () => onClose && onClose());
  17. useEffect(() => {
  18. if (!inputRef || !inputRef.current) return;
  19. const { current: el } = inputRef;
  20. el.focus();
  21. el.selectionStart = 0;
  22. el.selectionEnd = el.value.length;
  23. }, [inputRef]);
  24. return (
  25. <div
  26. ref={ref}
  27. className={
  28. 'absolute left-[30px] top-[30px] z-10 flex w-[300px] rounded bg-white py-1 px-1.5 shadow-md ' + className
  29. }
  30. >
  31. <input
  32. ref={inputRef}
  33. className={'border-shades-3 flex-1 rounded border bg-main-selector p-1'}
  34. value={value}
  35. onChange={(e) => onChange(e.target.value)}
  36. />
  37. </div>
  38. );
  39. };