-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[FlatButton] File input not working with button. #647
Comments
On version 0.7.5. |
@nschaubeck Does the button on the doc site work for you? |
@hai-cea Yes it does. It seemed like there wasn't even an |
I'm having this problem as well. @nschaubeck Did you ever find a solution? |
@e-monson I was actually not able to find a solution. |
this works!
|
@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}/>
);
} |
I see this issue is still not fixed correctly... Wouldn't it better to be able to set like a Right now @Wofiel seems to be the best solution. |
Why close this when there is not a native solution |
hi @Wofiel how do you suggest using this technique without refs in a stateless component |
@excalliburbd You can use id and Jquery, that should work |
@Birssan dom manipulation in react? is that okay? |
I would not recommand it usually, but I think to trigger a click event that should be ok in this case |
@Wofiel As can be done with version 15 react? |
@Wofiel with this solution |
If anyone else comes across this issue I found that you can set <RaisedButton
containerElement='label' // <-- Just add me!
label='My Label'>
<input type="file" />
</RaisedButton> |
@Thomas101 Thanks! I've just encountered this issue and your solution seems to work. |
@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! |
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 <RaisedButton
containerElement='label' // <-- Just add me!
label='My Label'>
<input type="file" style={{ display: 'none' }} />
</RaisedButton> |
@andreyluiz under what circumstances does this not work:
I haven't been able to find an issue wit the docs site example, but clearly many others have this issue! |
Is it suposed to the file input to not show with the code you provided? That's weird. In my case it renders an file input inside the material-ui button. |
@andreyluiz Yes, with the docs site example (http://www.material-ui.com/#/components/raised-button) there's no visible |
The latest Chrome version. |
I agree with @andreyluiz , the shows as well, but with a |
tl;dr <RaisedButton
containerElement="label"
icon={<Icons.FileUpload />} // material-ui-icons
labelColor="white"
primary
style={{ minWidth: 40, width: 40 }}>
<input
onChange={e => this.upload(e.target.files[0])}
style={{ display: 'none' }}
type="file"
/>
</RaisedButton> |
The above solution doesn't seem to work any more. I'm using the new v1 beta branch and the following doesn't work for me
I get the following error in the console:
Anybody know a workaround? Anyways shouldn't such a common use case be natively supported? (in a non-hacky way) |
@AdityaAnand1 the following code should work
|
https://stackoverflow.com/a/54043619. it is simple |
With this...
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).
The text was updated successfully, but these errors were encountered: