material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

TextField doesn't support 'ref' ?

Open dcsan opened this issue 10 years ago • 26 comments

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... https://github.com/callemall/material-ui/issues/2

dcsan avatar Dec 29 '15 17:12 dcsan

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.

alitaheri avatar Dec 29 '15 17:12 alitaheri

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

dcsan avatar Dec 29 '15 18:12 dcsan

In 20 minute at most.

alitaheri avatar Dec 29 '15 18:12 alitaheri

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

dcsan avatar Dec 29 '15 18:12 dcsan

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

alitaheri avatar Dec 29 '15 18:12 alitaheri

@dcsan - Just a little plug for https://github.com/mbrookes/formsy-material-ui - even if you don't want to use formsy for form validation, it might help you with ideas for using the components in forms.

mbrookes avatar Dec 29 '15 22:12 mbrookes

@mbrookes Good job on your project :+1: :+1: I liked it. might not be bad to open an issue requesting a mention in the README.

alitaheri avatar Dec 30 '15 06:12 alitaheri

Thanks @alitaheri - maybe a "related projects" section? I'll have a dig around github and see if there are others. I don't think it could be justified for mine alone. :)

mbrookes avatar Dec 30 '15 21:12 mbrookes

@mbrookes That would be great :+1: :+1: Thanks a lot

alitaheri avatar Dec 30 '15 21:12 alitaheri

@alitaheri I searched guthub for material-ui sorted by popularity, and while there are lots of Material Design related projects projects that use material-ui in their name, most are not related to material-ui at all. (It says something about this project that it's name is now synonymous with Material Design!). There may well be material-ui related projects that don't use material-ui in their name, but github's search is going to make them hard to find. I tried NPM as well, but nothing further there, If anything pops up in the future we can revisit this.

@dcsan, sorry to hijack your issue!

mbrookes avatar Dec 30 '15 21:12 mbrookes

@mbrookes Thanks for the effort :+1: :+1: Feel free to open another issue requesting that page. I think is good pattern to promote these related project as they might fulfill some needs our users have. :+1:

alitaheri avatar Dec 30 '15 21:12 alitaheri

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

schuchowsky avatar Dec 21 '16 18:12 schuchowsky

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

lucasbento avatar Dec 21 '16 18:12 lucasbento

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?

schuchowsky avatar Dec 21 '16 18:12 schuchowsky

@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.

lucasbento avatar Dec 21 '16 18:12 lucasbento

@schuchowsky check this, this way it works for me. http://stackoverflow.com/questions/43960183/material-ui-component-reference-does-not-work

isuruAb avatar May 14 '17 04:05 isuruAb

Not working in 1.0

TeodorKolev avatar Sep 30 '17 09:09 TeodorKolev

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

sfowl73 avatar Jan 24 '18 16:01 sfowl73

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.

julianoappelklein avatar Mar 15 '18 00:03 julianoappelklein

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

jankalfus avatar Jan 23 '19 14:01 jankalfus