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 doesn't support 'ref' ? #2699

Closed
dcsan opened this issue Dec 29, 2015 · 26 comments · Fixed by #2703
Closed

TextField doesn't support 'ref' ? #2699

dcsan opened this issue Dec 29, 2015 · 26 comments · Fixed by #2703
Labels
component: text field This is the name of the generic UI component, not the React module!

Comments

@dcsan
Copy link

dcsan commented Dec 29, 2015

I'm defining a TextField component (within a card) which I pass a ref
but the ref isn't defined ...

<div ref='lessonDetailContainer'>
...
            <TextField
              defaultValue={lessonData.vocab}
              multiLine={true}
              floatingLabelText='vocab'
              ref='lessonVocab'
            />
</div>

How are people making forms with material-ui? Just using the onChange events on every part of the form?

FWIW the TextField is inside a Card/Form
the ref on the wrapper div works fine.

like the very second issue in this repo...
#2

@alitaheri
Copy link
Member

I'm working on this as well. The problem is that the ref is passed down to the input. something that doesn't work with React. I'm addressing this in the following PR I'll make.

@dcsan
Copy link
Author

dcsan commented Dec 29, 2015

OK great, let me know if i can help! when do you expect the PR to land?

@alitaheri
Copy link
Member

In 20 minute at most.

@dcsan
Copy link
Author

dcsan commented Dec 29, 2015

i'm not sure if your PR will address this, but in trying to workaround,
none of the events send info in the parameters either:

            <TextField
              defaultValue={this.data.topic.inherits}
              floatingLabelText='inherits'
              onChange={this.editTopic}
            />
  editTopic(evt, p1, p2, p3) {
    evt.preventDefault();
    Logger.loga(['editTopic', evt, p1, p2, p3], logfile);

with onChange event and onEnterKeyDown property
there are no other values sent apart from the synthetic event.
just p1, p2 etc are undefined. I think this is a regression?

so we can't read via ref, or by using the component methods.
that makes it impossible to use these fields for anything other than display, afai can see?

Is there another workaround to use these text fields for forms, or one event that does send the fields data along?

http://www.material-ui.com/#/components/text-fields

@alitaheri
Copy link
Member

It does get passed into that callback, via the first argument evt.target.value. It's a lot messed up, granted. we have to address a lot of issues regarding these components

@mbrookes

This comment has been minimized.

@alitaheri

This comment has been minimized.

@mbrookes

This comment has been minimized.

@alitaheri

This comment has been minimized.

@mbrookes

This comment has been minimized.

@alitaheri

This comment has been minimized.

@schuchowsky
Copy link

So the solution for being able to use a ref in the TextField is to use another library? 👎

@lucasbento
Copy link

@schuchowsky: this issue is from December 30th, this is working since a long time ago, man.

@schuchowsky
Copy link

I'm trying to use a ref on a TextField, to attach an onfocus event on it. On componentDidMount the ref's value is null... Are you sure it is working?

@lucasbento
Copy link

@schuchowsky: yes, 2 apps running in production and using a few ref props here and there.

I strongly suggest using https://github.com/mbrookes/formsy-material-ui though, it's an awesome formsy-react wrapper to match Material UI style.

If you are indeed having an issue with ref on <TextField />, please open another issue following the ISSUE_TEMPLATE, commenting on issues from 1 year ago is definitely not gonna help you.

@isuruAb
Copy link

isuruAb commented May 14, 2017

@TeodorKolev
Copy link

Not working in 1.0

@sfowl73
Copy link

sfowl73 commented Jan 24, 2018

@TeodorKolev It looks like they have an "inputRef" prop in the new textfield https://material-ui.com/api/text-field/

@julianoappelklein
Copy link

julianoappelklein commented Mar 15, 2018

What worked for me, in this case and others using Material UI components, was to wrap a regular component (i.e. div or span) around my Material UI component.
Once I got the wrapper reference, using the querySelector, I could query for any needed element.

Getting the wrapper reference:

<div
     ref={(div) => { this.textAreaWrapper = div; }}
     style={{ flex:1, marginRight: '7px' }}
>            
     <TextField
               onChange={this.onChange.bind(this)}
               value={this.state.value}
               floatingLabelFixed={true}
               multiLine={multiLine}
               fullWidth={true}
               rowsMax={30}
               underlineShow={true}
               textareaStyle={{minHeight: '80px'}}
               style={{paddingRight, transition:'none', boxSizing:'border-box'}}
               floatingLabelText={field.title} />
</div>

Using it:

fixHeight(){
      if(this.inputWrapper){
            let textArea = this.textAreaWrapper.querySelector('textarea[id]');
            if(textArea){
                let maxHeight = textArea.clientHeight + 48;
                this.setState({maxHeight});
            }
      }
}

Code extracted from my open source CMS for Hugo sites.

@jankalfus
Copy link

Wrap your component using RootRef https://material-ui.com/api/root-ref/

@mwaeckerlin
Copy link

mwaeckerlin commented Mar 16, 2019

@jankalfus: How is RootRef supposed to work? It seems to be incompatible with the default React-ref, since, e.g here.:

      <RootRef rootRef={inputs.name}>
        <TextField
          id="name"
          label={t("x")}
          placeholder={t("y")}
        />
      </RootRef>

where inputs.name was initialized as: inputs.name = React.createRef();

inputs.name.current is defined, but input.ref.current.value is undefined!

To be compatible to the react-ref-hook-standard, input.ref.current.value should contain the value of the text field!

  • What's wrong?
  • Is this a bug?
  • What can I do as workaround?

@jankalfus
Copy link

@mwaeckerlin
That's because you're trying to access a value of the wrapper div, not the input. See this fiddle for an example: https://jsfiddle.net/8ojwcvyd/
The key part is this: this.myRef.current.querySelector("input").value

@mwaeckerlin
Copy link

@mwaeckerlin
Copy link

BTW, @jankalfus, I was looking for hours for a tutorial or example on how to use ref=… in material-ui and only found that nearly undocumented RootRef plus some tickets and some questions in Stack Overflow, but none with a good and working solution.

That means, I was not successful in finding a good comprehensive documentation on how to use React.createRef with material-ui. In any case: Please add some comprehensive documentation on how to properly use ref=… from React.createRef() in material-ui, including access to the value!

What I really want, is to use ref in exactly the same way, as I would use it for a standard <input> element. The solution above is the best I found.

I suggest to make this the default behaviour (so that no explicit onChange is necessary) and to forget about <RootRef>, which seems to me to be a dirty hack only.

@mwaeckerlin
Copy link

BTW: I'll use it in conjunction with an addition to Drizzle-React-Components that I am working on.

@gitKendra
Copy link

In case anyone comes across this looking for answer like I did, take a look at the recent TextField API documentation (https://material-ui.com/api/text-field/#textfield-api). It has an inputRef property to pass a ref to the input element and access the value like a normal <input>

// Create ref
const myInputRef = React.useRef()

// Get value from input ref
myInputRef.current.value

// Create component that uses the ref
<TextField inputRef={myInputRef} />

@zannager zannager added the component: text field This is the name of the generic UI component, not the React module! label Dec 20, 2022
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

Successfully merging a pull request may close this issue.