import type { Ref } from "react"; /** * Создает стабильный callback для присвоения DOM-узла в React ref (функцию или объект). * * Дженерик * - По умолчанию `T = HTMLElement` * * Поведение * - Если `ref` является callback-функцией: вызывает ее с переданным узлом. * - Если `ref` является ref-объектом: записывает узел в `ref.current`. * - При falsy-значении `arg` дополнительных действий не выполняется. * * @example * import { useRef } from "react"; * import { setupRef } from "@delement/ui/utils/react/setupRef"; * * const App = () => { * const inputRef = useRef(null); * const setInputRef = setupRef(inputRef); * return * } * @template T - Тип элемента или экземпляра, на который указывает ref. * @param {Ref} ref - Целевой React ref (callback или `RefObject`). * @returns {(arg: T | null) => void} Функция присвоения, подходящая для `ref` в JSX/TSX. */ export const setupRef = (ref: Ref): (arg: T | null) => void => { return (arg: T | null) => { if (typeof ref === "function") { ref(arg); } else if (ref) { ref.current = arg; } }; };