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

No way to get ref to root of <Input> in a <TextField> #12471

Closed
2 tasks done
dantman opened this issue Aug 10, 2018 · 8 comments
Closed
2 tasks done

No way to get ref to root of <Input> in a <TextField> #12471

dantman opened this issue Aug 10, 2018 · 8 comments
Labels
component: text field This is the name of the generic UI component, not the React module!

Comments

@dantman
Copy link
Contributor

dantman commented Aug 10, 2018

  • This is a v1.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behaviour

<Input> outputs a <div> root that wraps the accents and the input. It should be possible to attach a ref to this input. And it should be possible to do this from a <TextField />.

Current Behaviour

Input only passes {...other} to the <div>

Context

I needed to attach a Popper based menu to the bottom of a TextField, but I need it to open lined up with the whole <Input>, not the <input> (then it's inset by the inputAdornment) and not the <FormControl> (then when the menu is above the input it will appear above the label instead of overlapping the label).

@jedwards1211
Copy link
Contributor

jedwards1211 commented Aug 10, 2018

@dantman have you tried <TextField InputProps={{ref: c => this.inputDiv = findDOMNode(c)}} />? (not sure if it works)

@dantman
Copy link
Contributor Author

dantman commented Aug 10, 2018

@jedwards1211 Yeah, it works. It'll be using that as a workaround for now. But it should be possible to do this without the discouraged findDOMNode. And with the same ease of use as inputRef (i.e. no need to use a callback when you want React.createRef, no need to make an extra findDOMNode call outside passing the ref, and no need to wrap a normal ref callback with more code).

@oliviertassinari
Copy link
Member

oliviertassinari commented Aug 11, 2018

@dantman have you tried <TextField InputProps={{ref: c => this.inputDiv = findDOMNode(c)}} />? (not sure if it works)

@jedwards1211 Yes, it's the encouraging approach to solve the issue until React proposes a better solution. #10825 Won't help for this issue as it targets higher-order components, exposing a rootRef property to all the components doesn't scale.

@dantman
Copy link
Contributor Author

dantman commented Aug 11, 2018

Other MUI components already have an equivalent. e.g. ButtonBase has a ref={buttonRef} on the root element it outputs.

And I doubt that React is going to propose an alternative. Adding ref={rootRef} where it makes sense sounds perfectly reasonable, it's the same level of effort as adding {...other} to those same nodes as you already do.

@oliviertassinari
Copy link
Member

@dantman We do provide a buttonRef and inputRef properties for the DOM elements people are most likely to need (button & input). By the way, we exposes a RootRef component for this use case.

@jedwards1211
Copy link
Contributor

Another way to do it which is questionable but avoids findDOMNode: inputRef={c => this.inputDiv = c.parentElement}

@dantman
Copy link
Contributor Author

dantman commented Aug 11, 2018

RootRef is just findDOMNode in disguise. And it doesn't work in this use case because <TextField /> is in charge of adding the <Input /> I would need to wrap with RootRe.

@oliviertassinari
Copy link
Member

@dantman for advanced use cases, I encourage people to break the TextField abstraction. You can use the lower level components.

@oliviertassinari oliviertassinari added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Dec 20, 2022
@zannager zannager added component: text field This is the name of the generic UI component, not the React module! and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: text field This is the name of the generic UI component, not the React module!
Projects
None yet
Development

No branches or pull requests

4 participants