how to ForwardRef to wrapped component using function component and typescript

Viewed 956

I have problem with forwardRef in Typescript in ReactJS.

My goal is to wrap bootstrap FormControl and provide some simple logic for now on it then build on it. I know that FormControl forward its reference to the input element.

Now how can I forward a reference in my component to this reference that FormControl is forwarding. My code now looks like that:

import React from "react";
import { Form } from 'react-bootstrap';

export type FormSimpleTextProps = {
    name: string;
    placeholder: string;
    value: string;
    updated: boolean;
    editable: boolean;
    onChange: (name: string, newValue: string) => void;
};

export const FormSimpleText = React.forwardRef(
    (props: FormSimpleTextProps, ref: React.Ref<HTMLInputElement>) => {

        const onChangeHandler = (e: React.ChangeEvent<HTMLInputElement>) => {
            console.log(e);
        }

        return (
            <Form.Control

               ref={ref}
                name={props.name}
                value={props.value}
                onChange={onChangeHandler}
                className={props.updated ? "selected" : ""}
                plaintext={!props.editable}
                readOnly={!props.editable}
                required
                type="text"
                placeholder={props.placeholder}
            />
        );
    }
);

At the line with ref={ref} I'm getting error:

No overload matches this call.
  Overload 1 of 2, '(props: Readonly<ReplaceProps<"input", BsPrefixProps<"input"> & FormControlProps>>): FormControl<"input">', gave the following error.
    Type '((instance: HTMLInputElement | null) => void) | RefObject<HTMLInputElement> | null' is not assignable to type '(string & ((instance: HTMLInputElement | null) => void)) | (string & RefObject<HTMLInputElement>) | (((instance: FormControl<"input"> | null) => void) & ((instance: HTMLInputElement | null) => void)) | ... 4 more ... | undefined'.
      Type '(instance: HTMLInputElement | null) => void' is not assignable to type '(string & ((instance: HTMLInputElement | null) => void)) | (string & RefObject<HTMLInputElement>) | (((instance: FormControl<"input"> | null) => void) & ((instance: HTMLInputElement | null) => void)) | ... 4 more ... | undefined'.
        Type '(instance: HTMLInputElement | null) => void' is not assignable to type 'string & ((instance: HTMLInputElement | null) => void)'.
          Type '(instance: HTMLInputElement | null) => void' is not assignable to type 'string'.
  Overload 2 of 2, '(props: ReplaceProps<"input", BsPrefixProps<"input"> & FormControlProps>, context?: any): FormControl<"input">', gave the following error.
    Type '((instance: HTMLInputElement | null) => void) | RefObject<HTMLInputElement> | null' is not assignable to type '(string & ((instance: HTMLInputElement | null) => void)) | (string & RefObject<HTMLInputElement>) | (((instance: FormControl<"input"> | null) => void) & ((instance: HTMLInputElement | null) => void)) | ... 4 more ... | undefined'.
      Type '(instance: HTMLInputElement | null) => void' is not assignable to type '(string & ((instance: HTMLInputElement | null) => void)) | (string & RefObject<HTMLInputElement>) | (((instance: FormControl<"input"> | null) => void) & ((instance: HTMLInputElement | null) => void)) | ... 4 more ... | undefined'.
        Type '(instance: HTMLInputElement | null) => void' is not assignable to type 'string & ((instance: HTMLInputElement | null) => void)'.
          Type '(instance: HTMLInputElement | null) => void' is not assignable to type 'string'.  TS2769

    20 |         return (
    21 |             <Form.Control
  > 22 |                 ref={ref}
       |                 ^
    23 |                 name={props.name}
    24 |                 value={props.value}
    25 |                 onChange={onChangeHandler}

I just can't figure out based on error message what is wrong here. It is too complicated for me. Can somebody help in this example?

0 Answers
Related