UNPKG

@acrool/react-hooks

Version:

Common javascript utils methods for project development

28 lines (27 loc) 957 B
import { useRef as v, useState as b, useCallback as l, useEffect as h } from "react"; const p = () => { const e = v(null), [a, n] = b(!1), u = l(() => n(!0), []), s = l(() => n(!1), []); return h(() => { var t, r; return e.current && ((t = e.current) == null || t.addEventListener("focus", u), (r = e.current) == null || r.addEventListener("blur", s)), () => { e.current && (e.current.removeEventListener("focus", u), e.current.removeEventListener("blur", s)); }; }, []), { inputRef: e, focus: (t = 0) => { var o; if (!e.current) return; const r = e.current.value.split(` `), f = t === "last" ? r.length : t, c = r.slice(0, f).reduce((i, d) => i + d.length + 1, 0) + (((o = r[t]) == null ? void 0 : o.length) ?? 0); e.current.focus(), e.current.setSelectionRange(c, c); }, blur: () => { e.current && e.current.blur(); }, isFocus: a }; }; export { p as useTextArea };