mirror of
https://github.com/actualbudget/actual.git
synced 2026-03-11 12:43:09 -05:00
37 lines
886 B
TypeScript
37 lines
886 B
TypeScript
import {
|
|
type ReactElement,
|
|
type Ref,
|
|
cloneElement,
|
|
useEffect,
|
|
useRef,
|
|
} from 'react';
|
|
|
|
type InitialFocusProps = {
|
|
children:
|
|
| ReactElement<{ inputRef: Ref<HTMLInputElement> }>
|
|
| ((node: Ref<HTMLInputElement>) => ReactElement);
|
|
};
|
|
|
|
export function InitialFocus({ children }: InitialFocusProps) {
|
|
const node = useRef<HTMLInputElement>(null);
|
|
|
|
useEffect(() => {
|
|
if (node.current) {
|
|
// This is needed to avoid a strange interaction with
|
|
// `ScopeTab`, which doesn't allow it to be focused at first for
|
|
// some reason. Need to look into it.
|
|
setTimeout(() => {
|
|
if (node.current) {
|
|
node.current.focus();
|
|
node.current.setSelectionRange(0, 10000);
|
|
}
|
|
}, 0);
|
|
}
|
|
}, []);
|
|
|
|
if (typeof children === 'function') {
|
|
return children(node);
|
|
}
|
|
return cloneElement(children, { inputRef: node });
|
|
}
|