Open History to see the last commit
1import * as React from 'react';
2/**
3 * React.Ref uses the readonly type `React.RefObject` instead of
4 * `React.MutableRefObject`, We pretty much always assume ref objects are
5 * mutable (at least when we create them), so this type is a workaround so some
6 * of the weird mechanics of using refs with TS.
7 */
8β
9/**
10 * This is a hack for sure. The thing is, getting a component to intelligently
11 * infer props based on a component or JSX string passed into an `as` prop is
12 * kind of a huge pain. Getting it to work and satisfy the constraints of
13 * `forwardRef` seems dang near impossible. To avoid needing to do this awkward
14 * type song-and-dance every time we want to forward a ref into a component
15 * that accepts an `as` prop, we abstract all of that mess to this function for
16 * the time time being.
17 *
18 * TODO: Eventually we should probably just try to get the type defs above
19 * working across the board, but ain't nobody got time for that mess!
20 *
21 * @param Comp
22 */
23export function forwardRefWithAs(comp) {
24 return React.forwardRef(comp);
25}
26/*
27Test components to make sure our dynamic As prop components work as intended
28type PopupProps = {
29 lol: string;
30 children?: React.ReactNode | ((value?: number) => JSX.Element);
31};
32export const Popup = forwardRefWithAs<PopupProps, 'input'>(
33 ({ as: Comp = 'input', lol, className, children, ...props }, ref) => {
34 return (
35 <Comp ref={ref} {...props}>
36 {typeof children === 'function' ? children(56) : children}
37 </Comp>
38 );
39 }
40);
41export const TryMe1: React.FC = () => {
42 return <Popup as="input" lol="lol" name="me" />;
43};
44export const TryMe2: React.FC = () => {
45 let ref = React.useRef(null);
46 return <Popup ref={ref} as="div" lol="lol" />;
47};
48β
49export const TryMe4: React.FC = () => {
50 return <Popup as={Whoa} lol="lol" test="123" name="boop" />;
51};
52export const Whoa: React.FC<{
53 help?: boolean;
54 lol: string;
55 name: string;
56 test: string;
57}> = props => {
58 return <input {...props} />;
59};
60*/
61// export const TryMe3: React.FC = () => {
62// return <Popup as={Cool} lol="lol" name="me" test="123" />;
63// };
64// let Cool = styled(Whoa)`
65// padding: 10px;
66// `