summaryrefslogtreecommitdiff
path: root/preact/compat/test/ts/forward-ref.tsx
blob: 9a920cb05c598bb528b0c534ae2a33ac0882acdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import React from '../../src';

const MyInput: React.ForwardFn<{ id: string }, { focus(): void }> = (
	props,
	ref
) => {
	const inputRef = React.useRef<HTMLInputElement>(null);

	React.useImperativeHandle(ref, () => ({
		focus: () => {
			if (inputRef.current) {
				inputRef.current.focus();
			}
		}
	}));

	return <input {...props} ref={inputRef} />;
};

export const foo = React.forwardRef(MyInput);

export const Bar = React.forwardRef<HTMLDivElement, { children: any }>(
	(props, ref) => {
		return <div ref={ref}>{props.children}</div>;
	}
);