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

[FlatButton] File input not working with button.

Open actionnick opened this issue 11 years ago • 28 comments

With this...

<FlatButton primary={true} label="Choose an Image">
  <input type="file" id="imageButton"></input>
</FlatButton>

I end up with a button that doesn't actually render a file input field in the dom (clicking on the button does not open up the file dialog).

actionnick avatar May 15 '15 04:05 actionnick

On version 0.7.5.

actionnick avatar May 15 '15 04:05 actionnick

@nschaubeck Does the button on the doc site work for you?

hai-cea avatar May 15 '15 20:05 hai-cea

@hai-cea Yes it does. It seemed like there wasn't even an <input> element in DOM, like the button element wasn't rendering it's children.

actionnick avatar May 15 '15 20:05 actionnick

I'm having this problem as well. @nschaubeck Did you ever find a solution?

e-monson avatar Jun 05 '15 21:06 e-monson

@e-monson I was actually not able to find a solution.

actionnick avatar Jun 08 '15 16:06 actionnick

this works! let styles = { exampleImageInput: { cursor: 'pointer', position: 'absolute', top: '0', bottom: '0', right: '0', left: '0', width: '100%', opacity: '0' } }

      <FlatButton label="Choose an Image" primary={true}>
        <input id="imageButton" style={styles.exampleImageInput} type="file"></input>
      </FlatButton>

nikhildaga avatar Aug 28 '15 20:08 nikhildaga

@nikhildaga That only works on Chrome as placing input fields into buttons is not part of the W3C spec.

A different solution is to make the button just fire the click event of a hidden input field.

handleChange: function(e){
  console.log(e.target.value)
},
_openFileDialog: function(){
  var fileUploadDom = React.findDOMNode(this.refs.fileUpload);
  fileUploadDom.click();
},
render: function() {
  return (
    <FlatButton
      label="Upload file"
      onClick={this._openFileDialog}/>
    <input
      ref="fileUpload"
      type="file" 
      style={{"display" : "none"}}
      onChange={this._handleChange}/>
  );
}

Wofiel avatar Sep 07 '15 16:09 Wofiel

I see this issue is still not fixed correctly... The solution proposed in the doc only works in Chrome as @Wofiel said. (still the case in beta 15-0.2)

Wouldn't it better to be able to set like a htmlFor to the <FlatButton> (or <RaisedButton>), set an Id to the input file and that's all ? You wouldn't need to trigger the click manually... I tried wrapping the button in the label, unfortunately, that doesn't work :(

Right now @Wofiel seems to be the best solution.

seb-vial avatar Apr 27 '16 13:04 seb-vial

Why close this when there is not a native solution

mewsu avatar May 12 '16 18:05 mewsu

hi @Wofiel how do you suggest using this technique without refs in a stateless component

excalliburbd avatar Jul 19 '16 07:07 excalliburbd

@excalliburbd You can use id and Jquery, that should work

seb-vial avatar Jul 19 '16 08:07 seb-vial

@Birssan dom manipulation in react? is that okay?

excalliburbd avatar Jul 19 '16 09:07 excalliburbd

I would not recommand it usually, but I think to trigger a click event that should be ok in this case

seb-vial avatar Jul 19 '16 09:07 seb-vial

@Wofiel As can be done with version 15 react?

juandav avatar Aug 08 '16 15:08 juandav

@Wofiel with this solution console.log(e.target.value) one receive C:\fakepath\imagefilename.png Best solutions is e.target.files[0]

code-by avatar Sep 05 '16 10:09 code-by

If anyone else comes across this issue I found that you can set containerElement on the RaisedButton to be something more suited to having an input element as a child. For example a label ;-)

<RaisedButton
  containerElement='label' // <-- Just add me!
  label='My Label'>
    <input type="file" />
</RaisedButton>

Thomas101 avatar Sep 06 '16 11:09 Thomas101

@Thomas101 Thanks! I've just encountered this issue and your solution seems to work. Awesome :)

mat4n avatar Sep 06 '16 14:09 mat4n

@Thomas101 Thanks for the tip. This solved my problem as well. The actual <span key="labelElement"... was a deadzone for my click event. Your solution fixed this!

trialTrail avatar Jan 11 '17 17:01 trialTrail

Just complementing @Thomas101 answer, when I declare the components like his example, my RaisedButton is rendered with a file input inside. To prevent this, just set the file input style to display: none. :)

<RaisedButton
  containerElement='label' // <-- Just add me!
  label='My Label'>
    <input type="file" style={{ display: 'none' }} />
</RaisedButton>

andreyluiz avatar Apr 11 '17 13:04 andreyluiz

@andreyluiz under what circumstances does this not work:

<RaisedButton label='My Label'>
    <input type="file" />
</RaisedButton>

I haven't been able to find an issue wit the docs site example, but clearly many others have this issue!

mbrookes avatar Apr 11 '17 20:04 mbrookes