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

[TextField] add ref callback #6893

Closed
jgoux opened this issue May 18, 2017 · 16 comments
Closed

[TextField] add ref callback #6893

jgoux opened this issue May 18, 2017 · 16 comments
Labels
bug 🐛 Something doesn't work component: text field This is the name of the generic UI component, not the React module!

Comments

@jgoux
Copy link
Contributor

jgoux commented May 18, 2017

Problem description

The TextField component ref's callback returns null instead of the root node of the component.

More globally, it would be great to have this callback on every Material UI's components.

Link to minimal working code that reproduces the issue

https://codesandbox.io/s/kzJWNlX5

Versions

  • Material-UI: 1.0.0-alpha.14
  • React: 15.5.3
  • Browser: Chrome 58 on Windows 10 x64
@jgoux jgoux changed the title (next) [TextField] add ref callback [TextField] add ref callback May 18, 2017
@oliviertassinari oliviertassinari added next bug 🐛 Something doesn't work component: text field This is the name of the generic UI component, not the React module! labels May 18, 2017
@oliviertassinari
Copy link
Member

oliviertassinari commented May 20, 2017

This behaviour is explained by the fact that TextField is a stateless functional component. What's the use case for that reference? I think that good rule of thumb is to add a rootRef callback property whenever needed. Also, we could add a inputRef callback property to access the underlying input.

@bnopacheco
Copy link

bnopacheco commented May 20, 2017

Following the React docs... works for me, but I get warnings.
https://facebook.github.io/react/docs/refs-and-the-dom.html

Is there any advantage in using Ref or onChange methods?

// CustomTextField component
import React, { Component } from 'react'
import TextField from 'material-ui/TextField'

export default (props) => (
    <div>
        <TextField {...props} ref={props.inputRef}  />
    </div>
)
import React, {Component} from 'react'
import CustomTextField as TextField from '../TextField'


class LoginBox extends Component {

    constructor() {
        super()
    }

    send(event) {
        event.preventDefault()
        console.log(this.email.input.value
        console.log(this.password.input.value})
    }

    render() {
        return (

                <div>

                    <form onSubmit={this.send.bind(this)}>
                        <div>
                            <TextField
                                name="email"
                                type="email"
                                inputRef={el => this.email = el}
                            />

                            <TextField
                                name="password"
                                type="password"
                                inputRef={el => this.password = el}}
                            />
                            <input type="submit" value="Send" />

                        </div>
                    </form>
                </div>
        );
    }
}

warning.js?9398f22:36 Warning: Unknown prop inputRef on tag. Remove this prop from the element. For details, see https://fb.me/react-unknown-prop

"material-ui": "^0.18.1",
"react": "^15.5.4",
"react-dom": "^15.5.4"

@jgoux
Copy link
Contributor Author

jgoux commented May 21, 2017

@maisumbruno My issue concerns the next branch.
@oliviertassinari I wanted to use it for my Autocomplete component which doesn't work anymore without the ref.

@oliviertassinari
Copy link
Member

@jgoux What about?

<TextField
  inputProps={{
    ref: (node) => { console.log('node', node); },
  }}
/>

@oliviertassinari
Copy link
Member

Well, that won't work yet. I'm on it.

@oliviertassinari
Copy link
Member

oliviertassinari commented May 21, 2017

I think that exposing a inputRef property would be more appropriate as React is handling ref differently from the other properties. I have been iterating with the previous suggesting, but it's quite tricky.

@Nagle
Copy link

Nagle commented May 21, 2017

@jgoux Which Autocomplete component?

@jgoux
Copy link
Contributor Author

jgoux commented May 31, 2017

Thanks for the inputRef addition!

I'm still missing the ref to the root node of TextField. 😄

@Nagle This one : https://codesandbox.io/s/oQ0nkl5pk

@oliviertassinari
Copy link
Member

You can try innerRef of withStyles().

@jgoux
Copy link
Contributor Author

jgoux commented May 31, 2017

innerRef did the trick! 👍

I'm curious, why adding a new prop like innerRef instead of making the component a class and relying of React's native ref ?

@oliviertassinari
Copy link
Member

oliviertassinari commented May 31, 2017

@jgoux The component is already a class so you can always get a ref.

@jgoux
Copy link
Contributor Author

jgoux commented May 31, 2017

I'm not sure I follow.
TextField seems to be a functional component : https://github.com/callemall/material-ui/blob/next/src/TextField/TextField.js#L9

I tried adding a ref callback to it first, but it always return null

@oliviertassinari
Copy link
Member

oliviertassinari commented May 31, 2017

I tried adding a ref callback to it first, but it always return null

Functional components do not support ref callback.

@kushal-tech
Copy link

Use can do this by using inputRef
Look at the my answer
https://stackoverflow.com/questions/31446751/how-to-get-password-field-value-in-reacts-material-ui/47329368#47329368

@Girijashankar-CS
Copy link

I was needing ref to get the input value , i did the simple trick after passing the ref to material ui text field.

<TextField
id="required"
ref={"password"}
defaultValue={"password"}
/>;

I used the this to get the value

let sPassword = this.refs.password.input.value;

@aheidelberg
Copy link

aheidelberg commented Jul 6, 2018

Not sure if I had the same issue as the original issue but my search for a solution led me here. With RootRef as a wrapper I got the DOM ref I needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work 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

7 participants