Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createRef type for FormControl #3568

Closed
hamanovich opened this issue Mar 24, 2019 · 15 comments
Closed

createRef type for FormControl #3568

hamanovich opened this issue Mar 24, 2019 · 15 comments

Comments

@hamanovich
Copy link

I'm using react-bootstrap together with typescript.
What's the proper type in createRef for <Form.Control> element? (instead of any)

private textInput = createRef<any>();

<Form.Control name='textarea_name' as="textarea" ref={this.textInput} />

Once I replace to anything else e.g. , I always see the error in a console like so:
The expected type comes from property 'ref' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<Form<"input">> & Readonly<ReplaceProps<"input", BsPrefixProps<"input"> & FormControlProps>> & Readonly<{ children?: ReactNode; }>...

@taion
Copy link
Member

taion commented Mar 24, 2019

It's whatever the as component is (in this case <textarea>) – though I'm not sure our types are set up quite correctly.

@benkaiser
Copy link

Yeah there's definitely an issue with the types for Form.Control

@kbsanders
Copy link

kbsanders commented Apr 27, 2019

Ran into the same issue.

Until it's fixed, I have found that you can do the following to at least allow your RefObject to maintain a type for usage in the component class:

private textInput = React.createRef<HTMLTextAreaElement>();

<Form.Control name='textarea_name' as="textarea" ref={this.textInput as React.RefObject<any>} />

Note the cast to RefObject<any> inside the ref prop assignment.

@dennis-8
Copy link

dennis-8 commented Dec 13, 2019

This worked for me:

    private commentRef = React.createRef<
        FormControl<"textarea"> & HTMLTextAreaElement
    >();


    public componentDidMount() {
        if (this.commentRef.current) {
            this.commentRef.current.focus();
        }
    }

Also had to change rows in Form.Component as='textarea' from string to number.

@dennis-8
Copy link

Does anyone know if innerRef is working?

https://github.com/react-bootstrap/react-bootstrap/blob/master/types/simple.test.tsx#L211

When I'm usin innerRef I get this error:

React does not recognize the innerRef prop on a DOM element

@Aaronik
Copy link

Aaronik commented Dec 16, 2019

@Denis-N, how did you import that FormControl type from

private commentRef = React.createRef<
        FormControl<"textarea"> & HTMLTextAreaElement
    >();

??

I think if I had that I'd be able to type this correctly, but I can't find where that's exported.

@onichandame
Copy link

@Denis-N, how did you import that FormControl type from

private commentRef = React.createRef<
        FormControl<"textarea"> & HTMLTextAreaElement
    >();

??

I think if I had that I'd be able to type this correctly, but I can't find where that's exported.

I used import FormControl from 'react-bootstrap/FormControl' to import the type FormControl. @Denis-N 's solution works for me.

FYI, the version of react-bootstrap I am using is 4.4.1

@avxkim
Copy link

avxkim commented Feb 15, 2020

Doesn't work with useRef:

const inputLogin = useRef<HTMLInputElement>(null);

And markup:

<Form.Control
  type="text"
  name="login"
  ref={inputLogin as any}
/>

As workaround.

@onichandame
Copy link

onichandame commented Feb 16, 2020

@webcoderkz please try const inputLogin = useRef<HTMLInputElement & FormControl>(null); and JSX <Form.Control ref={inputLogin}/>

@avxkim
Copy link

avxkim commented Feb 16, 2020

@onichandame thanks, it does work. I think react-bootstrap should have some kind of README or guidance on how to type refs with TS.

@dennis-8
Copy link

dennis-8 commented May 5, 2020

In latest release the FormControl has changed.

My solution may not work now.

@AHorak
Copy link

AHorak commented May 13, 2020

@Denis-N have you found a new way?
you wrote:

My solution may not work now.

React.forwardRef<FormControl<ElementType<any>>
this doesn't work any more

@taion
Copy link
Member

taion commented May 19, 2020

consolidating discussion in #5187

@taion taion closed this as completed May 19, 2020
@dennis-8
Copy link

dennis-8 commented Jun 1, 2020

@Denis-N have you found a new way?
you wrote:

My solution may not work now.

React.forwardRef<FormControl<ElementType<any>>
this doesn't work any more

Sorry for long wait. Returned to the project today. This works for me now:

private commentRef = React.createRef<
        HTMLTextAreaElement & FormControl<any>
    >();

@dennis-8
Copy link

dennis-8 commented Jun 2, 2020

Wait, my new solution doesn't work today.

This one works:

private commentRef = React.createRef<
        HTMLTextAreaElement & BsPrefixRefForwardingComponent<"textarea", any>
    >();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants